Skip to content

Commit

Permalink
FAB-6901 Affiliation is not required.
Browse files Browse the repository at this point in the history
Change-Id: I5261bebeb6c3c1439911a59023dfc49e4da0dce4
Signed-off-by: rickr <cr22rc@gmail.com>
  • Loading branch information
cr22rc committed Nov 7, 2017
1 parent b9da935 commit 0b3e5aa
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,30 +34,52 @@ public class RegistrationRequest {
// The enrollment ID of the user
private String enrollmentID;
// Type of identity
private String type;
private String type = "user";
// Optional secret
private String secret;
// Maximum number of enrollments with the secret
private int maxEnrollments;
private Integer maxEnrollments = null;
// Affiliation for a user
private String affiliation;
// Array of attribute names and values
private Collection<Attribute> attrs;
private Collection<Attribute> attrs = new ArrayList<Attribute>();

private String caName;

// Constructor

/**
* Register user with certificate authority
*
* @param id The id of the user to register.
* @throws Exception
*/
public RegistrationRequest(String id) throws Exception {
if (id == null) {
throw new Exception("id may not be null");
}
this.enrollmentID = id;

}

/**
* Register user with certificate authority
*
* @param id The id of the user to register.
* @param affiliation The user's affiliation.
* @throws Exception
*/
public RegistrationRequest(String id, String affiliation) throws Exception {
if (id == null) {
throw new Exception("id may not be null");
}
if (affiliation == null) {
throw new Exception("affiliation may not be null");
}

this.enrollmentID = id;
this.affiliation = affiliation;
this.type = "user";
this.attrs = new ArrayList<Attribute>();

}

public String getEnrollmentID() {
Expand All @@ -76,7 +98,7 @@ public void setSecret(String secret) {
this.secret = secret;
}

public int getMaxEnrollments() {
public Integer getMaxEnrollments() {
return maxEnrollments;
}

Expand Down Expand Up @@ -113,7 +135,7 @@ void setCAName(String caName) {
}

// Convert the registration request to a JSON string
public String toJson() {
String toJson() {
StringWriter stringWriter = new StringWriter();
JsonWriter jsonWriter = Json.createWriter(new PrintWriter(stringWriter));
jsonWriter.writeObject(toJsonObject());
Expand All @@ -122,15 +144,21 @@ public String toJson() {
}

// Convert the registration request to a JSON object
public JsonObject toJsonObject() {
JsonObject toJsonObject() {
JsonObjectBuilder ob = Json.createObjectBuilder();
ob.add("id", enrollmentID);
ob.add("type", type);
if (this.secret != null) {
ob.add("secret", secret);
}
ob.add("max_enrollments", maxEnrollments);
ob.add("affiliation", affiliation);
if (null != maxEnrollments) {

ob.add("max_enrollments", maxEnrollments);
}
if (affiliation != null) {
ob.add("affiliation", affiliation);
}

JsonArrayBuilder ab = Json.createArrayBuilder();
for (Attribute attr : attrs) {
ab.add(attr.toJsonObject());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

package org.hyperledger.fabric_ca.sdk;

import javax.json.JsonObject;

import org.junit.Assert;
import org.junit.Test;

Expand All @@ -35,10 +37,39 @@ public void testNewInstance() {
RegistrationRequest testRegisterReq = new RegistrationRequest(regID, regAffiliation);
Assert.assertEquals(testRegisterReq.getEnrollmentID(), regID);
Assert.assertEquals(testRegisterReq.getType(), regType);
Assert.assertEquals(testRegisterReq.getMaxEnrollments(), 0);
Assert.assertEquals(testRegisterReq.getMaxEnrollments(), null);
Assert.assertEquals(testRegisterReq.getAffiliation(), regAffiliation);
Assert.assertTrue(testRegisterReq.getAttributes().isEmpty());

JsonObject jo = testRegisterReq.toJsonObject();

Assert.assertEquals(jo.getString("affiliation"), regAffiliation);
Assert.assertFalse(jo.containsKey("max_enrollments"));
Assert.assertEquals(regID, jo.getString("id"));

} catch (Exception e) {
Assert.fail("Unexpected Exception " + e.getMessage());
}
}

@Test
public void testNewInstanceNoAffiliation() {

try {
RegistrationRequest testRegisterReq = new RegistrationRequest(regID);
testRegisterReq.setMaxEnrollments(3);

Assert.assertEquals(regID, testRegisterReq.getEnrollmentID());
Assert.assertEquals(regType, testRegisterReq.getType());
Assert.assertEquals(new Integer(3), testRegisterReq.getMaxEnrollments());
Assert.assertEquals(null, testRegisterReq.getAffiliation());
Assert.assertTrue(testRegisterReq.getAttributes().isEmpty());

JsonObject jo = testRegisterReq.toJsonObject();
Assert.assertFalse(jo.containsKey("affiliation"));
Assert.assertEquals(3, jo.getInt("max_enrollments"));
Assert.assertEquals(regID, jo.getString("id"));

} catch (Exception e) {
Assert.fail("Unexpected Exception " + e.getMessage());
}
Expand Down

0 comments on commit 0b3e5aa

Please sign in to comment.