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