af46ab40d0e5dbe9a3b4d3bd97607e2c46f50d13
[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.Output;
36 import org.onap.so.sdncsimulator.models.OutputRequest;
37 import org.onap.so.sdncsimulator.providers.ServiceOperationsCacheServiceProvider;
38 import org.onap.so.sdncsimulator.utils.Constants;
39 import org.onap.so.simulator.model.UserCredentials;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.boot.test.context.SpringBootTest;
42 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
43 import org.springframework.boot.test.web.client.TestRestTemplate;
44 import org.springframework.boot.web.server.LocalServerPort;
45 import org.springframework.context.annotation.Configuration;
46 import org.springframework.core.io.ClassPathResource;
47 import org.springframework.http.HttpEntity;
48 import org.springframework.http.HttpHeaders;
49 import org.springframework.http.HttpMethod;
50 import org.springframework.http.HttpStatus;
51 import org.springframework.http.MediaType;
52 import org.springframework.http.ResponseEntity;
53 import org.springframework.test.context.ActiveProfiles;
54 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
55
56 /**
57  * @author Waqas Ikram (waqas.ikram@est.tech)
58  *
59  */
60 @RunWith(SpringJUnit4ClassRunner.class)
61 @ActiveProfiles("test")
62 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
63 @Configuration
64 public class OperationsControllerTest {
65
66     private static final String SVC_REQUEST_ID = "04fc9f50-87b8-430d-a232-ef24bd6c4150";
67
68     private static final String SERVICE_INSTANCE_ID = "ccece8fe-13da-456a-baf6-41b3a4a2bc2b";
69
70     private static final String SERVICE_TOPOLOGY_OPERATION_URL = "/GENERIC-RESOURCE-API:service-topology-operation/";
71
72     private static final String PASSWORD = "Kp8bJ4SXszM0WXlhak3eHlcse2gAw84vaoGGmJvUy2U";
73
74     @LocalServerPort
75     private int port;
76
77     @Autowired
78     private TestRestTemplate restTemplate;
79
80     @Autowired
81     private ServiceOperationsCacheServiceProvider cacheServiceProvider;
82
83     @Autowired
84     private UserCredentials userCredentials;
85
86
87     @Test
88     public void test_postServiceOperationInformation_successfullyAddedToCache() throws Exception {
89
90         final HttpEntity<?> httpEntity = new HttpEntity<>(getRequestInput(), getHttpHeaders());
91         final ResponseEntity<OutputRequest> responseEntity =
92                 restTemplate.exchange(getUrl(), HttpMethod.POST, httpEntity, OutputRequest.class);
93
94         assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
95         assertTrue(responseEntity.hasBody());
96
97         final OutputRequest actualOutputRequest = responseEntity.getBody();
98         assertNotNull(actualOutputRequest);
99
100         final Output actualObject = actualOutputRequest.getOutput();
101
102         assertNotNull(actualObject);
103         assertEquals(HttpStatus.OK.toString(), actualObject.getResponseCode());
104         assertEquals(Constants.YES, actualObject.getAckFinalIndicator());
105         assertEquals(SVC_REQUEST_ID, actualObject.getSvcRequestId());
106         assertNotNull(actualObject.getServiceResponseInformation());
107
108         final GenericResourceApiInstanceReference acutalReference = actualObject.getServiceResponseInformation();
109         assertEquals(Constants.RESTCONF_CONFIG_END_POINT + SERVICE_INSTANCE_ID, acutalReference.getObjectPath());
110         assertEquals(SERVICE_INSTANCE_ID, acutalReference.getInstanceId());
111         assertTrue(
112                 cacheServiceProvider.getGenericResourceApiServiceModelInfrastructure(SERVICE_INSTANCE_ID).isPresent());
113     }
114
115     @Test
116     public void test_postServiceOperationInformation_NullInputRequest_badRequest() throws Exception {
117
118         final HttpEntity<?> httpEntity = new HttpEntity<>(new InputRequest<>(), getHttpHeaders());
119         final ResponseEntity<OutputRequest> responseEntity =
120                 restTemplate.exchange(getUrl(), HttpMethod.POST, httpEntity, OutputRequest.class);
121
122         assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
123     }
124
125     @Test
126     public void test_postServiceOperationInformation_NullServiceInstanceId_badRequest() throws Exception {
127
128         final HttpEntity<?> httpEntity = new HttpEntity<>(getInvalidRequestInput(), getHttpHeaders());
129         final ResponseEntity<OutputRequest> responseEntity =
130                 restTemplate.exchange(getUrl(), HttpMethod.POST, httpEntity, OutputRequest.class);
131
132         assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
133         assertTrue(responseEntity.hasBody());
134
135         final OutputRequest actualOutputRequest = responseEntity.getBody();
136         assertNotNull(actualOutputRequest);
137
138         final Output actualObject = actualOutputRequest.getOutput();
139         assertNotNull(actualObject);
140         assertEquals(HttpStatus.BAD_REQUEST.toString(), actualObject.getResponseCode());
141         assertEquals(SVC_REQUEST_ID, actualObject.getSvcRequestId());
142         assertEquals(Constants.YES, actualObject.getAckFinalIndicator());
143
144     }
145
146     private HttpHeaders getHttpHeaders() {
147         return getHttpHeaders(userCredentials.getUsers().iterator().next().getUsername());
148     }
149
150     private String getUrl() {
151         return "http://localhost:" + port + Constants.OPERATIONS_URL + SERVICE_TOPOLOGY_OPERATION_URL;
152     }
153
154     private String getRequestInput() throws IOException {
155         return getFileAsString(getFile("test-data/input.json").toPath());
156     }
157
158     private String getInvalidRequestInput() throws IOException {
159         return getFileAsString(getFile("test-data/InvalidInput.json").toPath());
160     }
161
162     private String getFileAsString(final Path path) throws IOException {
163         return new String(Files.readAllBytes(path));
164     }
165
166     private File getFile(final String file) throws IOException {
167         return new ClassPathResource(file).getFile();
168     }
169
170     private HttpHeaders getHttpHeaders(final String username) {
171         final HttpHeaders requestHeaders = new HttpHeaders();
172         requestHeaders.add("Authorization", getBasicAuth(username));
173         requestHeaders.setContentType(MediaType.APPLICATION_JSON);
174         return requestHeaders;
175     }
176
177     private String getBasicAuth(final String username) {
178         return "Basic " + new String(Base64.getEncoder().encodeToString((username + ":" + PASSWORD).getBytes()));
179     }
180
181     @After
182     public void after() {
183         cacheServiceProvider.clearAll();
184     }
185
186 }