Replaced all tabs with spaces in java and pom.xml
[so.git] / common / src / test / java / org / onap / so / client / aai / AAIResourcesClientTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.so.client.aai;
22
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 javax.ws.rs.BadRequestException;
37 import org.junit.Before;
38 import org.junit.Rule;
39 import org.junit.Test;
40 import org.junit.rules.ExpectedException;
41 import org.junit.runner.RunWith;
42 import org.mockito.InjectMocks;
43 import org.mockito.Spy;
44 import org.mockito.junit.MockitoJUnitRunner;
45 import org.onap.aai.domain.yang.Relationship;
46 import org.onap.so.client.aai.entities.AAIEdgeLabel;
47 import org.onap.so.client.aai.entities.AAIResultWrapper;
48 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
49 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
50 import org.onap.so.client.defaultproperties.DefaultAAIPropertiesImpl;
51 import com.github.tomakehurst.wiremock.admin.NotFoundException;
52 import com.github.tomakehurst.wiremock.junit.WireMockRule;
53
54 @RunWith(MockitoJUnitRunner.class)
55 public class AAIResourcesClientTest {
56
57
58     @Rule
59     public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort());
60
61     @Rule
62     public ExpectedException thrown = ExpectedException.none();
63
64
65     @Spy
66     public AAIClient client;
67
68     @InjectMocks
69     public AAIResourcesClient aaiClient = new AAIResourcesClient();
70
71     private String AAI_JSON_FILE_LOCATION = "src/test/resources/__files/aai/query/";
72
73     @Before
74     public void beforeTest() {
75         doReturn(new DefaultAAIPropertiesImpl(wireMockRule.port())).when(client).getRestProperties();
76     }
77
78     @Test
79     public void verifyNotExists() {
80         AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test");
81         wireMockRule.stubFor(get(urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build()))
82                 .willReturn(aResponse().withHeader("Content-Type", "text/plain").withBody("hello").withStatus(404)));
83         AAIResourcesClient client = aaiClient;
84         boolean result = client.exists(path);
85         assertEquals("path not found", false, result);
86     }
87
88     @Test
89     public void verifyDelete() {
90         AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test2");
91         wireMockRule.stubFor(get(urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build()))
92                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
93                         .withBodyFile("aai/resources/mockObject.json").withStatus(200)));
94         wireMockRule.stubFor(delete(urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build()))
95                 .withQueryParam("resource-version", equalTo("1234")).willReturn(aResponse().withStatus(204)));
96         AAIResourcesClient client = aaiClient;
97         client.delete(path);
98     }
99
100     @Test
101     public void verifyBasicAuth() {
102         AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test3");
103         wireMockRule.stubFor(get(urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build().toString()))
104                 .withHeader("Authorization", equalTo("Basic dGVzdDp0ZXN0"))
105                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
106                         .withBodyFile("aai/resources/mockObject.json").withStatus(200)));
107         AAIResourcesClient client = aaiClient;
108         client.get(path);
109     }
110
111     @Test
112     public void verifyConnect() {
113         AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test2");
114         AAIResourceUri path2 = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test3");
115         wireMockRule.stubFor(
116                 put(urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build() + "/relationship-list/relationship"))
117                         .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(200)));
118
119         AAIResourceUri pathClone = path.clone();
120         AAIResourcesClient client = aaiClient;
121         client.connect(path, path2);
122         assertEquals("uri not modified", pathClone.build().toString(), path.build().toString());
123     }
124
125     @Test
126     public void verifyDisconnect() {
127         AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test2");
128         AAIResourceUri path2 = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test3");
129
130         wireMockRule.stubFor(
131                 delete(urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build() + "/relationship-list/relationship"))
132                         .willReturn(aResponse().withStatus(204)));
133
134         AAIResourceUri pathClone = path.clone();
135         AAIResourcesClient client = aaiClient;
136         client.disconnect(path, path2);
137         assertEquals("uri not modified", pathClone.build().toString(), path.build().toString());
138     }
139
140     @Test
141     public void verifyPatch() {
142         AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test2");
143
144         wireMockRule.stubFor(post(urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build()))
145                 .willReturn(aResponse().withStatus(200)));
146
147         AAIResourcesClient client = aaiClient;
148
149         client.update(path, "{}");
150     }
151
152     @Test
153     public void verifyNotExistsGet() {
154         AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test");
155         wireMockRule.stubFor(get(urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build()))
156                 .willReturn(aResponse().withHeader("Content-Type", "text/plain").withBody("hello").withStatus(404)));
157         AAIResourcesClient client = aaiClient;
158         AAIResultWrapper result = client.get(path);
159         assertEquals("is empty", true, result.isEmpty());
160     }
161
162     @Test
163     public void verifyNotExistsGetException() {
164         AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test");
165         wireMockRule.stubFor(get(urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build()))
166                 .willReturn(aResponse().withHeader("Content-Type", "text/plain").withBody("hello").withStatus(404)));
167         AAIResourcesClient client = aaiClient;
168         thrown.expect(NotFoundException.class);
169         thrown.expectMessage(containsString(path.build() + " not found in A&AI"));
170         AAIResultWrapper result = client.get(path, NotFoundException.class);
171     }
172
173     @Test
174     public void verifyFailedCallException() {
175         AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test");
176         wireMockRule.stubFor(get(urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build())).willReturn(aResponse()
177                 .withHeader("Content-Type", "text/plain").withBodyFile("aai/error-message.json").withStatus(400)));
178         AAIResourcesClient client = aaiClient;
179
180         thrown.expect(BadRequestException.class);
181         thrown.expectMessage(containsString(
182                 "Invalid input performing PUT on url (msg=Precondition Required:resource-version not passed for update of url"));
183         AAIResultWrapper result = client.get(path);
184     }
185
186     @Test
187     public void buildRelationshipTest() {
188         AAIResourcesClient client = aaiClient;
189         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test");
190         Relationship relationship = new Relationship();
191         relationship.setRelatedLink(uri.build().toString());
192         Relationship actual = client.buildRelationship(uri);
193         assertThat("expect equal no label", actual, sameBeanAs(relationship));
194
195         relationship.setRelationshipLabel(AAIEdgeLabel.USES.toString());
196         actual = client.buildRelationship(uri, AAIEdgeLabel.USES);
197         assertThat("expect equal has label", actual, sameBeanAs(relationship));
198
199     }
200
201 }