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