Moving Historical Queries to spring boot
[aai/data-router.git] / src / test / java / org / onap / aai / datarouter / query / ChameleonRouterTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.aai.datarouter.query;
22
23 import static org.mockito.Matchers.any;
24 import static org.mockito.Mockito.mock;
25
26 import java.io.File;
27 import java.io.FileInputStream;
28 import java.io.IOException;
29 import java.util.HashMap;
30
31 import javax.ws.rs.core.MediaType;
32
33 import org.apache.commons.io.IOUtils;
34 import org.junit.Assert;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.onap.aai.restclient.client.OperationResult;
39 import org.onap.aai.restclient.client.RestClient;
40 import org.powermock.api.mockito.PowerMockito;
41 import org.powermock.core.classloader.annotations.PrepareForTest;
42 import org.powermock.modules.junit4.PowerMockRunner;
43
44 import com.google.gson.JsonParser;
45
46 @RunWith(PowerMockRunner.class)
47 @PrepareForTest({ ChameleonRouter.class, RestClient.class })
48 public class ChameleonRouterTest {
49
50   ChameleonRouter chameleonRouter;
51   RestClient mockRestClient = mock(RestClient.class);
52
53   JsonParser parser = new JsonParser();
54
55   @SuppressWarnings("unchecked")
56   @Before
57   public void init() throws Exception {
58     RestClientConfig config = PowerMockito.mock(RestClientConfig.class);
59     PowerMockito.when(config.getCertPassword()).thenReturn("password");
60
61     PowerMockito.whenNew(RestClient.class).withAnyArguments().thenReturn(mockRestClient);
62     PowerMockito.when(mockRestClient.validateServerHostname(any(Boolean.class))).thenReturn(mockRestClient);
63     PowerMockito.when(mockRestClient.validateServerCertChain(any(Boolean.class))).thenReturn(mockRestClient);
64     PowerMockito.when(mockRestClient.clientCertFile(any(String.class))).thenReturn(mockRestClient);
65     PowerMockito.when(mockRestClient.clientCertPassword(any(String.class))).thenReturn(mockRestClient);
66     PowerMockito.when(mockRestClient.trustStore(any(String.class))).thenReturn(mockRestClient);
67     PowerMockito.when(mockRestClient.connectTimeoutMs(any(Integer.class))).thenReturn(mockRestClient);
68     PowerMockito.when(mockRestClient.readTimeoutMs(any(Integer.class))).thenReturn(mockRestClient);
69
70     chameleonRouter = new ChameleonRouter("http:///test", config);
71   }
72
73   @Test
74   public void testProcess() throws Exception {
75     OperationResult chameleonResponse = buildChameleonResponse();
76     PowerMockito.when(mockRestClient.get(any(String.class), any(HashMap.class), any(MediaType.class)))
77         .thenReturn(chameleonResponse);
78
79     String chameleonRouterResponse = chameleonRouter.process("/objects/364d646e-c947-4010-a66a-adf06aa306fb", "", null);
80     Assert.assertEquals(parser.parse(chameleonRouterResponse), parser.parse(readSampleChampResponse()));
81
82   }
83
84   private OperationResult buildChameleonResponse() throws IOException {
85     OperationResult response = new OperationResult();
86     response.setResultCode(200);
87
88     response.setResult(readSampleChameleonResponse());
89     return response;
90   }
91
92   private String readSampleChampResponse() throws IOException {
93     FileInputStream event = new FileInputStream(new File("src/test/resources/champ-response.json"));
94     String json = IOUtils.toString(event, "UTF-8");
95     return json;
96   }
97
98   private String readSampleChameleonResponse() throws IOException {
99     FileInputStream event = new FileInputStream(new File("src/test/resources/chameleon-response.json"));
100     String json = IOUtils.toString(event, "UTF-8");
101     return json;
102   }
103
104 }