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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
20 package org.onap.so.aaisimulator.controller;
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.Results;
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;
56 * @author waqas.ikram@ericsson.com
59 @RunWith(SpringJUnit4ClassRunner.class)
60 @ActiveProfiles("test")
61 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
63 public class OwningEntityControllerTest {
64 private static final String OWNING_ENTITY_JSON_FILE = "test-data/owning-entity.json";
66 private static final String OWN_ENTITY_ID_VALUE = "oe_1";
67 private static final String OWN_ENTITY_NAME_VALUE = "oe_2";
69 private static final String OWNING_ENTITY_RELATION_SHIP_JSON_FILE = "test-data/owning-entity-relation-ship.json";
75 private TestRestTemplate restTemplate;
78 private UserCredentials userCredentials;
81 private OwnEntityCacheServiceProvider cacheServiceProvider;
85 cacheServiceProvider.clearAll();
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);
94 assertEquals(HttpStatus.ACCEPTED, actual.getStatusCode());
96 final ResponseEntity<OwningEntity> actualResponse = invokeHttpGet(url, OwningEntity.class);
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());
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);
113 assertEquals(HttpStatus.ACCEPTED, actual.getStatusCode());
115 final ResponseEntity<Results> actualResponse =
116 invokeHttpGet(url + "?resultIndex=0&resultSize=1&format=" + Format.COUNT.getValue(), Results.class);
118 assertEquals(HttpStatus.OK, actualResponse.getStatusCode());
119 assertTrue(actualResponse.hasBody());
120 final Results result = actualResponse.getBody();
121 assertNotNull(result.getValues());
122 assertFalse(result.getValues().isEmpty());
123 assertEquals(1, result.getValues().get(0).get(Constants.OWNING_ENTITY));
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());
132 final String owningEntityRelationshipUrl = url + RELATIONSHIP_URL;
134 final ResponseEntity<Void> putResponse = invokeHttpPut(owningEntityRelationshipUrl, getRelationship());
136 assertEquals(HttpStatus.ACCEPTED, putResponse.getStatusCode());
138 final ResponseEntity<OwningEntity> actualResponse = invokeHttpGet(url, OwningEntity.class);
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));
151 private String getRelationship() throws IOException {
152 return TestUtils.getJsonString(OWNING_ENTITY_RELATION_SHIP_JSON_FILE);
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);
159 private String getUsername() {
160 return userCredentials.getUsers().iterator().next().getUsername();
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);
168 private HttpEntity<?> getHttpEntity(final Object obj) {
169 return new HttpEntity<>(obj, getHttpHeaders(getUsername()));
172 private String getOwningEntityEndPointUrl() {
173 return TestUtils.getBaseUrl(port) + Constants.OWNING_ENTITY_URL;