aboutsummaryrefslogtreecommitdiffstats
path: root/common/src/test/java
diff options
context:
space:
mode:
authorLukasz Muszkieta <lukasz.muszkieta@nokia.com>2019-02-08 14:14:12 +0100
committerLukasz Muszkieta <lukasz.muszkieta@nokia.com>2019-02-08 14:19:30 +0100
commitc66f971e3b06d986e4d72fd2e3e9ba860f2a3064 (patch)
treeec2d7fce31383d067e94156bcd224a2e5d765e9a /common/src/test/java
parent0f27e7f776cc0d26a4810b76787cf67bc18d03ab (diff)
Add junit tests for AdapterRestClient
Change-Id: Ieb92404710b716c40762365965852dc9a00947c2 Issue-ID: SO-1480 Signed-off-by: Lukasz Muszkieta <lukasz.muszkieta@nokia.com>
Diffstat (limited to 'common/src/test/java')
-rw-r--r--common/src/test/java/org/onap/so/client/adapter/rest/AdapterRestClientTest.java109
1 files changed, 109 insertions, 0 deletions
diff --git a/common/src/test/java/org/onap/so/client/adapter/rest/AdapterRestClientTest.java b/common/src/test/java/org/onap/so/client/adapter/rest/AdapterRestClientTest.java
new file mode 100644
index 0000000000..f4490faacc
--- /dev/null
+++ b/common/src/test/java/org/onap/so/client/adapter/rest/AdapterRestClientTest.java
@@ -0,0 +1,109 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP - SO
+ * ================================================================================
+ * Copyright (C) 2019 Nokia.
+ * ================================================================================
+ * 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.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.so.client.adapter.rest;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.entry;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.security.GeneralSecurityException;
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.commons.codec.binary.Base64;
+import org.junit.Before;
+import org.junit.Test;
+import org.onap.so.client.policy.JettisonStyleMapperProvider;
+import org.onap.so.utils.CryptoUtils;
+import org.onap.so.utils.TargetEntity;
+
+public class AdapterRestClientTest {
+
+ private static final String CRYPTO_KEY = "546573746F736973546573746F736973";
+ private static final String INVALID_CRYPTO_KEY = "1234";
+
+ private Map<String, String> headerMap;
+ private AdapterRestProperties adapterRestPropertiesMock;
+
+ @Before
+ public void setup() {
+ headerMap = new HashMap<>();
+ adapterRestPropertiesMock = mock(AdapterRestProperties.class);
+ }
+
+ @Test
+ public void initializeHeaderMap_success() throws URISyntaxException, GeneralSecurityException {
+ // given
+ String encyptedMessage = CryptoUtils.encrypt("testAdapter", CRYPTO_KEY);
+ when(adapterRestPropertiesMock.getAuth()).thenReturn(encyptedMessage);
+ when(adapterRestPropertiesMock.getKey()).thenReturn(CRYPTO_KEY);
+ AdapterRestClient testedObject = new AdapterRestClient(adapterRestPropertiesMock, new URI(""));
+ // when
+ testedObject.initializeHeaderMap(headerMap);
+ // then
+ assertThat(headerMap).containsOnly(entry("Authorization", getExpectedEncodedString(encyptedMessage)));
+ }
+
+ @Test
+ public void initializeHeaderMap_putNullToMapWhenAuthIsNull() throws URISyntaxException {
+ // given
+ AdapterRestClient testedObject = new AdapterRestClient(adapterRestPropertiesMock, new URI(""));
+ // when
+ testedObject.initializeHeaderMap(headerMap);
+ // then
+ assertThat(headerMap).containsOnly(entry("Authorization", null));
+ }
+
+ @Test
+ public void initializeHeaderMap_putNullToMapWhenExOccurs() throws URISyntaxException, GeneralSecurityException {
+ // given
+ String encyptedMessage = CryptoUtils.encrypt("testAdapter", CRYPTO_KEY);
+ when(adapterRestPropertiesMock.getAuth()).thenReturn(encyptedMessage);
+ when(adapterRestPropertiesMock.getKey()).thenReturn(INVALID_CRYPTO_KEY);
+ AdapterRestClient testedObject = new AdapterRestClient(adapterRestPropertiesMock, new URI(""),
+ "accept", "contentType");
+ // when
+ testedObject.initializeHeaderMap(headerMap);
+ // then
+ assertThat(headerMap).containsOnly(entry("Authorization", null));
+ }
+
+ @Test
+ public void getTargetEntity_success() throws URISyntaxException {
+ AdapterRestClient testedObject = new AdapterRestClient(adapterRestPropertiesMock, new URI(""));
+ assertThat(testedObject.getTargetEntity()).isEqualTo(TargetEntity.OPENSTACK_ADAPTER);
+ }
+
+ @Test
+ public void getCommonObjectMapperProvider_success() throws URISyntaxException {
+ AdapterRestClient testedObject = new AdapterRestClient(adapterRestPropertiesMock, new URI(""));
+ assertThat(testedObject.getCommonObjectMapperProvider()).isInstanceOf(JettisonStyleMapperProvider.class);
+ }
+
+ private String getExpectedEncodedString(String encryptedMessage) throws GeneralSecurityException {
+ String auth = CryptoUtils.decrypt(encryptedMessage, CRYPTO_KEY);
+ byte[] encoded = Base64.encodeBase64(auth.getBytes());
+ String encodedString = new String(encoded);
+ return "Basic " + encodedString;
+ }
+}