bcfff95e18bfacc67160df17d464aabd7459bab1
[integration/csit.git] / plans / so / integration-etsi-testing / so-simulators / sdnc-simulator / src / test / java / org / onap / so / sdncsimulator / controller / OperationsControllerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.so.sdncsimulator.controller;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertTrue;
25 import java.io.File;
26 import java.io.IOException;
27 import java.nio.file.Files;
28 import java.nio.file.Path;
29 import java.util.Base64;
30 import org.junit.After;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.onap.sdnc.northbound.client.model.GenericResourceApiInstanceReference;
34 import org.onap.so.sdncsimulator.models.InputRequest;
35 import org.onap.so.sdncsimulator.models.OutputRequest;
36 import org.onap.so.sdncsimulator.providers.ServiceOperationsCacheServiceProvider;
37 import org.onap.so.sdncsimulator.utils.Constants;
38 import org.onap.so.simulator.model.UserCredentials;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.boot.test.context.SpringBootTest;
41 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
42 import org.springframework.boot.test.web.client.TestRestTemplate;
43 import org.springframework.boot.web.server.LocalServerPort;
44 import org.springframework.context.annotation.Configuration;
45 import org.springframework.core.io.ClassPathResource;
46 import org.springframework.http.HttpEntity;
47 import org.springframework.http.HttpHeaders;
48 import org.springframework.http.HttpMethod;
49 import org.springframework.http.HttpStatus;
50 import org.springframework.http.MediaType;
51 import org.springframework.http.ResponseEntity;
52 import org.springframework.test.context.ActiveProfiles;
53 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
54
55 /**
56  * @author Waqas Ikram (waqas.ikram@est.tech)
57  *
58  */
59 @RunWith(SpringJUnit4ClassRunner.class)
60 @ActiveProfiles("test")
61 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
62 @Configuration
63 public class OperationsControllerTest {
64
65     private static final String SVC_REQUEST_ID = "04fc9f50-87b8-430d-a232-ef24bd6c4150";
66
67     private static final String SERVICE_INSTANCE_ID = "ccece8fe-13da-456a-baf6-41b3a4a2bc2b";
68
69     private static final String SERVICE_TOPOLOGY_OPERATION_URL = "/GENERIC-RESOURCE-API:service-topology-operation/";
70
71     private static final String PASSWORD = "Kp8bJ4SXszM0WXlhak3eHlcse2gAw84vaoGGmJvUy2U";
72
73     @LocalServerPort
74     private int port;
75
76     @Autowired
77     private TestRestTemplate restTemplate;
78
79     @Autowired
80     private ServiceOperationsCacheServiceProvider cacheServiceProvider;
81
82     @Autowired
83     private UserCredentials userCredentials;
84
85
86     @Test
87     public void test_postServiceOperationInformation_successfullyAddedToCache() throws Exception {
88
89         final HttpEntity<?> httpEntity = new HttpEntity<>(getRequestInput(), getHttpHeaders());
90         final ResponseEntity<OutputRequest> responseEntity =
91                 restTemplate.exchange(getUrl(), HttpMethod.POST, httpEntity, OutputRequest.class);
92
93         assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
94         assertTrue(responseEntity.hasBody());
95         final OutputRequest actualObject = responseEntity.getBody();
96         assertNotNull(actualObject);
97
98         assertEquals(HttpStatus.OK.toString(), actualObject.getResponseCode());
99         assertEquals(Constants.YES, actualObject.getAckFinalIndicator());
100         assertEquals(SVC_REQUEST_ID, actualObject.getSvcRequestId());
101         assertNotNull(actualObject.getServiceResponseInformation());
102
103         final GenericResourceApiInstanceReference acutalReference = actualObject.getServiceResponseInformation();
104         assertEquals(Constants.RESTCONF_CONFIG_END_POINT + SERVICE_INSTANCE_ID, acutalReference.getObjectPath());
105         assertEquals(SERVICE_INSTANCE_ID, acutalReference.getInstanceId());
106         assertTrue(
107                 cacheServiceProvider.getGenericResourceApiServiceModelInfrastructure(SERVICE_INSTANCE_ID).isPresent());
108     }
109
110     @Test
111     public void test_postServiceOperationInformation_NullInputRequest_badRequest() throws Exception {
112
113         final HttpEntity<?> httpEntity = new HttpEntity<>(new InputRequest<>(), getHttpHeaders());
114         final ResponseEntity<OutputRequest> responseEntity =
115                 restTemplate.exchange(getUrl(), HttpMethod.POST, httpEntity, OutputRequest.class);
116
117         assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
118     }
119
120     @Test
121     public void test_postServiceOperationInformation_NullServiceInstanceId_badRequest() throws Exception {
122
123         final HttpEntity<?> httpEntity = new HttpEntity<>(getInvalidRequestInput(), getHttpHeaders());
124         final ResponseEntity<OutputRequest> responseEntity =
125                 restTemplate.exchange(getUrl(), HttpMethod.POST, httpEntity, OutputRequest.class);
126
127         assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
128         assertTrue(responseEntity.hasBody());
129         final OutputRequest actualObject = responseEntity.getBody();
130
131         assertEquals(HttpStatus.BAD_REQUEST.toString(), actualObject.getResponseCode());
132         assertEquals(SVC_REQUEST_ID, actualObject.getSvcRequestId());
133         assertEquals(Constants.YES, actualObject.getAckFinalIndicator());
134
135     }
136
137     private HttpHeaders getHttpHeaders() {
138         return getHttpHeaders(userCredentials.getUsers().iterator().next().getUsername());
139     }
140
141
142     private String getUrl() {
143         return "http://localhost:" + port + Constants.OPERATIONS_URL + SERVICE_TOPOLOGY_OPERATION_URL;
144     }
145
146     private String getRequestInput() throws IOException {
147         return getFileAsString(getFile("test-data/input.json").toPath());
148     }
149
150     private String getInvalidRequestInput() throws IOException {
151         return getFileAsString(getFile("test-data/InvalidInput.json").toPath());
152     }
153
154     private String getFileAsString(final Path path) throws IOException {
155         return new String(Files.readAllBytes(path));
156     }
157
158     private File getFile(final String file) throws IOException {
159         return new ClassPathResource(file).getFile();
160     }
161
162     private HttpHeaders getHttpHeaders(final String username) {
163         final HttpHeaders requestHeaders = new HttpHeaders();
164         requestHeaders.add("Authorization", getBasicAuth(username));
165         requestHeaders.setContentType(MediaType.APPLICATION_JSON);
166         return requestHeaders;
167     }
168
169     private String getBasicAuth(final String username) {
170         return "Basic " + new String(Base64.getEncoder().encodeToString((username + ":" + PASSWORD).getBytes()));
171     }
172
173     @After
174     public void after() {
175         cacheServiceProvider.clearAll();
176     }
177
178 }