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;
36 import static org.mockito.Mockito.spy;
38 import org.junit.Rule;
39 import org.junit.Test;
40 import org.junit.rules.ExpectedException;
41 import org.onap.aai.domain.yang.Relationship;
42 import org.onap.so.client.aai.entities.AAIEdgeLabel;
43 import org.onap.so.client.aai.entities.AAIResultWrapper;
44 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
45 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
46 import org.onap.so.client.defaultproperties.DefaultAAIPropertiesImpl;
48 import com.github.tomakehurst.wiremock.admin.NotFoundException;
49 import com.github.tomakehurst.wiremock.junit.WireMockRule;
50 public class AAIResourcesClientTest {
54 public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().port(8443));
57 public ExpectedException thrown = ExpectedException.none();
60 public void verifyNotExists() {
61 AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test");
62 wireMockRule.stubFor(get(
63 urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build()))
66 .withHeader("Content-Type", "text/plain")
69 AAIResourcesClient client= createClient();
70 boolean result = client.exists(path);
71 assertEquals("path not found", false, result);
75 public void verifyDelete() {
76 AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test2");
77 wireMockRule.stubFor(get(
78 urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build()))
81 .withHeader("Content-Type", "application/json")
82 .withBodyFile("aai/resources/mockObject.json")
84 wireMockRule.stubFor(delete(
85 urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build()))
86 .withQueryParam("resource-version", equalTo("1234"))
90 AAIResourcesClient client= createClient();
95 public void verifyBasicAuth() {
96 AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test3");
97 wireMockRule.stubFor(get(
98 urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build().toString()))
99 .withHeader("Authorization", equalTo("Basic dGVzdDp0ZXN0"))
102 .withHeader("Content-Type", "application/json")
103 .withBodyFile("aai/resources/mockObject.json")
105 AAIResourcesClient client= createClient();
110 public void verifyConnect() {
111 AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test2");
112 AAIResourceUri path2 = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test3");
113 wireMockRule.stubFor(put(
114 urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build() + "/relationship-list/relationship"))
117 .withHeader("Content-Type", "application/json")
120 AAIResourceUri pathClone = path.clone();
121 AAIResourcesClient client= createClient();
122 client.connect(path, path2);
123 assertEquals("uri not modified", pathClone.build().toString(), path.build().toString());
127 public void verifyDisconnect() {
128 AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test2");
129 AAIResourceUri path2 = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test3");
131 wireMockRule.stubFor(post(
132 urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build() + "/relationship-list/relationship"))
137 AAIResourceUri pathClone = path.clone();
138 AAIResourcesClient client= createClient();
139 client.disconnect(path, path2);
140 assertEquals("uri not modified", pathClone.build().toString(), path.build().toString());
144 public void verifyPatch() {
145 AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test2");
147 wireMockRule.stubFor(post(
148 urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build()))
153 AAIResourcesClient client= createClient();
155 client.update(path, "{}");
159 public void verifyNotExistsGet() {
160 AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test");
161 wireMockRule.stubFor(get(
162 urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build()))
165 .withHeader("Content-Type", "text/plain")
168 AAIResourcesClient client= createClient();
169 AAIResultWrapper result = client.get(path);
170 assertEquals("is empty", true, result.isEmpty());
174 public void verifyNotExistsGetException() {
175 AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test");
176 wireMockRule.stubFor(get(
177 urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build()))
180 .withHeader("Content-Type", "text/plain")
183 AAIResourcesClient client= createClient();
184 thrown.expect(NotFoundException.class);
185 thrown.expectMessage(containsString(path.build() + " not found in A&AI"));
186 AAIResultWrapper result = client.get(path, NotFoundException.class);
190 public void buildRelationshipTest() {
191 AAIResourcesClient client = createClient();
192 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test");
193 Relationship relationship = new Relationship();
194 relationship.setRelatedLink(uri.build().toString());
195 Relationship actual = client.buildRelationship(uri);
196 assertThat("expect equal no label", actual, sameBeanAs(relationship));
198 relationship.setRelationshipLabel(AAIEdgeLabel.USES.toString());
199 actual = client.buildRelationship(uri, AAIEdgeLabel.USES);
200 assertThat("expect equal has label", actual, sameBeanAs(relationship));
204 private AAIResourcesClient createClient() {
205 AAIResourcesClient client = spy(new AAIResourcesClient());
206 doReturn(new DefaultAAIPropertiesImpl()).when(client).getRestProperties();