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