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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
20 package org.onap.so.sdncsimulator.controller;
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertTrue;
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;
57 * @author Waqas Ikram (waqas.ikram@est.tech)
60 @RunWith(SpringJUnit4ClassRunner.class)
61 @ActiveProfiles("test")
62 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
64 public class OperationsControllerTest {
66 private static final String SVC_REQUEST_ID = "04fc9f50-87b8-430d-a232-ef24bd6c4150";
68 private static final String SERVICE_INSTANCE_ID = "ccece8fe-13da-456a-baf6-41b3a4a2bc2b";
70 private static final String SERVICE_TOPOLOGY_OPERATION_URL = "/GENERIC-RESOURCE-API:service-topology-operation/";
72 private static final String PASSWORD = "Kp8bJ4SXszM0WXlhak3eHlcse2gAw84vaoGGmJvUy2U";
78 private TestRestTemplate restTemplate;
81 private ServiceOperationsCacheServiceProvider cacheServiceProvider;
84 private UserCredentials userCredentials;
88 public void test_postServiceOperationInformation_successfullyAddedToCache() throws Exception {
90 final HttpEntity<?> httpEntity = new HttpEntity<>(getRequestInput(), getHttpHeaders());
91 final ResponseEntity<OutputRequest> responseEntity =
92 restTemplate.exchange(getUrl(), HttpMethod.POST, httpEntity, OutputRequest.class);
94 assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
95 assertTrue(responseEntity.hasBody());
97 final OutputRequest actualOutputRequest = responseEntity.getBody();
98 assertNotNull(actualOutputRequest);
100 final Output actualObject = actualOutputRequest.getOutput();
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());
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());
112 cacheServiceProvider.getGenericResourceApiServiceModelInfrastructure(SERVICE_INSTANCE_ID).isPresent());
116 public void test_postServiceOperationInformation_NullInputRequest_badRequest() throws Exception {
118 final HttpEntity<?> httpEntity = new HttpEntity<>(new InputRequest<>(), getHttpHeaders());
119 final ResponseEntity<OutputRequest> responseEntity =
120 restTemplate.exchange(getUrl(), HttpMethod.POST, httpEntity, OutputRequest.class);
122 assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
126 public void test_postServiceOperationInformation_NullServiceInstanceId_badRequest() throws Exception {
128 final HttpEntity<?> httpEntity = new HttpEntity<>(getInvalidRequestInput(), getHttpHeaders());
129 final ResponseEntity<OutputRequest> responseEntity =
130 restTemplate.exchange(getUrl(), HttpMethod.POST, httpEntity, OutputRequest.class);
132 assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
133 assertTrue(responseEntity.hasBody());
135 final OutputRequest actualOutputRequest = responseEntity.getBody();
136 assertNotNull(actualOutputRequest);
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());
146 private HttpHeaders getHttpHeaders() {
147 return getHttpHeaders(userCredentials.getUsers().iterator().next().getUsername());
150 private String getUrl() {
151 return "http://localhost:" + port + Constants.OPERATIONS_URL + SERVICE_TOPOLOGY_OPERATION_URL;
154 private String getRequestInput() throws IOException {
155 return getFileAsString(getFile("test-data/input.json").toPath());
158 private String getInvalidRequestInput() throws IOException {
159 return getFileAsString(getFile("test-data/InvalidInput.json").toPath());
162 private String getFileAsString(final Path path) throws IOException {
163 return new String(Files.readAllBytes(path));
166 private File getFile(final String file) throws IOException {
167 return new ClassPathResource(file).getFile();
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;
177 private String getBasicAuth(final String username) {
178 return "Basic " + new String(Base64.getEncoder().encodeToString((username + ":" + PASSWORD).getBytes()));
182 public void after() {
183 cacheServiceProvider.clearAll();