Merge "Update to Setup and Testcases due to PM Mapper dmaap plugin feature"
[integration/csit.git] / plans / so / integration-etsi-testing / so-simulators / aai-simulator / src / test / java / org / onap / so / aaisimulator / controller / NodesControllerTest.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.aaisimulator.controller;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26 import static org.onap.so.aaisimulator.utils.Constants.RESOURCE_LINK;
27 import static org.onap.so.aaisimulator.utils.Constants.RESOURCE_TYPE;
28 import static org.onap.so.aaisimulator.utils.Constants.SERVICE_RESOURCE_TYPE;
29 import static org.onap.so.aaisimulator.utils.TestConstants.CUSTOMERS_URL;
30 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_INSTANCE_ID;
31 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_INSTANCE_URL;
32 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_NAME;
33 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_SUBSCRIPTIONS_URL;
34 import static org.onap.so.aaisimulator.utils.TestUtils.getJsonString;
35 import java.io.IOException;
36 import java.util.Map;
37 import org.junit.After;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.onap.aai.domain.yang.ServiceInstance;
41 import org.onap.so.aaisimulator.models.Format;
42 import org.onap.so.aaisimulator.models.Result;
43 import org.onap.so.aaisimulator.service.providers.CustomerCacheServiceProvider;
44 import org.onap.so.aaisimulator.service.providers.NodesCacheServiceProvider;
45 import org.onap.so.aaisimulator.utils.Constants;
46 import org.onap.so.aaisimulator.utils.TestUtils;
47 import org.springframework.beans.factory.annotation.Autowired;
48 import org.springframework.beans.factory.annotation.Value;
49 import org.springframework.boot.test.context.SpringBootTest;
50 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
51 import org.springframework.boot.test.web.client.TestRestTemplate;
52 import org.springframework.boot.web.server.LocalServerPort;
53 import org.springframework.context.annotation.Configuration;
54 import org.springframework.http.HttpEntity;
55 import org.springframework.http.HttpHeaders;
56 import org.springframework.http.HttpMethod;
57 import org.springframework.http.HttpStatus;
58 import org.springframework.http.ResponseEntity;
59 import org.springframework.test.context.ActiveProfiles;
60 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
61
62 /**
63  * @author waqas.ikram@ericsson.com
64  *
65  */
66 @RunWith(SpringJUnit4ClassRunner.class)
67 @ActiveProfiles("test")
68 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
69 @Configuration
70 public class NodesControllerTest {
71
72     @LocalServerPort
73     private int port;
74
75     @Autowired
76     private TestRestTemplate restTemplate;
77
78     @Value("${spring.security.username}")
79     private String username;
80
81     @Autowired
82     private NodesCacheServiceProvider nodesCacheServiceProvider;
83
84     @Autowired
85     private CustomerCacheServiceProvider customerCacheServiceProvider;
86
87     @After
88     public void after() {
89         nodesCacheServiceProvider.clearAll();
90         customerCacheServiceProvider.clearAll();
91     }
92
93     @Test
94     public void test_getNodesSericeInstance_usingServiceInstanceId_ableToRetrieveServiceInstanceFromCache()
95             throws Exception {
96
97         final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
98
99         final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
100
101         assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
102
103         final ResponseEntity<Void> response2 = invokeHttpPut(url, getServiceInstance());
104         assertEquals(HttpStatus.ACCEPTED, response2.getStatusCode());
105
106         final ResponseEntity<ServiceInstance> actual =
107                 restTemplate.exchange(getNodesEndPointUrl() + SERVICE_INSTANCE_URL, HttpMethod.GET,
108                         new HttpEntity<>(getHttpHeaders()), ServiceInstance.class);
109
110         assertEquals(HttpStatus.OK, actual.getStatusCode());
111         assertTrue(actual.hasBody());
112
113         final ServiceInstance actualServiceInstance = actual.getBody();
114
115         assertEquals(SERVICE_NAME, actualServiceInstance.getServiceInstanceName());
116         assertEquals(SERVICE_INSTANCE_ID, actualServiceInstance.getServiceInstanceId());
117
118     }
119
120     @Test
121     public void test_getNodesSericeInstance_usingServiceInstanceIdAndFormatPathed_ableToRetrieveServiceInstanceFromCache()
122             throws Exception {
123
124         final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
125
126         final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
127
128         assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
129
130         final ResponseEntity<Void> response2 = invokeHttpPut(url, getServiceInstance());
131         assertEquals(HttpStatus.ACCEPTED, response2.getStatusCode());
132
133         final ResponseEntity<Result> actual = restTemplate.exchange(
134                 getNodesEndPointUrl() + SERVICE_INSTANCE_URL + "?format=" + Format.PATHED.getValue(), HttpMethod.GET,
135                 new HttpEntity<>(getHttpHeaders()), Result.class);
136
137         assertEquals(HttpStatus.OK, actual.getStatusCode());
138         assertTrue(actual.hasBody());
139
140         final Result result = actual.getBody();
141
142         assertNotNull(result.getValues());
143         assertFalse(result.getValues().isEmpty());
144         final Map<String, Object> actualMap = result.getValues().get(0);
145
146         assertEquals(CUSTOMERS_URL + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL, actualMap.get(RESOURCE_LINK));
147         assertEquals(SERVICE_RESOURCE_TYPE, actualMap.get(RESOURCE_TYPE));
148
149     }
150
151     private String getNodesEndPointUrl() {
152         return TestUtils.getBaseUrl(port) + Constants.NODES_URL;
153     }
154
155
156     private String getCustomerEndPointUrl() {
157         return TestUtils.getBaseUrl(port) + CUSTOMERS_URL;
158     }
159
160     private String getCustomer() throws Exception, IOException {
161         return getJsonString("test-data/business-customer.json");
162     }
163
164     private String getServiceInstance() throws Exception, IOException {
165         return getJsonString("test-data/service-instance.json");
166     }
167
168     private ResponseEntity<Void> invokeHttpPut(final String url, final Object obj) {
169         final HttpEntity<?> httpEntity = getHttpEntity(obj);
170         return restTemplate.exchange(url, HttpMethod.PUT, httpEntity, Void.class);
171     }
172
173     private HttpEntity<?> getHttpEntity(final Object obj) {
174         return new HttpEntity<>(obj, getHttpHeaders());
175     }
176
177     private HttpHeaders getHttpHeaders() {
178         return TestUtils.getHttpHeaders(username);
179     }
180 }