2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 Nokia.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.so.client.adapter.rest;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.entry;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.when;
29 import java.net.URISyntaxException;
30 import java.security.GeneralSecurityException;
31 import java.util.HashMap;
33 import org.apache.commons.codec.binary.Base64;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.onap.so.client.policy.JettisonStyleMapperProvider;
37 import org.onap.so.utils.CryptoUtils;
38 import org.onap.so.utils.TargetEntity;
40 public class AdapterRestClientTest {
42 private static final String CRYPTO_KEY = "546573746F736973546573746F736973";
43 private static final String INVALID_CRYPTO_KEY = "1234";
45 private Map<String, String> headerMap;
46 private AdapterRestProperties adapterRestPropertiesMock;
50 headerMap = new HashMap<>();
51 adapterRestPropertiesMock = mock(AdapterRestProperties.class);
55 public void initializeHeaderMap_success() throws URISyntaxException, GeneralSecurityException {
57 String encyptedMessage = CryptoUtils.encrypt("testAdapter", CRYPTO_KEY);
58 when(adapterRestPropertiesMock.getAuth()).thenReturn(encyptedMessage);
59 when(adapterRestPropertiesMock.getKey()).thenReturn(CRYPTO_KEY);
60 AdapterRestClient testedObject = new AdapterRestClient(adapterRestPropertiesMock, new URI(""));
62 testedObject.initializeHeaderMap(headerMap);
64 assertThat(headerMap).containsOnly(entry("Authorization", getExpectedEncodedString(encyptedMessage)));
68 public void initializeHeaderMap_putNullToMapWhenAuthIsNull() throws URISyntaxException {
70 AdapterRestClient testedObject = new AdapterRestClient(adapterRestPropertiesMock, new URI(""));
72 testedObject.initializeHeaderMap(headerMap);
74 assertThat(headerMap).containsOnly(entry("Authorization", null));
78 public void initializeHeaderMap_putNullToMapWhenExOccurs() throws URISyntaxException, GeneralSecurityException {
80 String encyptedMessage = CryptoUtils.encrypt("testAdapter", CRYPTO_KEY);
81 when(adapterRestPropertiesMock.getAuth()).thenReturn(encyptedMessage);
82 when(adapterRestPropertiesMock.getKey()).thenReturn(INVALID_CRYPTO_KEY);
83 AdapterRestClient testedObject = new AdapterRestClient(adapterRestPropertiesMock, new URI(""),
84 "accept", "contentType");
86 testedObject.initializeHeaderMap(headerMap);
88 assertThat(headerMap).containsOnly(entry("Authorization", null));
92 public void getTargetEntity_success() throws URISyntaxException {
93 AdapterRestClient testedObject = new AdapterRestClient(adapterRestPropertiesMock, new URI(""));
94 assertThat(testedObject.getTargetEntity()).isEqualTo(TargetEntity.OPENSTACK_ADAPTER);
98 public void getCommonObjectMapperProvider_success() throws URISyntaxException {
99 AdapterRestClient testedObject = new AdapterRestClient(adapterRestPropertiesMock, new URI(""));
100 assertThat(testedObject.getCommonObjectMapperProvider()).isInstanceOf(JettisonStyleMapperProvider.class);
103 private String getExpectedEncodedString(String encryptedMessage) throws GeneralSecurityException {
104 String auth = CryptoUtils.decrypt(encryptedMessage, CRYPTO_KEY);
105 byte[] encoded = Base64.encodeBase64(auth.getBytes());
106 String encodedString = new String(encoded);
107 return "Basic " + encodedString;