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

Commit fc1d7ce

Browse files
FAB-4454 Increase IT coverage for HFCAClient
. Added more tests to increase coverage . Changed the existing tests to use the "thrown" pattern . Added MockHFCAClient to help test coverage . Added mechanism to generate unique users for each run (allows the tests to be run repeatedly without needing to restart CA) . Minor change to HFCAClient to prevent NPEs Change-Id: I920213f1bd7d372ba9200d5de68ca0c0e3344bb7 Signed-off-by: Chris Murphy <chrism@fast.au.fujitsu.com>
1 parent 7c87f20 commit fc1d7ce

File tree

3 files changed

+436
-90
lines changed

3 files changed

+436
-90
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,12 @@ public Enrollment enroll(String user, String secret, EnrollmentRequest req) thro
320320
}
321321

322322
JsonObject result = jsonst.getJsonObject("result");
323+
if (result == null) {
324+
throw new EnrollmentException(format("FabricCA failed enrollment for user %s - response did not contain a result", user));
325+
}
326+
323327
Base64.Decoder b64dec = Base64.getDecoder();
328+
324329
String signedPem = new String(b64dec.decode(result.getString("Cert").getBytes(UTF_8)));
325330
logger.debug(format("[HFCAClient] enroll returned pem:[%s]", signedPem));
326331

@@ -525,8 +530,7 @@ public void revoke(User revoker, String revokee, String reason) throws Revocatio
525530
* @return Body of post returned.
526531
* @throws Exception
527532
*/
528-
529-
private String httpPost(String url, String body, UsernamePasswordCredentials credentials) throws Exception {
533+
String httpPost(String url, String body, UsernamePasswordCredentials credentials) throws Exception {
530534
logger.debug(format("httpPost %s, body:%s", url, body));
531535
CredentialsProvider provider = new BasicCredentialsProvider();
532536

@@ -578,7 +582,7 @@ private String httpPost(String url, String body, UsernamePasswordCredentials cre
578582
return responseBody;
579583
}
580584

581-
private JsonObject httpPost(String url, String body, String authHTTPCert) throws Exception {
585+
JsonObject httpPost(String url, String body, String authHTTPCert) throws Exception {
582586

583587
HttpPost httpPost = new HttpPost(url);
584588
logger.debug(format("httpPost %s, body:%s, authHTTPCert: %s", url, body, authHTTPCert));
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright 2017 DTCC, Fujitsu Australia Software Technology, IBM - All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
package org.hyperledger.fabric_ca.sdk;
16+
17+
import java.io.StringReader;
18+
import java.net.MalformedURLException;
19+
import java.util.Properties;
20+
21+
import javax.json.Json;
22+
import javax.json.JsonObject;
23+
import javax.json.JsonReader;
24+
25+
import org.apache.http.auth.UsernamePasswordCredentials;
26+
import org.hyperledger.fabric_ca.sdk.exception.EnrollmentException;
27+
import org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException;
28+
29+
import static java.lang.String.format;
30+
31+
/**
32+
* A Mock class for testing HFCAClient.java
33+
*
34+
*/
35+
36+
public class MockHFCAClient extends HFCAClient {
37+
38+
private String httpPostResponse = null;
39+
40+
MockHFCAClient(String name, String url, Properties properties) throws MalformedURLException {
41+
super(name, url, properties);
42+
}
43+
44+
@Override
45+
String httpPost(String url, String body, UsernamePasswordCredentials credentials) throws Exception {
46+
return httpPostResponse == null ? super.httpPost(url, body, credentials) : httpPostResponse;
47+
}
48+
49+
@Override
50+
JsonObject httpPost(String url, String body, String authHTTPCert) throws Exception {
51+
52+
JsonObject response;
53+
54+
if (httpPostResponse == null) {
55+
response = super.httpPost(url, body, authHTTPCert);
56+
} else {
57+
JsonReader reader = Json.createReader(new StringReader(httpPostResponse));
58+
response = (JsonObject) reader.read();
59+
60+
// TODO: HFCAClient could do with some minor refactoring to avoid duplicating this code here!!
61+
JsonObject result = response.getJsonObject("result");
62+
if (result == null) {
63+
EnrollmentException e = new EnrollmentException(
64+
format("POST request to %s failed request body %s " + "Body of response did not contain result",
65+
url, body),
66+
new Exception());
67+
throw e;
68+
}
69+
}
70+
return response;
71+
}
72+
73+
public static MockHFCAClient createNewInstance(String url, Properties properties) throws MalformedURLException {
74+
75+
return new MockHFCAClient(null, url, properties);
76+
}
77+
78+
public static MockHFCAClient createNewInstance(String name, String url, Properties properties)
79+
throws MalformedURLException, InvalidArgumentException {
80+
81+
if (name == null || name.isEmpty()) {
82+
83+
throw new InvalidArgumentException("name must not be null or an empty string.");
84+
}
85+
86+
return new MockHFCAClient(name, url, properties);
87+
}
88+
89+
// Sets the test string to be returned from httpPost
90+
// If null, it returns the actual response
91+
public void setHttpPostResponse(String httpPostResponse) {
92+
this.httpPostResponse = httpPostResponse;
93+
}
94+
95+
}

0 commit comments

Comments
 (0)