|
| 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