2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 Nokia Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
22 package org.onap.vid.scheduler;
24 import com.xebialabs.restito.semantics.Action;
25 import org.junit.AfterClass;
26 import org.junit.BeforeClass;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
30 import org.mockito.runners.MockitoJUnitRunner;
31 import org.onap.vid.aai.util.HttpClientMode;
32 import org.onap.vid.aai.util.HttpsAuthClient;
33 import org.onap.vid.testUtils.StubServerUtil;
34 import org.testng.annotations.BeforeMethod;
36 import javax.ws.rs.client.Client;
37 import javax.ws.rs.client.Invocation;
38 import javax.ws.rs.client.WebTarget;
39 import javax.ws.rs.core.MultivaluedHashMap;
40 import javax.ws.rs.core.Response;
41 import java.io.IOException;
42 import java.security.GeneralSecurityException;
43 import java.util.Collections;
44 import java.util.function.Function;
46 import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
49 @RunWith(MockitoJUnitRunner.class)
50 public class SchedulerRestInterfaceTest {
52 private static final String USR_PWD_AUTH_STRING = "c2FtcGxlOnBhUyR3MFJk";
53 private static final String APPLICATION_JSON = "application/json";
54 private static MultivaluedHashMap<String, Object> commonHeaders = new MultivaluedHashMap<>();
55 private static StubServerUtil serverUtil;
56 private String sampleBaseUrl;
58 private HttpsAuthClient mockedHttpsAuthClient;
60 private Client mockedClient;
62 private Invocation.Builder mockedBuilder;
64 private Response mockedResponse;
66 private WebTarget mockedWebTarget;
69 private Function<String, String> propertyGetter;
72 private SchedulerRestInterface schedulerInterface = new SchedulerRestInterface();
75 public static void setUpClass() {
76 serverUtil = new StubServerUtil();
77 serverUtil.runServer();
78 commonHeaders.put("Authorization", Collections.singletonList("Basic " + USR_PWD_AUTH_STRING));
82 public static void tearDownClass() {
83 serverUtil.stopServer();
88 MockitoAnnotations.initMocks(this);
90 sampleBaseUrl = serverUtil.constructTargetUrl("http", "");
94 public void testShouldGetOKWhenStringIsExpected() throws IOException, GeneralSecurityException {
95 String sampleSourceId = "AAI";
96 RestObject<String> sampleRestObj = new RestObject<>();
97 String resultHolder = "";
99 String responseContent = "sample : SAMPLE RESULT STRING";
100 Mockito.doReturn(mockedClient).when(mockedHttpsAuthClient).getClient(HttpClientMode.WITHOUT_KEYSTORE);
101 Mockito.doReturn("sample").when(propertyGetter).apply(SchedulerProperties.SCHEDULER_USER_NAME_VAL);
102 Mockito.doReturn("paS$w0Rd").when(propertyGetter).apply(SchedulerProperties.SCHEDULER_PASSWORD_VAL);
103 Mockito.doReturn(sampleBaseUrl).when(propertyGetter).apply(SchedulerProperties.SCHEDULER_SERVER_URL_VAL);
104 Mockito.doReturn(200).when(mockedResponse).getStatus();
105 Mockito.doReturn(responseContent).when(mockedResponse).readEntity(String.class);
106 Mockito.doReturn(mockedResponse).when(mockedBuilder).get();
107 Mockito.when(mockedBuilder.header(Matchers.any(), Matchers.any())).thenReturn(mockedBuilder);
108 Mockito.doReturn(mockedBuilder).when(mockedBuilder).headers(commonHeaders);
109 Mockito.doReturn(mockedBuilder).when(mockedBuilder).accept(APPLICATION_JSON);
110 Mockito.doReturn(mockedBuilder).when(mockedWebTarget).request();
111 Mockito.doReturn(mockedWebTarget).when(mockedClient).target(sampleBaseUrl + "test");
113 serverUtil.prepareGetCall("/test", responseContent, Action.ok());
115 schedulerInterface.Get(resultHolder, sampleSourceId, "test", sampleRestObj);
117 assertResponseData(sampleRestObj, responseContent, 200);
122 public void testShouldDeleteSuccessfully() throws IOException, GeneralSecurityException {
123 String sampleTargetUrl = serverUtil.constructTargetUrl("http", "");
124 String sampleSourceId = "AAI";
125 RestObject<String> sampleRestObj = new RestObject<>();
126 String resultHolder = "";
128 String responseContent = "sample : SAMPLE RESULT STRING";
129 Mockito.doReturn(mockedClient).when(mockedHttpsAuthClient).getClient(HttpClientMode.WITHOUT_KEYSTORE);
130 Mockito.doReturn("sample").when(propertyGetter).apply(SchedulerProperties.SCHEDULER_USER_NAME_VAL);
131 Mockito.doReturn("paS$w0Rd").when(propertyGetter).apply(SchedulerProperties.SCHEDULER_PASSWORD_VAL);
132 Mockito.doReturn(sampleTargetUrl).when(propertyGetter).apply(SchedulerProperties.SCHEDULER_SERVER_URL_VAL);
133 Mockito.doReturn(200).when(mockedResponse).getStatus();
134 Mockito.doReturn(responseContent).when(mockedResponse).readEntity(String.class);
135 Mockito.doReturn(mockedResponse).when(mockedBuilder).delete();
136 Mockito.when(mockedBuilder.header(Matchers.any(), Matchers.any())).thenReturn(mockedBuilder);
137 Mockito.doReturn(mockedBuilder).when(mockedBuilder).headers(commonHeaders);
138 Mockito.doReturn(mockedBuilder).when(mockedBuilder).accept(APPLICATION_JSON);
139 Mockito.doReturn(mockedBuilder).when(mockedWebTarget).request();
140 Mockito.doReturn(mockedWebTarget).when(mockedClient).target(sampleTargetUrl + "test");
142 serverUtil.prepareDeleteCall("/test", responseContent, Action.ok());
144 schedulerInterface.Delete(resultHolder, sampleSourceId, "test", sampleRestObj);
146 assertResponseData(sampleRestObj, responseContent, 200);
150 private void assertResponseData(RestObject<String> sampleRestObj, String expectedResponse, int expectedStatusCode) {
152 assertThat(sampleRestObj.getStatusCode()).isEqualTo(expectedStatusCode);
153 assertThat(sampleRestObj.get()).isInstanceOf(String.class).isEqualTo(expectedResponse);
154 assertThat(sampleRestObj.getUUID()).isNull();