Adding operations endpoint for sdn-c simulator
[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 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;
52
53 /**
54  * @author Waqas Ikram (waqas.ikram@est.tech)
55  *
56  */
57 @RunWith(SpringJUnit4ClassRunner.class)
58 @ActiveProfiles("test")
59 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
60 @Configuration
61 public class OperationsControllerTest {
62
63     private static final String SVC_REQUEST_ID = "04fc9f50-87b8-430d-a232-ef24bd6c4150";
64
65     private static final String SERVICE_INSTANCE_ID = "ccece8fe-13da-456a-baf6-41b3a4a2bc2b";
66
67     private static final String SERVICE_TOPOLOGY_OPERATION_URL = "/GENERIC-RESOURCE-API:service-topology-operation/";
68
69     @LocalServerPort
70     private int port;
71
72     @Autowired
73     private TestRestTemplate restTemplate;
74
75     @Autowired
76     private ServiceOperationsCacheServiceProvider cacheServiceProvider;
77
78     @Test
79     public void test_postServiceOperationInformation_successfullyAddedToCache() throws Exception {
80
81         final HttpEntity<?> httpEntity = new HttpEntity<>(getRequestInput(), getHttpHeaders());
82         final ResponseEntity<OutputRequest> responseEntity =
83                 restTemplate.exchange(getUrl(), HttpMethod.POST, httpEntity, OutputRequest.class);
84
85         assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
86         assertTrue(responseEntity.hasBody());
87         final OutputRequest actualObject = responseEntity.getBody();
88         assertNotNull(actualObject);
89
90         assertEquals(HttpStatus.OK.toString(), actualObject.getResponseCode());
91         assertEquals(Constants.YES, actualObject.getAckFinalIndicator());
92         assertEquals(SVC_REQUEST_ID, actualObject.getSvcRequestId());
93         assertNotNull(actualObject.getServiceResponseInformation());
94
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());
98         assertTrue(
99                 cacheServiceProvider.getGenericResourceApiServiceModelInfrastructure(SERVICE_INSTANCE_ID).isPresent());
100     }
101
102     @Test
103     public void test_postServiceOperationInformation_NullInputRequest_badRequest() throws Exception {
104
105         final HttpEntity<?> httpEntity = new HttpEntity<>(new InputRequest<>(), getHttpHeaders());
106         final ResponseEntity<OutputRequest> responseEntity =
107                 restTemplate.exchange(getUrl(), HttpMethod.POST, httpEntity, OutputRequest.class);
108
109         assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
110     }
111
112     @Test
113     public void test_postServiceOperationInformation_NullServiceInstanceId_badRequest() throws Exception {
114
115         final HttpEntity<?> httpEntity = new HttpEntity<>(getInvalidRequestInput(), getHttpHeaders());
116         final ResponseEntity<OutputRequest> responseEntity =
117                 restTemplate.exchange(getUrl(), HttpMethod.POST, httpEntity, OutputRequest.class);
118
119         assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
120         assertTrue(responseEntity.hasBody());
121         final OutputRequest actualObject = responseEntity.getBody();
122
123         assertEquals(HttpStatus.BAD_REQUEST.toString(), actualObject.getResponseCode());
124         assertEquals(SVC_REQUEST_ID, actualObject.getSvcRequestId());
125         assertEquals(Constants.YES, actualObject.getAckFinalIndicator());
126
127     }
128
129     private HttpHeaders getHttpHeaders() {
130         final HttpHeaders requestHeaders = new HttpHeaders();
131         requestHeaders.setContentType(MediaType.APPLICATION_JSON);
132         return requestHeaders;
133     }
134
135
136     private String getUrl() {
137         return "http://localhost:" + port + Constants.OPERATIONS_URL + SERVICE_TOPOLOGY_OPERATION_URL;
138     }
139
140     private String getRequestInput() throws IOException {
141         return getFileAsString(getFile("test-data/input.json").toPath());
142     }
143
144     private String getInvalidRequestInput() throws IOException {
145         return getFileAsString(getFile("test-data/InvalidInput.json").toPath());
146     }
147
148     private String getFileAsString(final Path path) throws IOException {
149         return new String(Files.readAllBytes(path));
150     }
151
152     private static File getFile(final String file) throws IOException {
153         return new ClassPathResource(file).getFile();
154     }
155
156     @After
157     public void after() {
158         cacheServiceProvider.clearAll();
159     }
160
161 }