0655aca9e854a0a197f3bea873526155eb6705e1
[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.fasterxml.jackson.core.JsonProcessingException;
25 import com.xebialabs.restito.semantics.Action;
26 import org.glassfish.grizzly.http.util.HttpStatus;
27 import org.json.simple.parser.JSONParser;
28 import org.json.simple.parser.ParseException;
29 import org.junit.AfterClass;
30 import org.junit.BeforeClass;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.junit.MockitoJUnitRunner;
34 import org.onap.vid.exceptions.GenericUncheckedException;
35 import org.onap.vid.testUtils.StubServerUtil;
36 import org.testng.annotations.AfterMethod;
37
38 import java.util.HashMap;
39 import java.util.Map;
40
41 import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
42
43
44 @RunWith(MockitoJUnitRunner.class)
45 public class SchedulerRestInterfaceTest {
46
47     private static final String SAMPLE_USERNAME = "sample";
48     private static final String SAMPLE_PASSWORD = "paS$w0Rd";
49     private static final String SAMPLE_SCHEDULER_SERVER_URL = "http://localhost";
50     private static final String SAMPLE_SOURCE_ID = "AAI";
51     private static final JSONParser JSON_PARSER = new JSONParser();
52     private static final String RESPONSE_CONTENT = "\"schedules\": \"SAMPLE STRING\"";
53     private static final String ERROR_RESPONSE = "\"error\": \"Internal server error!\"";
54     private static Map<String, String> DUMMY_SYSTEM_PROPERTIES = new HashMap<String, String>() {{
55         put(SchedulerProperties.SCHEDULER_USER_NAME_VAL, SAMPLE_USERNAME);
56         put(SchedulerProperties.SCHEDULER_PASSWORD_VAL, SAMPLE_PASSWORD);
57         put(SchedulerProperties.SCHEDULER_SERVER_URL_VAL, SAMPLE_SCHEDULER_SERVER_URL);
58     }};
59     private static StubServerUtil serverUtil;
60     private static SchedulerRestInterface schedulerInterface = new SchedulerRestInterface((key) -> DUMMY_SYSTEM_PROPERTIES.get(key));
61
62     @BeforeClass
63     public static void setUpClass() {
64         serverUtil = new StubServerUtil();
65         serverUtil.runServer();
66     }
67
68     @AfterClass
69     public static void tearDownClass() {
70         serverUtil.stopServer();
71     }
72
73
74     @AfterMethod
75     public void tearDown() {
76         serverUtil.stopServer();
77     }
78
79     @Test
80     public void testShouldGetOKWhenStringIsExpected() throws JsonProcessingException, ParseException {
81         prepareEnvForTest();
82         RestObject<String> sampleRestObj = new RestObject<>();
83         serverUtil.prepareGetCall("/test", RESPONSE_CONTENT, Action.ok());
84
85         schedulerInterface.Get("", SAMPLE_SOURCE_ID, "", sampleRestObj);
86
87         assertResponseHasExpectedBodyAndStatus(sampleRestObj, RESPONSE_CONTENT, 200);
88     }
89
90     @Test(expected = GenericUncheckedException.class)
91     public void shouldRaiseExceptionWhenErrorOccursDuringGet() throws JsonProcessingException {
92         prepareEnvForTest();
93         RestObject<String> sampleRestObj = new RestObject<>();
94
95         serverUtil.prepareGetCall("/test", ERROR_RESPONSE, Action.status(HttpStatus.INTERNAL_SERVER_ERROR_500));
96
97         schedulerInterface.Get("", SAMPLE_SOURCE_ID, "", sampleRestObj);
98     }
99
100     @Test
101     public void shouldDeleteResourceSuccessfully() throws JsonProcessingException, ParseException {
102         prepareEnvForTest();
103         RestObject<String> sampleRestObj = new RestObject<>();
104         serverUtil.prepareDeleteCall("/test", RESPONSE_CONTENT, Action.ok());
105
106         schedulerInterface.Delete("", SAMPLE_SOURCE_ID, "", sampleRestObj);
107
108         assertResponseHasExpectedBodyAndStatus(sampleRestObj, RESPONSE_CONTENT, 200);
109     }
110
111     @Test
112     public void shouldRaiseExceptionWhenErrorOccursDuringDelete() throws JsonProcessingException, ParseException {
113         prepareEnvForTest();
114         RestObject<String> sampleRestObj = new RestObject<>();
115         serverUtil.prepareDeleteCall("/test", ERROR_RESPONSE, Action.status(HttpStatus.INTERNAL_SERVER_ERROR_500));
116
117         schedulerInterface.Delete("", SAMPLE_SOURCE_ID, "", sampleRestObj);
118
119         assertResponseHasExpectedBodyAndStatus(sampleRestObj, ERROR_RESPONSE, 500);
120     }
121
122
123     private void assertResponseHasExpectedBodyAndStatus(RestObject<String> sampleRestObj, String expectedResponse, int expectedStatusCode) throws ParseException {
124         Object parsedResult = JSON_PARSER.parse(sampleRestObj.get());
125
126         assertThat(sampleRestObj.getStatusCode()).isEqualTo(expectedStatusCode);
127         assertThat(parsedResult).isInstanceOf(String.class).isEqualTo(expectedResponse);
128         assertThat(sampleRestObj.getUUID()).isNull();
129
130     }
131
132     private void prepareEnvForTest() {
133         String targetUrl = serverUtil.constructTargetUrl("http", "test");
134         DUMMY_SYSTEM_PROPERTIES.put(SchedulerProperties.SCHEDULER_SERVER_URL_VAL, targetUrl);
135     }
136 }