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.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;
56 * @author Waqas Ikram (waqas.ikram@est.tech)
59 @RunWith(SpringJUnit4ClassRunner.class)
60 @ActiveProfiles("test")
61 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
63 public class OperationsControllerTest {
65 private static final String SVC_REQUEST_ID = "04fc9f50-87b8-430d-a232-ef24bd6c4150";
67 private static final String SERVICE_INSTANCE_ID = "ccece8fe-13da-456a-baf6-41b3a4a2bc2b";
69 private static final String SERVICE_TOPOLOGY_OPERATION_URL = "/GENERIC-RESOURCE-API:service-topology-operation/";
71 private static final String PASSWORD = "Kp8bJ4SXszM0WXlhak3eHlcse2gAw84vaoGGmJvUy2U";
77 private TestRestTemplate restTemplate;
80 private ServiceOperationsCacheServiceProvider cacheServiceProvider;
83 private UserCredentials userCredentials;
87 public void test_postServiceOperationInformation_successfullyAddedToCache() throws Exception {
89 final HttpEntity<?> httpEntity = new HttpEntity<>(getRequestInput(), getHttpHeaders());
90 final ResponseEntity<OutputRequest> responseEntity =
91 restTemplate.exchange(getUrl(), HttpMethod.POST, httpEntity, OutputRequest.class);
93 assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
94 assertTrue(responseEntity.hasBody());
95 final OutputRequest actualObject = responseEntity.getBody();
96 assertNotNull(actualObject);
98 assertEquals(HttpStatus.OK.toString(), actualObject.getResponseCode());
99 assertEquals(Constants.YES, actualObject.getAckFinalIndicator());
100 assertEquals(SVC_REQUEST_ID, actualObject.getSvcRequestId());
101 assertNotNull(actualObject.getServiceResponseInformation());
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());
107 cacheServiceProvider.getGenericResourceApiServiceModelInfrastructure(SERVICE_INSTANCE_ID).isPresent());
111 public void test_postServiceOperationInformation_NullInputRequest_badRequest() throws Exception {
113 final HttpEntity<?> httpEntity = new HttpEntity<>(new InputRequest<>(), getHttpHeaders());
114 final ResponseEntity<OutputRequest> responseEntity =
115 restTemplate.exchange(getUrl(), HttpMethod.POST, httpEntity, OutputRequest.class);
117 assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
121 public void test_postServiceOperationInformation_NullServiceInstanceId_badRequest() throws Exception {
123 final HttpEntity<?> httpEntity = new HttpEntity<>(getInvalidRequestInput(), getHttpHeaders());
124 final ResponseEntity<OutputRequest> responseEntity =
125 restTemplate.exchange(getUrl(), HttpMethod.POST, httpEntity, OutputRequest.class);
127 assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
128 assertTrue(responseEntity.hasBody());
129 final OutputRequest actualObject = responseEntity.getBody();
131 assertEquals(HttpStatus.BAD_REQUEST.toString(), actualObject.getResponseCode());
132 assertEquals(SVC_REQUEST_ID, actualObject.getSvcRequestId());
133 assertEquals(Constants.YES, actualObject.getAckFinalIndicator());
137 private HttpHeaders getHttpHeaders() {
138 return getHttpHeaders(userCredentials.getUsers().iterator().next().getUsername());
142 private String getUrl() {
143 return "http://localhost:" + port + Constants.OPERATIONS_URL + SERVICE_TOPOLOGY_OPERATION_URL;
146 private String getRequestInput() throws IOException {
147 return getFileAsString(getFile("test-data/input.json").toPath());
150 private String getInvalidRequestInput() throws IOException {
151 return getFileAsString(getFile("test-data/InvalidInput.json").toPath());
154 private String getFileAsString(final Path path) throws IOException {
155 return new String(Files.readAllBytes(path));
158 private File getFile(final String file) throws IOException {
159 return new ClassPathResource(file).getFile();
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;
169 private String getBasicAuth(final String username) {
170 return "Basic " + new String(Base64.getEncoder().encodeToString((username + ":" + PASSWORD).getBytes()));
174 public void after() {
175 cacheServiceProvider.clearAll();