Changed to unmaintained
[appc.git] / appc-adapters / appc-rest-healthcheck-adapter / appc-rest-healthcheck-adapter-bundle / src / test / java / org / onap / appc / adapter / restHealthcheck / impl / TestRestHealthcheckAdapterImpl.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) 2017 Amdocs
8  * ================================================================================
9  * Modifications Copyright (C) 2019 Ericsson
10  * =============================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  * 
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  * 
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  * 
23  * ============LICENSE_END=========================================================
24  */
25 package org.onap.appc.adapter.restHealthcheck.impl;
26
27 import static org.junit.Assert.assertEquals;
28 import java.io.IOException;
29 import java.util.HashMap;
30 import java.util.Map;
31 import org.apache.http.HttpEntity;
32 import org.apache.http.HttpResponse;
33 import org.apache.http.ParseException;
34 import org.apache.http.StatusLine;
35 import org.apache.http.impl.client.CloseableHttpClient;
36 import org.apache.http.impl.client.HttpClients;
37 import org.apache.http.util.EntityUtils;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.mockito.Mockito;
42 import org.onap.appc.exceptions.APPCException;
43 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
44 import org.powermock.api.mockito.PowerMockito;
45 import org.powermock.core.classloader.annotations.PrepareForTest;
46 import org.powermock.modules.junit4.PowerMockRunner;
47 import com.att.cdp.exceptions.ZoneException;
48 import org.apache.http.client.methods.CloseableHttpResponse;
49 import org.apache.http.client.methods.HttpGet;
50
51
52
53 @RunWith(PowerMockRunner.class)
54 @PrepareForTest({HttpClients.class, EntityUtils.class})
55 public class TestRestHealthcheckAdapterImpl {
56
57     private RestHealthcheckAdapterImpl adapter;
58     private CloseableHttpClient client;
59     
60     @Before
61     public void setup() throws IllegalArgumentException, IllegalAccessException, ParseException, IOException {
62         PowerMockito.mockStatic(HttpClients.class);
63         PowerMockito.mockStatic(EntityUtils.class);
64         client = Mockito.mock(CloseableHttpClient.class);
65         PowerMockito.when(HttpClients.createDefault()).thenReturn(client);
66         PowerMockito.when(EntityUtils.toString(Mockito.any(HttpEntity.class))).thenReturn("TEST");
67         adapter = new RestHealthcheckAdapterImpl();
68     }
69
70     @Test
71     public void testCheckHealth() throws IOException, IllegalStateException, IllegalArgumentException,
72         ZoneException, APPCException {
73             Map<String, String> params = new HashMap<>();
74             params.put("VNF.URI", "http://restHalthCheck.test");
75             params.put("VNF.endpoint", "health");
76             HttpResponse response = Mockito.mock(CloseableHttpResponse.class);
77             HttpEntity entity = Mockito.mock(HttpEntity.class);
78             Mockito.doReturn(entity).when(response).getEntity();
79             StatusLine statusLine = Mockito.mock(StatusLine.class);
80             Mockito.doReturn(200).when(statusLine).getStatusCode();
81             Mockito.doReturn(statusLine).when(response).getStatusLine();
82             Mockito.doReturn(response).when(client).execute(Mockito.any(HttpGet.class));
83             SvcLogicContext svcContext = new SvcLogicContext();
84             adapter.checkHealth(params, svcContext);
85             String statusCode = svcContext.getAttribute("healthcheck.result.code");
86             assertEquals("400", statusCode);
87     }
88
89     @Test
90     public void testCheckHealthFailure() throws IOException, IllegalStateException, IllegalArgumentException,
91         ZoneException, APPCException {
92             Map<String, String> params = new HashMap<>();
93             params.put("VNF.URI", "http://restHalthCheck.test");
94             params.put("VNF.endpoint", "health");
95             HttpResponse response = Mockito.mock(CloseableHttpResponse.class);
96             HttpEntity entity = Mockito.mock(HttpEntity.class);
97             Mockito.doReturn(entity).when(response).getEntity();
98             StatusLine statusLine = Mockito.mock(StatusLine.class);
99             Mockito.doReturn(400).when(statusLine).getStatusCode();
100             Mockito.doReturn(statusLine).when(response).getStatusLine();
101             Mockito.doReturn(response).when(client).execute(Mockito.any(HttpGet.class));
102             SvcLogicContext svcContext = new SvcLogicContext();
103             adapter.checkHealth(params, svcContext);
104             String statusCode = svcContext.getAttribute("healthcheck.result.code");
105             assertEquals("200", statusCode);
106     }
107
108     @Test
109     public void testCheckHealthException() throws IOException, IllegalStateException, IllegalArgumentException,
110         ZoneException, APPCException {
111             Map<String, String> params = new HashMap<>();
112             params.put("VNF.URI", "http://restHalthCheck.test");
113             params.put("VNF.endpoint", "health");
114             HttpResponse response = Mockito.mock(CloseableHttpResponse.class);
115             HttpEntity entity = Mockito.mock(HttpEntity.class);
116             Mockito.doReturn(entity).when(response).getEntity();
117             StatusLine statusLine = Mockito.mock(StatusLine.class);
118             Mockito.doReturn(400).when(statusLine).getStatusCode();
119             Mockito.doReturn(statusLine).when(response).getStatusLine();
120             Mockito.doThrow(new IOException()).when(client).execute(Mockito.any(HttpGet.class));
121             SvcLogicContext svcContext = new SvcLogicContext();
122             adapter.checkHealth(params, svcContext);
123             String statusCode = svcContext.getAttribute("healthcheck.result.code");
124             assertEquals(RestHealthcheckAdapterImpl.OUTCOME_FAILURE, svcContext.getStatus());
125     }
126
127
128
129 }