VID-266 added stub server for testing
[vid.git] / vid-app-common / src / test / java / org / onap / vid / scheduler / SchedulerRestInterfaceTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21
22 package org.onap.vid.scheduler;
23
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;
29 import org.mockito.*;
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;
35
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;
45
46 import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
47
48
49 @RunWith(MockitoJUnitRunner.class)
50 public class SchedulerRestInterfaceTest {
51
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;
57     @Mock
58     private HttpsAuthClient mockedHttpsAuthClient;
59     @Mock
60     private Client mockedClient;
61     @Mock
62     private Invocation.Builder mockedBuilder;
63     @Mock
64     private Response mockedResponse;
65     @Mock
66     private WebTarget mockedWebTarget;
67
68     @Mock
69     private Function<String, String> propertyGetter;
70
71     @InjectMocks
72     private SchedulerRestInterface schedulerInterface = new SchedulerRestInterface();
73
74     @BeforeClass
75     public static void setUpClass() {
76         serverUtil = new StubServerUtil();
77         serverUtil.runServer();
78         commonHeaders.put("Authorization", Collections.singletonList("Basic " + USR_PWD_AUTH_STRING));
79     }
80
81     @AfterClass
82     public static void tearDownClass() {
83         serverUtil.stopServer();
84     }
85
86     @BeforeMethod
87     public void setUp() {
88         MockitoAnnotations.initMocks(this);
89
90         sampleBaseUrl = serverUtil.constructTargetUrl("http", "");
91     }
92
93     @Test
94     public void testShouldGetOKWhenStringIsExpected() throws IOException, GeneralSecurityException {
95         String sampleSourceId = "AAI";
96         RestObject<String> sampleRestObj = new RestObject<>();
97         String resultHolder = "";
98
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");
112
113         serverUtil.prepareGetCall("/test", responseContent, Action.ok());
114
115         schedulerInterface.Get(resultHolder, sampleSourceId, "test", sampleRestObj);
116
117         assertResponseData(sampleRestObj, responseContent, 200);
118     }
119
120
121     @Test
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 = "";
127
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");
141
142         serverUtil.prepareDeleteCall("/test", responseContent, Action.ok());
143
144         schedulerInterface.Delete(resultHolder, sampleSourceId, "test", sampleRestObj);
145
146         assertResponseData(sampleRestObj, responseContent, 200);
147     }
148
149
150     private void assertResponseData(RestObject<String> sampleRestObj, String expectedResponse, int expectedStatusCode) {
151
152         assertThat(sampleRestObj.getStatusCode()).isEqualTo(expectedStatusCode);
153         assertThat(sampleRestObj.get()).isInstanceOf(String.class).isEqualTo(expectedResponse);
154         assertThat(sampleRestObj.getUUID()).isNull();
155
156     }
157
158 }