Skip to content
This repository was archived by the owner on Apr 22, 2025. It is now read-only.

Commit 0b3e5aa

Browse files
committed
FAB-6901 Affiliation is not required.
Change-Id: I5261bebeb6c3c1439911a59023dfc49e4da0dce4 Signed-off-by: rickr <cr22rc@gmail.com>
1 parent b9da935 commit 0b3e5aa

File tree

2 files changed

+70
-11
lines changed

2 files changed

+70
-11
lines changed

src/main/java/org/hyperledger/fabric_ca/sdk/RegistrationRequest.java

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,30 +34,52 @@ public class RegistrationRequest {
3434
// The enrollment ID of the user
3535
private String enrollmentID;
3636
// Type of identity
37-
private String type;
37+
private String type = "user";
3838
// Optional secret
3939
private String secret;
4040
// Maximum number of enrollments with the secret
41-
private int maxEnrollments;
41+
private Integer maxEnrollments = null;
4242
// Affiliation for a user
4343
private String affiliation;
4444
// Array of attribute names and values
45-
private Collection<Attribute> attrs;
45+
private Collection<Attribute> attrs = new ArrayList<Attribute>();
4646

4747
private String caName;
4848

4949
// Constructor
50+
51+
/**
52+
* Register user with certificate authority
53+
*
54+
* @param id The id of the user to register.
55+
* @throws Exception
56+
*/
57+
public RegistrationRequest(String id) throws Exception {
58+
if (id == null) {
59+
throw new Exception("id may not be null");
60+
}
61+
this.enrollmentID = id;
62+
63+
}
64+
65+
/**
66+
* Register user with certificate authority
67+
*
68+
* @param id The id of the user to register.
69+
* @param affiliation The user's affiliation.
70+
* @throws Exception
71+
*/
5072
public RegistrationRequest(String id, String affiliation) throws Exception {
5173
if (id == null) {
5274
throw new Exception("id may not be null");
5375
}
5476
if (affiliation == null) {
5577
throw new Exception("affiliation may not be null");
5678
}
79+
5780
this.enrollmentID = id;
5881
this.affiliation = affiliation;
59-
this.type = "user";
60-
this.attrs = new ArrayList<Attribute>();
82+
6183
}
6284

6385
public String getEnrollmentID() {
@@ -76,7 +98,7 @@ public void setSecret(String secret) {
7698
this.secret = secret;
7799
}
78100

79-
public int getMaxEnrollments() {
101+
public Integer getMaxEnrollments() {
80102
return maxEnrollments;
81103
}
82104

@@ -113,7 +135,7 @@ void setCAName(String caName) {
113135
}
114136

115137
// Convert the registration request to a JSON string
116-
public String toJson() {
138+
String toJson() {
117139
StringWriter stringWriter = new StringWriter();
118140
JsonWriter jsonWriter = Json.createWriter(new PrintWriter(stringWriter));
119141
jsonWriter.writeObject(toJsonObject());
@@ -122,15 +144,21 @@ public String toJson() {
122144
}
123145

124146
// Convert the registration request to a JSON object
125-
public JsonObject toJsonObject() {
147+
JsonObject toJsonObject() {
126148
JsonObjectBuilder ob = Json.createObjectBuilder();
127149
ob.add("id", enrollmentID);
128150
ob.add("type", type);
129151
if (this.secret != null) {
130152
ob.add("secret", secret);
131153
}
132-
ob.add("max_enrollments", maxEnrollments);
133-
ob.add("affiliation", affiliation);
154+
if (null != maxEnrollments) {
155+
156+
ob.add("max_enrollments", maxEnrollments);
157+
}
158+
if (affiliation != null) {
159+
ob.add("affiliation", affiliation);
160+
}
161+
134162
JsonArrayBuilder ab = Json.createArrayBuilder();
135163
for (Attribute attr : attrs) {
136164
ab.add(attr.toJsonObject());

src/test/java/org/hyperledger/fabric_ca/sdk/RegistrationRequestTest.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
package org.hyperledger.fabric_ca.sdk;
1616

17+
import javax.json.JsonObject;
18+
1719
import org.junit.Assert;
1820
import org.junit.Test;
1921

@@ -35,10 +37,39 @@ public void testNewInstance() {
3537
RegistrationRequest testRegisterReq = new RegistrationRequest(regID, regAffiliation);
3638
Assert.assertEquals(testRegisterReq.getEnrollmentID(), regID);
3739
Assert.assertEquals(testRegisterReq.getType(), regType);
38-
Assert.assertEquals(testRegisterReq.getMaxEnrollments(), 0);
40+
Assert.assertEquals(testRegisterReq.getMaxEnrollments(), null);
3941
Assert.assertEquals(testRegisterReq.getAffiliation(), regAffiliation);
4042
Assert.assertTrue(testRegisterReq.getAttributes().isEmpty());
4143

44+
JsonObject jo = testRegisterReq.toJsonObject();
45+
46+
Assert.assertEquals(jo.getString("affiliation"), regAffiliation);
47+
Assert.assertFalse(jo.containsKey("max_enrollments"));
48+
Assert.assertEquals(regID, jo.getString("id"));
49+
50+
} catch (Exception e) {
51+
Assert.fail("Unexpected Exception " + e.getMessage());
52+
}
53+
}
54+
55+
@Test
56+
public void testNewInstanceNoAffiliation() {
57+
58+
try {
59+
RegistrationRequest testRegisterReq = new RegistrationRequest(regID);
60+
testRegisterReq.setMaxEnrollments(3);
61+
62+
Assert.assertEquals(regID, testRegisterReq.getEnrollmentID());
63+
Assert.assertEquals(regType, testRegisterReq.getType());
64+
Assert.assertEquals(new Integer(3), testRegisterReq.getMaxEnrollments());
65+
Assert.assertEquals(null, testRegisterReq.getAffiliation());
66+
Assert.assertTrue(testRegisterReq.getAttributes().isEmpty());
67+
68+
JsonObject jo = testRegisterReq.toJsonObject();
69+
Assert.assertFalse(jo.containsKey("affiliation"));
70+
Assert.assertEquals(3, jo.getInt("max_enrollments"));
71+
Assert.assertEquals(regID, jo.getString("id"));
72+
4273
} catch (Exception e) {
4374
Assert.fail("Unexpected Exception " + e.getMessage());
4475
}

0 commit comments

Comments
 (0)