Add junit tests for AdapterRestClient 14/78114/1
authorLukasz Muszkieta <lukasz.muszkieta@nokia.com>
Fri, 8 Feb 2019 13:14:12 +0000 (14:14 +0100)
committerLukasz Muszkieta <lukasz.muszkieta@nokia.com>
Fri, 8 Feb 2019 13:19:30 +0000 (14:19 +0100)
Change-Id: Ieb92404710b716c40762365965852dc9a00947c2
Issue-ID: SO-1480
Signed-off-by: Lukasz Muszkieta <lukasz.muszkieta@nokia.com>
common/src/test/java/org/onap/so/client/adapter/rest/AdapterRestClientTest.java [new file with mode: 0644]

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 (file)
index 0000000..f4490fa
--- /dev/null
@@ -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;
+    }
+}