b6ea54c09a9dbb66ee8a3b8ef8eabfc0fdbb879c
[appc.git] / appc-event-listener / appc-event-listener-bundle / src / test / java / org / onap / appc / listener / LCM / operation / ProviderOperationsTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2018 Nokia Solutions and Networks
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * ============LICENSE_END=========================================================
22  */
23 package org.onap.appc.listener.LCM.operation;
24
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertNull;
29 import static org.junit.Assert.assertTrue;
30 import com.att.aft.dme2.internal.jersey.core.util.Base64;
31 import com.fasterxml.jackson.core.JsonProcessingException;
32 import com.fasterxml.jackson.databind.JsonNode;
33 import com.fasterxml.jackson.databind.ObjectMapper;
34 import com.fasterxml.jackson.databind.node.ObjectNode;
35 import java.io.ByteArrayInputStream;
36 import java.io.IOException;
37 import java.io.InputStream;
38 import java.net.Socket;
39 import java.net.SocketException;
40 import java.security.KeyManagementException;
41 import java.security.KeyStore;
42 import java.security.KeyStoreException;
43 import java.security.NoSuchAlgorithmException;
44 import java.security.UnrecoverableKeyException;
45 import org.apache.http.HttpEntity;
46 import org.apache.http.HttpResponse;
47 import org.apache.http.StatusLine;
48 import org.apache.http.client.HttpClient;
49 import org.junit.Before;
50 import org.junit.Test;
51 import org.mockito.Mock;
52 import org.mockito.Mockito;
53 import org.onap.appc.exceptions.APPCException;
54 import org.onap.appc.listener.LCM.operation.ProviderOperations.MySSLSocketFactory;
55
56 public class ProviderOperationsTest {
57
58     private ProviderOperations providerOperations;
59     private MySSLSocketFactory socketFactory;
60
61     @Mock
62     private KeyStore mockKeyStore;
63
64
65     @Before
66     public void setup()
67         throws UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
68
69         providerOperations =
70             new ProviderOperations("http://127.0.0.1", "test_user", "test_password");
71         socketFactory = new MySSLSocketFactory(mockKeyStore);
72     }
73
74     @Test
75     public void setAuthentication_should_return_null_given_null_arguments() {
76         String newAuthentication = providerOperations.setAuthentication(null, null);
77         assertNull(newAuthentication);
78     }
79
80     @Test
81     public void should_set_properties() {
82         providerOperations.setUrl("hp://123.1.2.3");
83         assertEquals("http://127.0.0.1", providerOperations.getUrl());
84         providerOperations.setUrl("http://123.1.2.3");
85         assertEquals("http://123.1.2.3", providerOperations.getUrl());
86
87         String newAuthentication = providerOperations.setAuthentication("new_user", "new_password");
88         assertEquals("new_user:new_password", Base64.base64Decode(newAuthentication));
89     }
90
91     @Test
92     public void isSucceeded_should_resolve_status_codes() {
93
94         assertFalse(ProviderOperations.isSucceeded(null));
95         assertFalse(ProviderOperations.isSucceeded(200));
96         assertTrue(ProviderOperations.isSucceeded(100));
97         assertTrue(ProviderOperations.isSucceeded(400));
98     }
99
100     @Test(expected = APPCException.class)
101     public void topologyDG_should_throw_given_null_message() throws APPCException {
102
103         providerOperations.topologyDG("test-rpc-name", null);
104     }
105
106     @Test(expected = SocketException.class)
107     public void sslSocketFactory_should_throw_when_socket_not_connected() throws IOException {
108         Socket socket = socketFactory.createSocket();
109         assertNotNull(socket);
110
111         socketFactory.createSocket(socket, "127.0.0.1", 123, true);
112     }
113
114     //TODO write some test cases for topologyDG method
115     @Test
116     public void testBuildPostRequest() throws JsonProcessingException, IOException, APPCException {
117         String jsonString = "{\"output\":{\"status\":{\"code\":\"200\",\"message\":\"TEST_MESSAGE\"}}}";
118         providerOperations = Mockito.spy(
119                 new ProviderOperations("http://127.0.0.1", "test_user", "test_password"));
120         HttpClient httpClient = Mockito.mock(HttpClient.class);
121         HttpResponse httpResponse = Mockito.mock(HttpResponse.class);
122         StatusLine statusLine = Mockito.mock(StatusLine.class);
123         Mockito.when(statusLine.getStatusCode()).thenReturn(200);
124         Mockito.when(httpResponse.getStatusLine()).thenReturn(statusLine);
125         HttpEntity httpEntity = Mockito.mock(HttpEntity.class);
126         InputStream inputStream = new ByteArrayInputStream(jsonString.getBytes());
127         Mockito.when(httpEntity.getContent()).thenReturn(inputStream);
128         Mockito.when(httpResponse.getEntity()).thenReturn(httpEntity);
129         Mockito.when(httpClient.execute(Mockito.any())).thenReturn(httpResponse);
130         Mockito.when(providerOperations.getHttpClient()).thenReturn(httpClient);
131         ObjectMapper mapper = new ObjectMapper();
132         JsonNode jsonNode = mapper.readTree(jsonString);
133         assertEquals(ObjectNode.class, providerOperations.topologyDG(null, jsonNode).getClass());
134     }
135 }