Dmaap micro service jar
[appc.git] / services / appc-dmaap-service / appc-event-listener-bundle / src / test / java / org / onap / appc / listener / demo / impl / TestProviderOperations.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2019 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
6  * file except in compliance with the License. You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software distributed under the License
11  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12  * or implied. See the License for the specific language governing permissions and limitations under
13  * the License.
14  *
15  * SPDX-License-Identifier: Apache-2.0
16  * ============LICENSE_END=========================================================
17  */
18
19 package org.onap.appc.listener.demo.impl;
20
21 import static org.junit.Assert.assertTrue;
22 import static org.mockito.Matchers.anyObject;
23 import static org.mockito.Mockito.when;
24 import java.io.ByteArrayInputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.net.URL;
28 import java.nio.charset.StandardCharsets;
29 import org.apache.http.HttpEntity;
30 import org.apache.http.HttpResponse;
31 import org.apache.http.StatusLine;
32 import org.apache.http.client.ClientProtocolException;
33 import org.apache.http.client.HttpClient;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.Mockito;
38 import org.mockito.internal.util.reflection.Whitebox;
39 import org.onap.appc.exceptions.APPCException;
40 import org.onap.appc.listener.demo.model.Action;
41 import org.onap.appc.listener.demo.model.CommonMessage.CommonHeader;
42 import org.onap.appc.listener.demo.model.CommonMessage.Payload;
43 import org.onap.appc.listener.demo.model.IncomingMessage;
44 import org.onap.appc.listener.util.HttpClientUtil;
45 import org.powermock.api.mockito.PowerMockito;
46 import org.powermock.core.classloader.annotations.PrepareForTest;
47 import org.powermock.modules.junit4.PowerMockRunner;
48
49 @RunWith(PowerMockRunner.class)
50 @PrepareForTest(HttpClientUtil.class)
51 public class TestProviderOperations {
52
53   private ProviderOperations providerOperations;
54   private IncomingMessage message;
55   private URL url;
56   private CommonHeader commonHeader;
57   private Payload payload;
58   private HttpClient httpClient;
59   private HttpResponse httpResponse;
60   private StatusLine statusLine;
61   private HttpEntity httpEntity;
62   private InputStream inputStream;
63   private String reponseMessage;
64
65   @Before
66   public void setUp() throws Exception {
67     reponseMessage = "{\"output\":{\"common-response-header\":{\"success\":true,\"reason\":\"\"}}}";
68     providerOperations = new ProviderOperations();
69     PowerMockito.mockStatic(HttpClientUtil.class);
70     httpClient = PowerMockito.mock(HttpClient.class);
71     httpResponse = PowerMockito.mock(HttpResponse.class);
72     statusLine = PowerMockito.mock(StatusLine.class);
73     httpEntity = PowerMockito.mock(HttpEntity.class);
74     inputStream = new ByteArrayInputStream(reponseMessage.getBytes(StandardCharsets.UTF_8));
75     message = Mockito.mock(IncomingMessage.class);
76     url = PowerMockito.mock(URL.class);
77     commonHeader = Mockito.mock(CommonHeader.class);
78     payload = Mockito.mock(Payload.class);
79     when(message.getAction()).thenReturn(Action.Evacuate);
80     when(message.getHeader()).thenReturn(commonHeader);
81     when(message.getPayload()).thenReturn(payload);
82     when(HttpClientUtil.getHttpClient("http")).thenReturn(httpClient);
83     when(httpClient.execute(anyObject())).thenReturn(httpResponse);
84     when(httpResponse.getStatusLine()).thenReturn(statusLine);
85     when(statusLine.getStatusCode()).thenReturn(200);
86     when(httpResponse.getEntity()).thenReturn(httpEntity);
87     when(httpEntity.getContent()).thenReturn(inputStream);
88     Whitebox.setInternalState(url, "protocol", "http");
89     Whitebox.setInternalState(providerOperations, "url", url);
90   }
91
92   @Test
93   public void testTopologyDG() throws APPCException, ClientProtocolException, IOException {
94     assertTrue(ProviderOperations.topologyDG(message));
95   }
96
97   @Test
98   public void testTopologyDGWithBaseAuth()
99       throws APPCException, ClientProtocolException, IOException {
100     ProviderOperations.setAuthentication("user", "password");
101     ProviderOperations.setUrl("http://localhost:8080");
102     assertTrue(ProviderOperations.topologyDG(message));
103   }
104
105   @Test(expected = APPCException.class)
106   public void testTopologyDGFail() throws APPCException, ClientProtocolException, IOException {
107     reponseMessage =
108         "{\"output\":{\"common-response-header\":{\"success\":false,\"reason\":\"\"}}}";
109     inputStream = new ByteArrayInputStream(reponseMessage.getBytes(StandardCharsets.UTF_8));
110     when(httpEntity.getContent()).thenReturn(inputStream);
111     ProviderOperations.topologyDG(message);
112   }
113
114   @Test(expected = APPCException.class)
115   public void testTopologyDGInvalidResponse()
116       throws APPCException, ClientProtocolException, IOException {
117     reponseMessage = "{\"output\":{\"common-response-header\":{\"succss\":false,\"reason\":\"\"}}}";
118     inputStream = new ByteArrayInputStream(reponseMessage.getBytes(StandardCharsets.UTF_8));
119     when(httpEntity.getContent()).thenReturn(inputStream);
120     ProviderOperations.topologyDG(message);
121   }
122
123   @Test(expected = APPCException.class)
124   public void testTopologyDGWithInvalidHttp()
125       throws APPCException, ClientProtocolException, IOException {
126     when(statusLine.getStatusCode()).thenReturn(500);
127     ProviderOperations.topologyDG(message);
128   }
129 }