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 org.hamcrest.CoreMatchers.containsString;
32 import static org.junit.Assert.assertEquals;
33 import static org.mockito.Mockito.doReturn;
34 import static org.mockito.Mockito.spy;
36 import org.junit.Before;
37 import org.junit.Rule;
38 import org.junit.Test;
39 import org.junit.rules.ExpectedException;
40 import org.onap.so.client.aai.entities.AAIResultWrapper;
41 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
42 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
43 import org.onap.so.client.defaultproperties.DefaultAAIPropertiesImpl;
45 import com.github.tomakehurst.wiremock.admin.NotFoundException;
46 import com.github.tomakehurst.wiremock.junit.WireMockRule;
47 public class AAIResourcesClientTest {
51 public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().port(8443));
54 public ExpectedException thrown = ExpectedException.none();
57 public void verifyNotExists() {
58 AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test");
59 wireMockRule.stubFor(get(
60 urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build()))
63 .withHeader("Content-Type", "text/plain")
66 AAIResourcesClient client= createClient();
67 boolean result = client.exists(path);
68 assertEquals("path not found", false, result);
72 public void verifyDelete() {
73 AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test2");
74 wireMockRule.stubFor(get(
75 urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build()))
78 .withHeader("Content-Type", "application/json")
79 .withBodyFile("aai/resources/mockObject.json")
81 wireMockRule.stubFor(delete(
82 urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build()))
83 .withQueryParam("resource-version", equalTo("1234"))
87 AAIResourcesClient client= createClient();
92 public void verifyBasicAuth() {
93 AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test3");
94 wireMockRule.stubFor(get(
95 urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build().toString()))
96 .withHeader("Authorization", equalTo("Basic TVNPOk1TTw=="))
99 .withHeader("Content-Type", "application/json")
100 .withBodyFile("aai/resources/mockObject.json")
102 AAIResourcesClient client= createClient();
107 public void verifyConnect() {
108 AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test2");
109 AAIResourceUri path2 = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test3");
110 wireMockRule.stubFor(put(
111 urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build() + "/relationship-list/relationship"))
114 .withHeader("Content-Type", "application/json")
117 AAIResourceUri pathClone = path.clone();
118 AAIResourcesClient client= createClient();
119 client.connect(path, path2);
120 assertEquals("uri not modified", pathClone.build().toString(), path.build().toString());
124 public void verifyDisconnect() {
125 AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test2");
126 AAIResourceUri path2 = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test3");
128 wireMockRule.stubFor(post(
129 urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build() + "/relationship-list/relationship"))
134 AAIResourceUri pathClone = path.clone();
135 AAIResourcesClient client= createClient();
136 client.disconnect(path, path2);
137 assertEquals("uri not modified", pathClone.build().toString(), path.build().toString());
141 public void verifyPatch() {
142 AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test2");
144 wireMockRule.stubFor(post(
145 urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build()))
150 AAIResourcesClient client= createClient();
152 client.update(path, "{}");
156 public void verifyNotExistsGet() {
157 AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test");
158 wireMockRule.stubFor(get(
159 urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build()))
162 .withHeader("Content-Type", "text/plain")
165 AAIResourcesClient client= createClient();
166 AAIResultWrapper result = client.get(path);
167 assertEquals("is empty", true, result.isEmpty());
171 public void verifyNotExistsGetException() {
172 AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test");
173 wireMockRule.stubFor(get(
174 urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build()))
177 .withHeader("Content-Type", "text/plain")
180 AAIResourcesClient client= createClient();
181 thrown.expect(NotFoundException.class);
182 thrown.expectMessage(containsString(path.build() + " not found in A&AI"));
183 AAIResultWrapper result = client.get(path, NotFoundException.class);
186 private AAIResourcesClient createClient() {
187 AAIResourcesClient client = spy(new AAIResourcesClient());
188 doReturn(new DefaultAAIPropertiesImpl()).when(client).getRestProperties();