c63d602bca0385b53c65111d6240194b777e5fc9
[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
37 import javax.ws.rs.BadRequestException;
38
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;
53
54 import com.github.tomakehurst.wiremock.admin.NotFoundException;
55 import com.github.tomakehurst.wiremock.junit.WireMockRule;
56
57 @RunWith(MockitoJUnitRunner.class)
58 public class AAIResourcesClientTest {
59
60
61         @Rule
62         public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort());
63         
64         @Rule
65         public ExpectedException thrown = ExpectedException.none();
66         
67         
68         @Spy
69         public AAIClient client;
70         
71         @InjectMocks
72         public AAIResourcesClient aaiClient = new AAIResourcesClient();
73         
74         private String AAI_JSON_FILE_LOCATION = "src/test/resources/__files/aai/query/";
75
76         @Before
77         public void beforeTest() {
78                 doReturn(new DefaultAAIPropertiesImpl(wireMockRule.port())).when(client).getRestProperties();
79         }
80         
81         @Test
82         public void verifyNotExists() {
83                 AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test");
84                 wireMockRule.stubFor(get(
85                                 urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build()))
86                                 .willReturn(
87                                         aResponse()
88                                         .withHeader("Content-Type", "text/plain")
89                                         .withBody("hello")
90                                         .withStatus(404)));
91                 AAIResourcesClient client= aaiClient;
92                 boolean result = client.exists(path);
93                 assertEquals("path not found", false, result);
94         }
95         
96         @Test
97         public void verifyDelete() {
98                 AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test2");
99                 wireMockRule.stubFor(get(
100                                 urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build()))
101                                 .willReturn(
102                                         aResponse()
103                                         .withHeader("Content-Type", "application/json")
104                                         .withBodyFile("aai/resources/mockObject.json")
105                                         .withStatus(200)));
106                 wireMockRule.stubFor(delete(
107                                 urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build()))
108                                 .withQueryParam("resource-version", equalTo("1234"))
109                                 .willReturn(
110                                         aResponse()
111                                         .withStatus(204)));
112                 AAIResourcesClient client= aaiClient;
113                 client.delete(path);
114         }
115         
116         @Test
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"))
122                                 .willReturn(
123                                         aResponse()
124                                         .withHeader("Content-Type", "application/json")
125                                         .withBodyFile("aai/resources/mockObject.json")
126                                         .withStatus(200)));
127                 AAIResourcesClient client= aaiClient;
128                 client.get(path);
129         }
130         
131         @Test
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"))
137                                 .willReturn(
138                                         aResponse()
139                                         .withHeader("Content-Type", "application/json")
140                                         .withStatus(200)));
141                 
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());
146         }
147         
148         @Test
149         public void verifyDisconnect() {
150                 AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test2");
151                 AAIResourceUri path2 = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test3");
152                 
153                 wireMockRule.stubFor(delete(
154                                 urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build() + "/relationship-list/relationship"))
155                                 .willReturn(
156                                         aResponse()
157                                         .withStatus(204)));
158                 
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());
163         }
164         
165         @Test
166         public void verifyPatch() {
167                 AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test2");
168                 
169                 wireMockRule.stubFor(post(
170                                 urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build()))
171                                 .willReturn(
172                                         aResponse()
173                                         .withStatus(200)));
174                 
175                 AAIResourcesClient client= aaiClient;
176
177                 client.update(path, "{}");
178         }
179         
180         @Test
181         public void verifyNotExistsGet() {
182                 AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test");
183                 wireMockRule.stubFor(get(
184                                 urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build()))
185                                 .willReturn(
186                                         aResponse()
187                                         .withHeader("Content-Type", "text/plain")
188                                         .withBody("hello")
189                                         .withStatus(404)));
190                 AAIResourcesClient client= aaiClient;
191                 AAIResultWrapper result = client.get(path);
192                 assertEquals("is empty", true, result.isEmpty());
193         }
194         
195         @Test
196         public void verifyNotExistsGetException() {
197                 AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test");
198                 wireMockRule.stubFor(get(
199                                 urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build()))
200                                 .willReturn(
201                                         aResponse()
202                                         .withHeader("Content-Type", "text/plain")
203                                         .withBody("hello")
204                                         .withStatus(404)));
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);
209         }
210         
211         @Test
212         public void verifyFailedCallException() {
213                 AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test");
214                 wireMockRule.stubFor(get(
215                                 urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build()))
216                                 .willReturn(
217                                         aResponse()
218                                         .withHeader("Content-Type", "text/plain")
219                                         .withBodyFile("aai/error-message.json")
220                                         .withStatus(400)));
221                 AAIResourcesClient client= aaiClient;
222                 
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);
226         }
227         
228         @Test
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));
236                 
237                 relationship.setRelationshipLabel(AAIEdgeLabel.USES.toString());
238                 actual = client.buildRelationship(uri, AAIEdgeLabel.USES);
239                 assertThat("expect equal has label", actual, sameBeanAs(relationship));
240                 
241         }
242
243 }