Moving Historical Queries to spring boot
[aai/data-router.git] / src / test / java / org / onap / aai / datarouter / query / ChampRouterTest.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 @RunWith(PowerMockRunner.class)
45 @PrepareForTest({ ChampRouter.class, RestClient.class })
46 public class ChampRouterTest {
47
48   ChampRouter champRouter;
49   RestClient mockRestClient = mock(RestClient.class);
50
51   @SuppressWarnings("unchecked")
52   @Before
53   public void init() throws Exception {
54     RestClientConfig config = PowerMockito.mock(RestClientConfig.class);
55     PowerMockito.when(config.getCertPassword()).thenReturn("password");
56
57     PowerMockito.whenNew(RestClient.class).withAnyArguments().thenReturn(mockRestClient);
58     PowerMockito.when(mockRestClient.validateServerHostname(any(Boolean.class))).thenReturn(mockRestClient);
59     PowerMockito.when(mockRestClient.validateServerCertChain(any(Boolean.class))).thenReturn(mockRestClient);
60     PowerMockito.when(mockRestClient.clientCertFile(any(String.class))).thenReturn(mockRestClient);
61     PowerMockito.when(mockRestClient.clientCertPassword(any(String.class))).thenReturn(mockRestClient);
62     PowerMockito.when(mockRestClient.trustStore(any(String.class))).thenReturn(mockRestClient);
63     PowerMockito.when(mockRestClient.connectTimeoutMs(any(Integer.class))).thenReturn(mockRestClient);
64     PowerMockito.when(mockRestClient.readTimeoutMs(any(Integer.class))).thenReturn(mockRestClient);
65
66     champRouter = new ChampRouter("http:///test", config);
67   }
68
69   @Test
70   public void testProcess() throws Exception {
71     OperationResult champResponse = buildChampResponse();
72     PowerMockito.when(mockRestClient.get(any(String.class), any(HashMap.class), any(MediaType.class)))
73         .thenReturn(champResponse);
74
75     String champRouterResponse = champRouter.process("/object/1234", "", null);
76     Assert.assertEquals(champRouterResponse, readSampleChampResponse());
77
78   }
79
80   private OperationResult buildChampResponse() throws IOException {
81     OperationResult response = new OperationResult();
82     response.setResultCode(200);
83
84     response.setResult(readSampleChampResponse());
85     return response;
86   }
87
88   private String readSampleChampResponse() throws IOException {
89     FileInputStream event = new FileInputStream(new File("src/test/resources/champ-response.json"));
90     String json = IOUtils.toString(event, "UTF-8");
91     return json;
92   }
93
94 }