Skip to content

Commit

Permalink
FAB-4454 Increase IT coverage for HFCAClient
Browse files Browse the repository at this point in the history
. 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>
  • Loading branch information
chrism28282828 committed Jun 18, 2017
1 parent 7c87f20 commit fc1d7ce
Show file tree
Hide file tree
Showing 3 changed files with 436 additions and 90 deletions.
10 changes: 7 additions & 3 deletions src/main/java/org/hyperledger/fabric_ca/sdk/HFCAClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,12 @@ public Enrollment enroll(String user, String secret, EnrollmentRequest req) thro
}

JsonObject result = jsonst.getJsonObject("result");
if (result == null) {
throw new EnrollmentException(format("FabricCA failed enrollment for user %s - response did not contain a result", user));
}

Base64.Decoder b64dec = Base64.getDecoder();

String signedPem = new String(b64dec.decode(result.getString("Cert").getBytes(UTF_8)));
logger.debug(format("[HFCAClient] enroll returned pem:[%s]", signedPem));

Expand Down Expand Up @@ -525,8 +530,7 @@ public void revoke(User revoker, String revokee, String reason) throws Revocatio
* @return Body of post returned.
* @throws Exception
*/

private String httpPost(String url, String body, UsernamePasswordCredentials credentials) throws Exception {
String httpPost(String url, String body, UsernamePasswordCredentials credentials) throws Exception {
logger.debug(format("httpPost %s, body:%s", url, body));
CredentialsProvider provider = new BasicCredentialsProvider();

Expand Down Expand Up @@ -578,7 +582,7 @@ private String httpPost(String url, String body, UsernamePasswordCredentials cre
return responseBody;
}

private JsonObject httpPost(String url, String body, String authHTTPCert) throws Exception {
JsonObject httpPost(String url, String body, String authHTTPCert) throws Exception {

HttpPost httpPost = new HttpPost(url);
logger.debug(format("httpPost %s, body:%s, authHTTPCert: %s", url, body, authHTTPCert));
Expand Down
95 changes: 95 additions & 0 deletions src/test/java/org/hyperledger/fabric_ca/sdk/MockHFCAClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright 2017 DTCC, Fujitsu Australia Software Technology, IBM - All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.hyperledger.fabric_ca.sdk;

import java.io.StringReader;
import java.net.MalformedURLException;
import java.util.Properties;

import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonReader;

import org.apache.http.auth.UsernamePasswordCredentials;
import org.hyperledger.fabric_ca.sdk.exception.EnrollmentException;
import org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException;

import static java.lang.String.format;

/**
* A Mock class for testing HFCAClient.java
*
*/

public class MockHFCAClient extends HFCAClient {

private String httpPostResponse = null;

MockHFCAClient(String name, String url, Properties properties) throws MalformedURLException {
super(name, url, properties);
}

@Override
String httpPost(String url, String body, UsernamePasswordCredentials credentials) throws Exception {
return httpPostResponse == null ? super.httpPost(url, body, credentials) : httpPostResponse;
}

@Override
JsonObject httpPost(String url, String body, String authHTTPCert) throws Exception {

JsonObject response;

if (httpPostResponse == null) {
response = super.httpPost(url, body, authHTTPCert);
} else {
JsonReader reader = Json.createReader(new StringReader(httpPostResponse));
response = (JsonObject) reader.read();

// TODO: HFCAClient could do with some minor refactoring to avoid duplicating this code here!!
JsonObject result = response.getJsonObject("result");
if (result == null) {
EnrollmentException e = new EnrollmentException(
format("POST request to %s failed request body %s " + "Body of response did not contain result",
url, body),
new Exception());
throw e;
}
}
return response;
}

public static MockHFCAClient createNewInstance(String url, Properties properties) throws MalformedURLException {

return new MockHFCAClient(null, url, properties);
}

public static MockHFCAClient createNewInstance(String name, String url, Properties properties)
throws MalformedURLException, InvalidArgumentException {

if (name == null || name.isEmpty()) {

throw new InvalidArgumentException("name must not be null or an empty string.");
}

return new MockHFCAClient(name, url, properties);
}

// Sets the test string to be returned from httpPost
// If null, it returns the actual response
public void setHttpPostResponse(String httpPostResponse) {
this.httpPostResponse = httpPostResponse;
}

}
Loading

0 comments on commit fc1d7ce

Please # to comment.