7f825db8f3f8ddeeeff149526c5ebd1a43c792b1
[integration/csit.git] / plans / so / integration-etsi-testing / so-simulators / aai-simulator / src / test / java / org / onap / so / aaisimulator / controller / OwningEntityControllerTest.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.RELATIONSHIP_URL;
27 import static org.onap.so.aaisimulator.utils.TestUtils.getFile;
28 import static org.onap.so.aaisimulator.utils.TestUtils.getHttpHeaders;
29 import static org.onap.so.aaisimulator.utils.TestUtils.getJsonString;
30 import java.io.IOException;
31 import java.nio.file.Files;
32 import org.junit.After;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.onap.aai.domain.yang.OwningEntity;
36 import org.onap.so.aaisimulator.models.Format;
37 import org.onap.so.aaisimulator.models.Result;
38 import org.onap.so.aaisimulator.service.providers.OwnEntityCacheServiceProvider;
39 import org.onap.so.aaisimulator.utils.Constants;
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.HttpMethod;
50 import org.springframework.http.HttpStatus;
51 import org.springframework.http.ResponseEntity;
52 import org.springframework.test.context.ActiveProfiles;
53 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
54
55 /**
56  * @author waqas.ikram@ericsson.com
57  *
58  */
59 @RunWith(SpringJUnit4ClassRunner.class)
60 @ActiveProfiles("test")
61 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
62 @Configuration
63 public class OwningEntityControllerTest {
64     private static final String OWNING_ENTITY_JSON_FILE = "test-data/owning-entity.json";
65
66     private static final String OWN_ENTITY_ID_VALUE = "oe_1";
67     private static final String OWN_ENTITY_NAME_VALUE = "oe_2";
68
69     private static final String OWNING_ENTITY_RELATION_SHIP_JSON_FILE = "test-data/owning-entity-relation-ship.json";
70
71     @LocalServerPort
72     private int port;
73
74     @Autowired
75     private TestRestTemplate restTemplate;
76
77     @Autowired
78     private UserCredentials userCredentials;
79
80     @Autowired
81     private OwnEntityCacheServiceProvider cacheServiceProvider;
82
83     @After
84     public void after() {
85         cacheServiceProvider.clearAll();
86     }
87
88     @Test
89     public void test_putOwningEntity_successfullyAddedToCache() throws Exception {
90         final String url = getOwningEntityEndPointUrl() + "/" + OWN_ENTITY_ID_VALUE;
91         final String body = new String(Files.readAllBytes(getFile(OWNING_ENTITY_JSON_FILE).toPath()));
92         final ResponseEntity<Void> actual = invokeHttpPut(url, body);
93
94         assertEquals(HttpStatus.ACCEPTED, actual.getStatusCode());
95
96         final ResponseEntity<OwningEntity> actualResponse = invokeHttpGet(url, OwningEntity.class);
97
98         assertEquals(HttpStatus.OK, actualResponse.getStatusCode());
99         assertTrue(actualResponse.hasBody());
100         final OwningEntity actualOwningEntity = actualResponse.getBody();
101         assertEquals(OWN_ENTITY_ID_VALUE, actualOwningEntity.getOwningEntityId());
102         assertEquals(OWN_ENTITY_NAME_VALUE, actualOwningEntity.getOwningEntityName());
103         assertNotNull(actualOwningEntity.getResourceVersion());
104
105     }
106
107     @Test
108     public void test_getOwningEntityCount_correctResult() throws Exception {
109         final String url = getOwningEntityEndPointUrl() + "/" + OWN_ENTITY_ID_VALUE;
110         final String body = new String(Files.readAllBytes(getFile(OWNING_ENTITY_JSON_FILE).toPath()));
111         final ResponseEntity<Void> actual = invokeHttpPut(url, body);
112
113         assertEquals(HttpStatus.ACCEPTED, actual.getStatusCode());
114
115         final ResponseEntity<Result> actualResponse =
116                 invokeHttpGet(url + "?resultIndex=0&resultSize=1&format=" + Format.COUNT.getValue(), Result.class);
117
118         assertEquals(HttpStatus.OK, actualResponse.getStatusCode());
119         assertTrue(actualResponse.hasBody());
120         final Result result = actualResponse.getBody();
121         assertNotNull(result.getValues());
122         assertFalse(result.getValues().isEmpty());
123         assertEquals(1, result.getValues().get(0).get(Constants.OWNING_ENTITY));
124     }
125
126     @Test
127     public void test_putOwningEntityRelationShip_successfullyAddedToCache() throws Exception {
128         final String url = getOwningEntityEndPointUrl() + "/" + OWN_ENTITY_ID_VALUE;
129         final ResponseEntity<Void> actual = invokeHttpPut(url, getJsonString(OWNING_ENTITY_JSON_FILE));
130         assertEquals(HttpStatus.ACCEPTED, actual.getStatusCode());
131
132         final String owningEntityRelationshipUrl = url + RELATIONSHIP_URL;
133
134         final ResponseEntity<Void> putResponse = invokeHttpPut(owningEntityRelationshipUrl, getRelationship());
135
136         assertEquals(HttpStatus.ACCEPTED, putResponse.getStatusCode());
137
138         final ResponseEntity<OwningEntity> actualResponse = invokeHttpGet(url, OwningEntity.class);
139
140         assertEquals(HttpStatus.OK, actualResponse.getStatusCode());
141         assertTrue(actualResponse.hasBody());
142         final OwningEntity actualOwningEntity = actualResponse.getBody();
143         assertEquals(OWN_ENTITY_ID_VALUE, actualOwningEntity.getOwningEntityId());
144         assertEquals(OWN_ENTITY_NAME_VALUE, actualOwningEntity.getOwningEntityName());
145         assertNotNull(actualOwningEntity.getRelationshipList());
146         assertFalse(actualOwningEntity.getRelationshipList().getRelationship().isEmpty());
147         assertNotNull(actualOwningEntity.getRelationshipList().getRelationship().get(0));
148
149     }
150
151     private String getRelationship() throws IOException {
152         return TestUtils.getJsonString(OWNING_ENTITY_RELATION_SHIP_JSON_FILE);
153     }
154
155     private <T> ResponseEntity<T> invokeHttpGet(final String url, final Class<T> clazz) {
156         return restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders(getUsername())), clazz);
157     }
158
159     private String getUsername() {
160         return userCredentials.getUsers().iterator().next().getUsername();
161     }
162
163     private ResponseEntity<Void> invokeHttpPut(final String url, final Object obj) {
164         final HttpEntity<?> httpEntity = getHttpEntity(obj);
165         return restTemplate.exchange(url, HttpMethod.PUT, httpEntity, Void.class);
166     }
167
168     private HttpEntity<?> getHttpEntity(final Object obj) {
169         return new HttpEntity<>(obj, getHttpHeaders(getUsername()));
170     }
171
172     private String getOwningEntityEndPointUrl() {
173         return TestUtils.getBaseUrl(port) + Constants.OWNING_ENTITY_URL;
174     }
175
176 }