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 org.junit.After;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.onap.sdnc.northbound.client.model.GenericResourceApiInstanceReference;
33 import org.onap.so.sdncsimulator.models.InputRequest;
34 import org.onap.so.sdncsimulator.models.OutputRequest;
35 import org.onap.so.sdncsimulator.providers.ServiceOperationsCacheServiceProvider;
36 import org.onap.so.sdncsimulator.utils.Constants;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.boot.test.context.SpringBootTest;
39 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
40 import org.springframework.boot.test.web.client.TestRestTemplate;
41 import org.springframework.boot.web.server.LocalServerPort;
42 import org.springframework.context.annotation.Configuration;
43 import org.springframework.core.io.ClassPathResource;
44 import org.springframework.http.HttpEntity;
45 import org.springframework.http.HttpHeaders;
46 import org.springframework.http.HttpMethod;
47 import org.springframework.http.HttpStatus;
48 import org.springframework.http.MediaType;
49 import org.springframework.http.ResponseEntity;
50 import org.springframework.test.context.ActiveProfiles;
51 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
54 * @author Waqas Ikram (waqas.ikram@est.tech)
57 @RunWith(SpringJUnit4ClassRunner.class)
58 @ActiveProfiles("test")
59 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
61 public class OperationsControllerTest {
63 private static final String SVC_REQUEST_ID = "04fc9f50-87b8-430d-a232-ef24bd6c4150";
65 private static final String SERVICE_INSTANCE_ID = "ccece8fe-13da-456a-baf6-41b3a4a2bc2b";
67 private static final String SERVICE_TOPOLOGY_OPERATION_URL = "/GENERIC-RESOURCE-API:service-topology-operation/";
73 private TestRestTemplate restTemplate;
76 private ServiceOperationsCacheServiceProvider cacheServiceProvider;
79 public void test_postServiceOperationInformation_successfullyAddedToCache() throws Exception {
81 final HttpEntity<?> httpEntity = new HttpEntity<>(getRequestInput(), getHttpHeaders());
82 final ResponseEntity<OutputRequest> responseEntity =
83 restTemplate.exchange(getUrl(), HttpMethod.POST, httpEntity, OutputRequest.class);
85 assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
86 assertTrue(responseEntity.hasBody());
87 final OutputRequest actualObject = responseEntity.getBody();
88 assertNotNull(actualObject);
90 assertEquals(HttpStatus.OK.toString(), actualObject.getResponseCode());
91 assertEquals(Constants.YES, actualObject.getAckFinalIndicator());
92 assertEquals(SVC_REQUEST_ID, actualObject.getSvcRequestId());
93 assertNotNull(actualObject.getServiceResponseInformation());
95 final GenericResourceApiInstanceReference acutalReference = actualObject.getServiceResponseInformation();
96 assertEquals(Constants.RESTCONF_CONFIG_END_POINT + SERVICE_INSTANCE_ID, acutalReference.getObjectPath());
97 assertEquals(SERVICE_INSTANCE_ID, acutalReference.getInstanceId());
99 cacheServiceProvider.getGenericResourceApiServiceModelInfrastructure(SERVICE_INSTANCE_ID).isPresent());
103 public void test_postServiceOperationInformation_NullInputRequest_badRequest() throws Exception {
105 final HttpEntity<?> httpEntity = new HttpEntity<>(new InputRequest<>(), getHttpHeaders());
106 final ResponseEntity<OutputRequest> responseEntity =
107 restTemplate.exchange(getUrl(), HttpMethod.POST, httpEntity, OutputRequest.class);
109 assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
113 public void test_postServiceOperationInformation_NullServiceInstanceId_badRequest() throws Exception {
115 final HttpEntity<?> httpEntity = new HttpEntity<>(getInvalidRequestInput(), getHttpHeaders());
116 final ResponseEntity<OutputRequest> responseEntity =
117 restTemplate.exchange(getUrl(), HttpMethod.POST, httpEntity, OutputRequest.class);
119 assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
120 assertTrue(responseEntity.hasBody());
121 final OutputRequest actualObject = responseEntity.getBody();
123 assertEquals(HttpStatus.BAD_REQUEST.toString(), actualObject.getResponseCode());
124 assertEquals(SVC_REQUEST_ID, actualObject.getSvcRequestId());
125 assertEquals(Constants.YES, actualObject.getAckFinalIndicator());
129 private HttpHeaders getHttpHeaders() {
130 final HttpHeaders requestHeaders = new HttpHeaders();
131 requestHeaders.setContentType(MediaType.APPLICATION_JSON);
132 return requestHeaders;
136 private String getUrl() {
137 return "http://localhost:" + port + Constants.OPERATIONS_URL + SERVICE_TOPOLOGY_OPERATION_URL;
140 private String getRequestInput() throws IOException {
141 return getFileAsString(getFile("test-data/input.json").toPath());
144 private String getInvalidRequestInput() throws IOException {
145 return getFileAsString(getFile("test-data/InvalidInput.json").toPath());
148 private String getFileAsString(final Path path) throws IOException {
149 return new String(Files.readAllBytes(path));
152 private static File getFile(final String file) throws IOException {
153 return new ClassPathResource(file).getFile();
157 public void after() {
158 cacheServiceProvider.clearAll();