import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
+import java.util.Base64;
import java.util.Map;
import java.util.Optional;
private final Integer aaiHostPortNumber;
private final String aaiPath;
private final Map<String,String> aaiHeaders;
+ private final String aaiUserName;
+ private final String aaiUserPassword;
public AAIProducerClient(AAIClientConfiguration aaiClientConfiguration) {
aaiHostPortNumber = aaiClientConfiguration.aaiHostPortNumber();
aaiPath = aaiClientConfiguration.aaiBasePath() + aaiClientConfiguration.aaiPnfPath();
aaiHeaders = aaiClientConfiguration.aaiHeaders();
+ aaiUserName = aaiClientConfiguration.aaiUserName();
+ aaiUserPassword = aaiClientConfiguration.aaiUserPassword();
}
return Optional.empty();
}
});
-
}
private Optional<HttpRequestBase> createRequest(ConsumerDmaapModel consumerDmaapModel) throws URISyntaxException {
});
}
- private HttpPatch createHttpPatch(URI extendedURI, String jsonBody) throws UnsupportedEncodingException {
+ HttpPatch createHttpPatch(URI extendedURI, String jsonBody) throws UnsupportedEncodingException {
HttpPatch httpPatch = new HttpPatch(extendedURI);
httpPatch.setEntity( new StringEntity(jsonBody));
aaiHeaders.forEach(httpPatch::addHeader);
httpPatch.addHeader("Content-Type", "application/merge-patch+json");
+ httpPatch.addHeader("Authorization", "Basic " + encode());
return httpPatch;
}
+
+ String encode() throws UnsupportedEncodingException {
+ return Base64.getEncoder().encodeToString((this.aaiUserName + ":" + this.aaiUserPassword)
+ .getBytes("UTF-8"));
+ }
}
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
+import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
assertNotNull(testedObject.getHttpResponse(consumerDmaapModel));
}
+ @Test
+ void encode_shouldCreateEncodedString_whenUserAndPasswordAreSet() throws UnsupportedEncodingException {
+ // given
+ String expected = "UFJIOlBSSA==";
+ // when
+ String result = testedObject.encode();
+ // then
+ assertNotNull(result);
+ assertEquals(expected, result);
+ }
+
+ @Test
+ void createHttpPatch_shouldContainAuthorizationBasicValue() throws UnsupportedEncodingException {
+ // given
+ String expected = "Authorization: Basic UFJIOlBSSA==";
+ // when
+ HttpPatch patch = testedObject.createHttpPatch(URI.create("localhost"), "{}");
+ // then
+ assertNotNull(patch);
+ assertEquals(expected, patch.getLastHeader("Authorization").toString());
+ }
+
+
private static void setField() throws NoSuchFieldException, IllegalAccessException {
Field field = testedObject.getClass().getDeclaredField("closeableHttpClient");
field.setAccessible(true);
"aaiHost": "localhost",
"aaiHostPortNumber": 8080,
"aaiProtocol": "https",
- "aaiUserName": "admin",
- "aaiUserPassword": "admin",
+ "aaiUserName": "AAI",
+ "aaiUserPassword": "AAI",
"aaiIgnoreSSLCertificateErrors": true,
"aaiBasePath": "/aai/v12",
"aaiPnfPath": "/network/pnfs/pnf",
"X-FromAppId": "prh",
"X-TransactionId": "9999",
"Accept": "application/json",
- "Real-Time": "true",
- "Authorization": "Basic QUFJOkFBSQ=="
+ "Real-Time": "true"
}
}
}
"aaiHostPortNumber": 8080,
"aaiIgnoreSSLCertificateErrors": true,
"aaiProtocol": "https",
- "aaiUserName": "admin",
- "aaiUserPassword": "admin",
+ "aaiUserName": "AAI",
+ "aaiUserPassword": "AAI",
"aaiBasePath": "/aai/v11",
"aaiPnfPath": "/network/pnfs/pnf",
"aaiHeaders": {
"X-FromAppId": "prh",
"X-TransactionId": "9999",
"Accept": "application/json",
- "Real-Time": "true",
- "Authorization": "Basic QUFJOkFBSQ=="
+ "Real-Time": "true"
}
}
},