2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.so.client.aai;
23 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
24 import static com.github.tomakehurst.wiremock.client.WireMock.delete;
25 import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
26 import static com.github.tomakehurst.wiremock.client.WireMock.get;
27 import static com.github.tomakehurst.wiremock.client.WireMock.post;
28 import static com.github.tomakehurst.wiremock.client.WireMock.put;
29 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
30 import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
31 import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
32 import static org.hamcrest.CoreMatchers.containsString;
33 import static org.junit.Assert.assertEquals;
34 import static org.junit.Assert.assertThat;
35 import static org.mockito.Mockito.doReturn;
37 import javax.ws.rs.BadRequestException;
39 import org.junit.Before;
40 import org.junit.Rule;
41 import org.junit.Test;
42 import org.junit.rules.ExpectedException;
43 import org.junit.runner.RunWith;
44 import org.mockito.InjectMocks;
45 import org.mockito.Spy;
46 import org.mockito.junit.MockitoJUnitRunner;
47 import org.onap.aai.domain.yang.Relationship;
48 import org.onap.so.client.aai.entities.AAIEdgeLabel;
49 import org.onap.so.client.aai.entities.AAIResultWrapper;
50 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
51 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
52 import org.onap.so.client.defaultproperties.DefaultAAIPropertiesImpl;
54 import com.github.tomakehurst.wiremock.admin.NotFoundException;
55 import com.github.tomakehurst.wiremock.junit.WireMockRule;
57 @RunWith(MockitoJUnitRunner.class)
58 public class AAIResourcesClientTest {
62 public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort());
65 public ExpectedException thrown = ExpectedException.none();
69 public AAIClient client;
72 public AAIResourcesClient aaiClient = new AAIResourcesClient();
74 private String AAI_JSON_FILE_LOCATION = "src/test/resources/__files/aai/query/";
77 public void beforeTest() {
78 doReturn(new DefaultAAIPropertiesImpl(wireMockRule.port())).when(client).getRestProperties();
82 public void verifyNotExists() {
83 AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test");
84 wireMockRule.stubFor(get(
85 urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build()))
88 .withHeader("Content-Type", "text/plain")
91 AAIResourcesClient client= aaiClient;
92 boolean result = client.exists(path);
93 assertEquals("path not found", false, result);
97 public void verifyDelete() {
98 AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test2");
99 wireMockRule.stubFor(get(
100 urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build()))
103 .withHeader("Content-Type", "application/json")
104 .withBodyFile("aai/resources/mockObject.json")
106 wireMockRule.stubFor(delete(
107 urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build()))
108 .withQueryParam("resource-version", equalTo("1234"))
112 AAIResourcesClient client= aaiClient;
117 public void verifyBasicAuth() {
118 AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test3");
119 wireMockRule.stubFor(get(
120 urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build().toString()))
121 .withHeader("Authorization", equalTo("Basic dGVzdDp0ZXN0"))
124 .withHeader("Content-Type", "application/json")
125 .withBodyFile("aai/resources/mockObject.json")
127 AAIResourcesClient client= aaiClient;
132 public void verifyConnect() {
133 AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test2");
134 AAIResourceUri path2 = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test3");
135 wireMockRule.stubFor(put(
136 urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build() + "/relationship-list/relationship"))
139 .withHeader("Content-Type", "application/json")
142 AAIResourceUri pathClone = path.clone();
143 AAIResourcesClient client= aaiClient;
144 client.connect(path, path2);
145 assertEquals("uri not modified", pathClone.build().toString(), path.build().toString());
149 public void verifyDisconnect() {
150 AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test2");
151 AAIResourceUri path2 = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test3");
153 wireMockRule.stubFor(delete(
154 urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build() + "/relationship-list/relationship"))
159 AAIResourceUri pathClone = path.clone();
160 AAIResourcesClient client= aaiClient;
161 client.disconnect(path, path2);
162 assertEquals("uri not modified", pathClone.build().toString(), path.build().toString());
166 public void verifyPatch() {
167 AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test2");
169 wireMockRule.stubFor(post(
170 urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build()))
175 AAIResourcesClient client= aaiClient;
177 client.update(path, "{}");
181 public void verifyNotExistsGet() {
182 AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test");
183 wireMockRule.stubFor(get(
184 urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build()))
187 .withHeader("Content-Type", "text/plain")
190 AAIResourcesClient client= aaiClient;
191 AAIResultWrapper result = client.get(path);
192 assertEquals("is empty", true, result.isEmpty());
196 public void verifyNotExistsGetException() {
197 AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test");
198 wireMockRule.stubFor(get(
199 urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build()))
202 .withHeader("Content-Type", "text/plain")
205 AAIResourcesClient client= aaiClient;
206 thrown.expect(NotFoundException.class);
207 thrown.expectMessage(containsString(path.build() + " not found in A&AI"));
208 AAIResultWrapper result = client.get(path, NotFoundException.class);
212 public void verifyFailedCallException() {
213 AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test");
214 wireMockRule.stubFor(get(
215 urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build()))
218 .withHeader("Content-Type", "text/plain")
219 .withBodyFile("aai/error-message.json")
221 AAIResourcesClient client= aaiClient;
223 thrown.expect(BadRequestException.class);
224 thrown.expectMessage(containsString("Invalid input performing PUT on url (msg=Precondition Required:resource-version not passed for update of url"));
225 AAIResultWrapper result = client.get(path);
229 public void buildRelationshipTest() {
230 AAIResourcesClient client = aaiClient;
231 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test");
232 Relationship relationship = new Relationship();
233 relationship.setRelatedLink(uri.build().toString());
234 Relationship actual = client.buildRelationship(uri);
235 assertThat("expect equal no label", actual, sameBeanAs(relationship));
237 relationship.setRelationshipLabel(AAIEdgeLabel.USES.toString());
238 actual = client.buildRelationship(uri, AAIEdgeLabel.USES);
239 assertThat("expect equal has label", actual, sameBeanAs(relationship));