cb70479501f817935d640697fc7d64084e3c2129
[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.TestConstants.PLATFORM_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.Platform;
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.PlatformCacheServiceProvider;
38 import org.onap.so.aaisimulator.utils.Constants;
39 import org.onap.so.aaisimulator.utils.TestConstants;
40 import org.onap.so.aaisimulator.utils.TestUtils;
41 import org.onap.so.simulator.model.UserCredentials;
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.test.web.client.TestRestTemplate;
46 import org.springframework.boot.web.server.LocalServerPort;
47 import org.springframework.context.annotation.Configuration;
48 import org.springframework.http.HttpEntity;
49 import org.springframework.http.HttpHeaders;
50 import org.springframework.http.HttpMethod;
51 import org.springframework.http.HttpStatus;
52 import org.springframework.http.ResponseEntity;
53 import org.springframework.test.context.ActiveProfiles;
54 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
55
56 /**
57  * @author Waqas Ikram (waqas.ikram@est.tech)
58  *
59  */
60 @RunWith(SpringJUnit4ClassRunner.class)
61 @ActiveProfiles("test")
62 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
63 @Configuration
64 public class PlatformControllerTest {
65
66     @LocalServerPort
67     private int port;
68
69     @Autowired
70     private TestRestTemplate restTemplate;
71
72     @Autowired
73     private UserCredentials userCredentials;
74
75     @Autowired
76     private PlatformCacheServiceProvider platformCacheServiceProvider;
77
78
79     @After
80     public void after() {
81         platformCacheServiceProvider.clearAll();
82     }
83
84     @Test
85     public void test_putPlatform_successfullyAddedToCache() throws Exception {
86
87         final String platformUrl = getUrl(Constants.PLATFORMS_URL, PLATFORM_NAME);
88         final ResponseEntity<Void> platformResponse = invokeHttpPut(platformUrl, TestUtils.getPlatform(), Void.class);
89         assertEquals(HttpStatus.ACCEPTED, platformResponse.getStatusCode());
90
91         final ResponseEntity<Platform> response = invokeHttpGet(platformUrl, Platform.class);
92         assertEquals(HttpStatus.OK, response.getStatusCode());
93
94         assertTrue(response.hasBody());
95
96         final Platform actualPlatform = response.getBody();
97         assertEquals(PLATFORM_NAME, actualPlatform.getPlatformName());
98         assertNotNull("resource version should not be null", actualPlatform.getResourceVersion());
99
100     }
101
102     @Test
103     public void test_putGenericVnfRelationShipToPlatform_successfullyAddedToCache() throws Exception {
104
105         final String platformUrl = getUrl(Constants.PLATFORMS_URL, PLATFORM_NAME);
106         final ResponseEntity<Void> platformResponse = invokeHttpPut(platformUrl, TestUtils.getPlatform(), Void.class);
107         assertEquals(HttpStatus.ACCEPTED, platformResponse.getStatusCode());
108
109         final String platformRelationShipUrl = getUrl(Constants.PLATFORMS_URL, PLATFORM_NAME, RELATIONSHIP_URL);
110
111         final ResponseEntity<Relationship> responseEntity =
112                 invokeHttpPut(platformRelationShipUrl, TestUtils.getPlatformRelationShip(), Relationship.class);
113         assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode());
114
115         final Optional<Platform> optional = platformCacheServiceProvider.getPlatform(PLATFORM_NAME);
116         assertTrue(optional.isPresent());
117
118         final Platform actual = optional.get();
119
120         assertNotNull(actual.getRelationshipList());
121         final List<Relationship> relationshipList = actual.getRelationshipList().getRelationship();
122         assertFalse("Relationship list should not be empty", relationshipList.isEmpty());
123         final Relationship relationship = relationshipList.get(0);
124
125         assertFalse("RelationshipData list should not be empty", relationship.getRelationshipData().isEmpty());
126         assertFalse("RelatedToProperty list should not be empty", relationship.getRelatedToProperty().isEmpty());
127
128         final RelationshipData relationshipData = relationship.getRelationshipData().get(0);
129         assertEquals(Constants.GENERIC_VNF_VNF_ID, relationshipData.getRelationshipKey());
130         assertEquals(TestConstants.VNF_ID, relationshipData.getRelationshipValue());
131
132         final RelatedToProperty relatedToProperty = relationship.getRelatedToProperty().get(0);
133         assertEquals(Constants.GENERIC_VNF_VNF_NAME, relatedToProperty.getPropertyKey());
134         assertEquals(TestConstants.GENERIC_VNF_NAME, relatedToProperty.getPropertyValue());
135
136     }
137
138     private <T> ResponseEntity<T> invokeHttpGet(final String url, final Class<T> clazz) {
139         return restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), clazz);
140     }
141
142     private <T> ResponseEntity<T> invokeHttpPut(final String url, final Object obj, final Class<T> clazz) {
143         final HttpEntity<?> httpEntity = getHttpEntity(obj);
144         return restTemplate.exchange(url, HttpMethod.PUT, httpEntity, clazz);
145     }
146
147     private HttpEntity<?> getHttpEntity(final Object obj) {
148         return new HttpEntity<>(obj, getHttpHeaders());
149     }
150
151     private HttpHeaders getHttpHeaders() {
152         return TestUtils.getHttpHeaders(userCredentials.getUsers().iterator().next().getUsername());
153     }
154
155     private String getUrl(final String... urls) {
156         return TestUtils.getUrl(port, urls);
157     }
158
159 }