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