Making enhancements in aai simulator
[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.GENERIC_VNFS_URL;
31 import static org.onap.so.aaisimulator.utils.TestConstants.GENERIC_VNF_NAME;
32 import static org.onap.so.aaisimulator.utils.TestConstants.GENERIC_VNF_URL;
33 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_INSTANCE_ID;
34 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_INSTANCE_URL;
35 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_NAME;
36 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_SUBSCRIPTIONS_URL;
37 import static org.onap.so.aaisimulator.utils.TestConstants.VNF_ID;
38 import java.io.IOException;
39 import java.util.Map;
40 import org.junit.After;
41 import org.junit.Test;
42 import org.onap.aai.domain.yang.GenericVnf;
43 import org.onap.aai.domain.yang.GenericVnfs;
44 import org.onap.aai.domain.yang.ServiceInstance;
45 import org.onap.so.aaisimulator.models.Format;
46 import org.onap.so.aaisimulator.models.Results;
47 import org.onap.so.aaisimulator.service.providers.CustomerCacheServiceProvider;
48 import org.onap.so.aaisimulator.service.providers.NodesCacheServiceProvider;
49 import org.onap.so.aaisimulator.utils.TestConstants;
50 import org.onap.so.aaisimulator.utils.TestUtils;
51 import org.springframework.beans.factory.annotation.Autowired;
52 import org.springframework.http.HttpStatus;
53 import org.springframework.http.ResponseEntity;
54
55 /**
56  * @author waqas.ikram@ericsson.com
57  *
58  */
59 public class NodesControllerTest extends AbstractSpringBootTest {
60
61     @Autowired
62     private NodesCacheServiceProvider nodesCacheServiceProvider;
63
64     @Autowired
65     private CustomerCacheServiceProvider customerCacheServiceProvider;
66
67     @After
68     public void after() {
69         nodesCacheServiceProvider.clearAll();
70         customerCacheServiceProvider.clearAll();
71     }
72
73     @Test
74     public void test_getNodesSericeInstance_usingServiceInstanceId_ableToRetrieveServiceInstanceFromCache()
75             throws Exception {
76
77         invokeCustomerandServiceInstanceUrls();
78
79         final ResponseEntity<ServiceInstance> actual = testRestTemplateService
80                 .invokeHttpGet(getUrl(TestConstants.NODES_URL, SERVICE_INSTANCE_URL), ServiceInstance.class);
81
82         assertEquals(HttpStatus.OK, actual.getStatusCode());
83         assertTrue(actual.hasBody());
84
85         final ServiceInstance actualServiceInstance = actual.getBody();
86
87         assertEquals(SERVICE_NAME, actualServiceInstance.getServiceInstanceName());
88         assertEquals(SERVICE_INSTANCE_ID, actualServiceInstance.getServiceInstanceId());
89
90     }
91
92     @Test
93     public void test_getNodesSericeInstance_usingServiceInstanceIdAndFormatPathed_ableToRetrieveServiceInstanceFromCache()
94             throws Exception {
95
96         invokeCustomerandServiceInstanceUrls();
97
98         final ResponseEntity<Results> actual = testRestTemplateService.invokeHttpGet(
99                 getUrl(TestConstants.NODES_URL, SERVICE_INSTANCE_URL) + "?format=" + Format.PATHED.getValue(),
100                 Results.class);
101
102         assertEquals(HttpStatus.OK, actual.getStatusCode());
103         assertTrue(actual.hasBody());
104
105         final Results result = actual.getBody();
106
107         assertNotNull(result.getValues());
108         assertFalse(result.getValues().isEmpty());
109         final Map<String, Object> actualMap = result.getValues().get(0);
110
111         assertEquals(CUSTOMERS_URL + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL, actualMap.get(RESOURCE_LINK));
112         assertEquals(SERVICE_RESOURCE_TYPE, actualMap.get(RESOURCE_TYPE));
113
114     }
115
116     @Test
117     public void test_getNodesGenericVnfs_usingVnfName_ableToRetrieveItFromCache() throws Exception {
118         invokeCustomerandServiceInstanceUrls();
119
120         final String genericVnfUrl = getUrl(GENERIC_VNF_URL, VNF_ID);
121         final ResponseEntity<Void> genericVnfResponse =
122                 testRestTemplateService.invokeHttpPut(genericVnfUrl, TestUtils.getGenericVnf(), Void.class);
123         assertEquals(HttpStatus.ACCEPTED, genericVnfResponse.getStatusCode());
124
125         final String nodeGenericVnfsUrl =
126                 getUrl(TestConstants.NODES_URL, GENERIC_VNFS_URL) + "?vnf-name=" + GENERIC_VNF_NAME;
127
128         final ResponseEntity<GenericVnfs> actual =
129                 testRestTemplateService.invokeHttpGet(nodeGenericVnfsUrl, GenericVnfs.class);
130
131         assertEquals(HttpStatus.OK, actual.getStatusCode());
132         assertTrue(actual.hasBody());
133
134         final GenericVnfs genericVnfs = actual.getBody();
135         assertEquals(1, genericVnfs.getGenericVnf().size());
136
137         final GenericVnf genericVnf = genericVnfs.getGenericVnf().get(0);
138         assertEquals(GENERIC_VNF_NAME, genericVnf.getVnfName());
139         assertEquals(VNF_ID, genericVnf.getVnfId());
140
141     }
142
143     private void invokeCustomerandServiceInstanceUrls() throws Exception, IOException {
144         final String url = getUrl(CUSTOMERS_URL, SERVICE_SUBSCRIPTIONS_URL, SERVICE_INSTANCE_URL);
145
146         final ResponseEntity<Void> response =
147                 testRestTemplateService.invokeHttpPut(getUrl(CUSTOMERS_URL), TestUtils.getCustomer(), Void.class);
148
149         assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
150
151         final ResponseEntity<Void> response2 =
152                 testRestTemplateService.invokeHttpPut(url, TestUtils.getServiceInstance(), Void.class);
153         assertEquals(HttpStatus.ACCEPTED, response2.getStatusCode());
154     }
155
156 }