22dc1cac309fc5cdfb097edfbee146ec3338b4f8
[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 static org.mockito.Mockito.spy;
37
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;
47
48 import com.github.tomakehurst.wiremock.admin.NotFoundException;
49 import com.github.tomakehurst.wiremock.junit.WireMockRule;
50 public class AAIResourcesClientTest {
51
52
53         @Rule
54         public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().port(8443));
55         
56         @Rule
57         public ExpectedException thrown = ExpectedException.none();
58         
59         @Test
60         public void verifyNotExists() {
61                 AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test");
62                 wireMockRule.stubFor(get(
63                                 urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build()))
64                                 .willReturn(
65                                         aResponse()
66                                         .withHeader("Content-Type", "text/plain")
67                                         .withBody("hello")
68                                         .withStatus(404)));
69                 AAIResourcesClient client= createClient();
70                 boolean result = client.exists(path);
71                 assertEquals("path not found", false, result);
72         }
73         
74         @Test
75         public void verifyDelete() {
76                 AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test2");
77                 wireMockRule.stubFor(get(
78                                 urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build()))
79                                 .willReturn(
80                                         aResponse()
81                                         .withHeader("Content-Type", "application/json")
82                                         .withBodyFile("aai/resources/mockObject.json")
83                                         .withStatus(200)));
84                 wireMockRule.stubFor(delete(
85                                 urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build()))
86                                 .withQueryParam("resource-version", equalTo("1234"))
87                                 .willReturn(
88                                         aResponse()
89                                         .withStatus(204)));
90                 AAIResourcesClient client= createClient();
91                 client.delete(path);
92         }
93         
94         @Test
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 TVNPOk1TTw=="))
100                                 .willReturn(
101                                         aResponse()
102                                         .withHeader("Content-Type", "application/json")
103                                         .withBodyFile("aai/resources/mockObject.json")
104                                         .withStatus(200)));
105                 AAIResourcesClient client= createClient();
106                 client.get(path);
107         }
108         
109         @Test
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"))
115                                 .willReturn(
116                                         aResponse()
117                                         .withHeader("Content-Type", "application/json")
118                                         .withStatus(200)));
119                 
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());
124         }
125         
126         @Test
127         public void verifyDisconnect() {
128                 AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test2");
129                 AAIResourceUri path2 = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test3");
130                 
131                 wireMockRule.stubFor(post(
132                                 urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build() + "/relationship-list/relationship"))
133                                 .willReturn(
134                                         aResponse()
135                                         .withStatus(204)));
136                 
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());
141         }
142         
143         @Test
144         public void verifyPatch() {
145                 AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test2");
146                 
147                 wireMockRule.stubFor(post(
148                                 urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build()))
149                                 .willReturn(
150                                         aResponse()
151                                         .withStatus(200)));
152                 
153                 AAIResourcesClient client= createClient();
154
155                 client.update(path, "{}");
156         }
157         
158         @Test
159         public void verifyNotExistsGet() {
160                 AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test");
161                 wireMockRule.stubFor(get(
162                                 urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build()))
163                                 .willReturn(
164                                         aResponse()
165                                         .withHeader("Content-Type", "text/plain")
166                                         .withBody("hello")
167                                         .withStatus(404)));
168                 AAIResourcesClient client= createClient();
169                 AAIResultWrapper result = client.get(path);
170                 assertEquals("is empty", true, result.isEmpty());
171         }
172         
173         @Test
174         public void verifyNotExistsGetException() {
175                 AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test");
176                 wireMockRule.stubFor(get(
177                                 urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build()))
178                                 .willReturn(
179                                         aResponse()
180                                         .withHeader("Content-Type", "text/plain")
181                                         .withBody("hello")
182                                         .withStatus(404)));
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);
187         }
188         
189         @Test
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));
197                 
198                 relationship.setRelationshipLabel(AAIEdgeLabel.USES.toString());
199                 actual = client.buildRelationship(uri, AAIEdgeLabel.USES);
200                 assertThat("expect equal has label", actual, sameBeanAs(relationship));
201                 
202         }
203         
204         private AAIResourcesClient createClient() {
205                 AAIResourcesClient client = spy(new AAIResourcesClient());
206                 doReturn(new DefaultAAIPropertiesImpl()).when(client).getRestProperties();
207                 return client;
208         }
209 }