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 / LinesOfBusinessControllerTest.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.BI_DIRECTIONAL_RELATIONSHIP_LIST_URL;
27 import static org.onap.so.aaisimulator.utils.TestConstants.GENERIC_VNF_URL;
28 import static org.onap.so.aaisimulator.utils.TestConstants.LINE_OF_BUSINESS_NAME;
29 import static org.onap.so.aaisimulator.utils.TestConstants.VNF_ID;
30 import java.util.List;
31 import java.util.Optional;
32 import org.junit.After;
33 import org.junit.Test;
34 import org.onap.aai.domain.yang.LineOfBusiness;
35 import org.onap.aai.domain.yang.RelatedToProperty;
36 import org.onap.aai.domain.yang.Relationship;
37 import org.onap.aai.domain.yang.RelationshipData;
38 import org.onap.so.aaisimulator.models.Format;
39 import org.onap.so.aaisimulator.models.Results;
40 import org.onap.so.aaisimulator.service.providers.LinesOfBusinessCacheServiceProvider;
41 import org.onap.so.aaisimulator.utils.Constants;
42 import org.onap.so.aaisimulator.utils.TestConstants;
43 import org.onap.so.aaisimulator.utils.TestUtils;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.http.HttpStatus;
46 import org.springframework.http.ResponseEntity;
47
48 /**
49  * @author Waqas Ikram (waqas.ikram@est.tech)
50  *
51  */
52 public class LinesOfBusinessControllerTest extends AbstractSpringBootTest {
53
54     @Autowired
55     private LinesOfBusinessCacheServiceProvider linesOfBusinessCacheServiceProvider;
56
57     @After
58     public void after() {
59         linesOfBusinessCacheServiceProvider.clearAll();
60     }
61
62     @Test
63     public void test_putLineOfBusiness_successfullyAddedToCache() throws Exception {
64
65         final String url = getUrl(TestConstants.LINES_OF_BUSINESS_URL, LINE_OF_BUSINESS_NAME);
66         final ResponseEntity<Void> lineOfBusinessResponse =
67                 testRestTemplateService.invokeHttpPut(url, TestUtils.getLineOfBusiness(), Void.class);
68         assertEquals(HttpStatus.ACCEPTED, lineOfBusinessResponse.getStatusCode());
69
70         final ResponseEntity<LineOfBusiness> response =
71                 testRestTemplateService.invokeHttpGet(url, LineOfBusiness.class);
72         assertEquals(HttpStatus.OK, response.getStatusCode());
73
74         assertTrue(response.hasBody());
75
76         final LineOfBusiness actualLineOfBusiness = response.getBody();
77         assertEquals(LINE_OF_BUSINESS_NAME, actualLineOfBusiness.getLineOfBusinessName());
78         assertNotNull("resource version should not be null", actualLineOfBusiness.getResourceVersion());
79
80     }
81
82     @Test
83     public void test_getLineOfBusinessWithFormatCount() throws Exception {
84
85         final String url = getUrl(TestConstants.LINES_OF_BUSINESS_URL, LINE_OF_BUSINESS_NAME);
86         final ResponseEntity<Void> lineOfBusinessResponse =
87                 testRestTemplateService.invokeHttpPut(url, TestUtils.getLineOfBusiness(), Void.class);
88         assertEquals(HttpStatus.ACCEPTED, lineOfBusinessResponse.getStatusCode());
89
90         final ResponseEntity<Results> response = testRestTemplateService
91                 .invokeHttpGet(url + "?resultIndex=0&resultSize=1&format=" + Format.COUNT.getValue(), Results.class);
92         assertEquals(HttpStatus.OK, response.getStatusCode());
93
94         assertTrue(response.hasBody());
95
96         final Results result = response.getBody();
97         assertNotNull(result.getValues());
98         assertFalse(result.getValues().isEmpty());
99         assertEquals(1, result.getValues().get(0).get(Constants.LINE_OF_BUSINESS));
100     }
101
102
103     @Test
104     public void test_putGenericVnfRelationShipToPlatform_successfullyAddedToCache() throws Exception {
105
106         final String url = getUrl(TestConstants.LINES_OF_BUSINESS_URL, LINE_OF_BUSINESS_NAME);
107         final ResponseEntity<Void> response =
108                 testRestTemplateService.invokeHttpPut(url, TestUtils.getLineOfBusiness(), Void.class);
109         assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
110
111         final String relationShipUrl = getUrl(TestConstants.LINES_OF_BUSINESS_URL, LINE_OF_BUSINESS_NAME,
112                 BI_DIRECTIONAL_RELATIONSHIP_LIST_URL);
113
114         final ResponseEntity<Relationship> responseEntity = testRestTemplateService.invokeHttpPut(relationShipUrl,
115                 TestUtils.getGenericVnfRelationShip(), Relationship.class);
116         assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode());
117
118         final Optional<LineOfBusiness> optional =
119                 linesOfBusinessCacheServiceProvider.getLineOfBusiness(LINE_OF_BUSINESS_NAME);
120         assertTrue(optional.isPresent());
121
122         final LineOfBusiness actual = optional.get();
123
124         assertNotNull(actual.getRelationshipList());
125         final List<Relationship> relationshipList = actual.getRelationshipList().getRelationship();
126         assertFalse("Relationship list should not be empty", relationshipList.isEmpty());
127         final Relationship relationship = relationshipList.get(0);
128
129         assertEquals(GENERIC_VNF_URL + VNF_ID, relationship.getRelatedLink());
130         assertFalse("RelationshipData list should not be empty", relationship.getRelationshipData().isEmpty());
131         assertFalse("RelatedToProperty list should not be empty", relationship.getRelatedToProperty().isEmpty());
132
133         final RelationshipData relationshipData = relationship.getRelationshipData().get(0);
134         assertEquals(Constants.GENERIC_VNF_VNF_ID, relationshipData.getRelationshipKey());
135         assertEquals(TestConstants.VNF_ID, relationshipData.getRelationshipValue());
136
137         final RelatedToProperty relatedToProperty = relationship.getRelatedToProperty().get(0);
138         assertEquals(Constants.GENERIC_VNF_VNF_NAME, relatedToProperty.getPropertyKey());
139         assertEquals(TestConstants.GENERIC_VNF_NAME, relatedToProperty.getPropertyValue());
140
141     }
142
143 }