Cleanup of dmaap adapter
[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 org.apache.commons.codec.binary.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         String authStr = "new_user:new_password";
89         assertEquals(new String(Base64.encodeBase64(authStr.getBytes())), newAuthentication);
90     }
91
92     @Test
93     public void isSucceeded_should_resolve_status_codes() {
94
95         assertFalse(ProviderOperations.isSucceeded(null));
96         assertFalse(ProviderOperations.isSucceeded(200));
97         assertTrue(ProviderOperations.isSucceeded(100));
98         assertTrue(ProviderOperations.isSucceeded(400));
99     }
100
101     @Test(expected = APPCException.class)
102     public void topologyDG_should_throw_given_null_message() throws APPCException {
103
104         providerOperations.topologyDG("test-rpc-name", null);
105     }
106
107     @Test(expected = SocketException.class)
108     public void sslSocketFactory_should_throw_when_socket_not_connected() throws IOException {
109         Socket socket = socketFactory.createSocket();
110         assertNotNull(socket);
111
112         socketFactory.createSocket(socket, "127.0.0.1", 123, true);
113     }
114
115     //TODO write some test cases for topologyDG method
116     @Test
117     public void testBuildPostRequest() throws JsonProcessingException, IOException, APPCException {
118         String jsonString = "{\"output\":{\"status\":{\"code\":\"200\",\"message\":\"TEST_MESSAGE\"}}}";
119         providerOperations = Mockito.spy(
120                 new ProviderOperations("http://127.0.0.1", "test_user", "test_password"));
121         HttpClient httpClient = Mockito.mock(HttpClient.class);
122         HttpResponse httpResponse = Mockito.mock(HttpResponse.class);
123         StatusLine statusLine = Mockito.mock(StatusLine.class);
124         Mockito.when(statusLine.getStatusCode()).thenReturn(200);
125         Mockito.when(httpResponse.getStatusLine()).thenReturn(statusLine);
126         HttpEntity httpEntity = Mockito.mock(HttpEntity.class);
127         InputStream inputStream = new ByteArrayInputStream(jsonString.getBytes());
128         Mockito.when(httpEntity.getContent()).thenReturn(inputStream);
129         Mockito.when(httpResponse.getEntity()).thenReturn(httpEntity);
130         Mockito.when(httpClient.execute(Mockito.any())).thenReturn(httpResponse);
131         Mockito.when(providerOperations.getHttpClient()).thenReturn(httpClient);
132         ObjectMapper mapper = new ObjectMapper();
133         JsonNode jsonNode = mapper.readTree(jsonString);
134         assertEquals(ObjectNode.class, providerOperations.topologyDG(null, jsonNode).getClass());
135     }
136 }