From c66f971e3b06d986e4d72fd2e3e9ba860f2a3064 Mon Sep 17 00:00:00 2001 From: Lukasz Muszkieta Date: Fri, 8 Feb 2019 14:14:12 +0100 Subject: Add junit tests for AdapterRestClient Change-Id: Ieb92404710b716c40762365965852dc9a00947c2 Issue-ID: SO-1480 Signed-off-by: Lukasz Muszkieta --- .../client/adapter/rest/AdapterRestClientTest.java | 109 +++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 common/src/test/java/org/onap/so/client/adapter/rest/AdapterRestClientTest.java (limited to 'common/src') 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 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; + } +} -- cgit 1.2.3-korg