Containerization feature of SO
[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 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;
35
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;
44
45 import com.github.tomakehurst.wiremock.admin.NotFoundException;
46 import com.github.tomakehurst.wiremock.junit.WireMockRule;
47 public class AAIResourcesClientTest {
48
49
50         @Rule
51         public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().port(8443));
52         
53         @Rule
54         public ExpectedException thrown = ExpectedException.none();
55         
56         @Test
57         public void verifyNotExists() {
58                 AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test");
59                 wireMockRule.stubFor(get(
60                                 urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build()))
61                                 .willReturn(
62                                         aResponse()
63                                         .withHeader("Content-Type", "text/plain")
64                                         .withBody("hello")
65                                         .withStatus(404)));
66                 AAIResourcesClient client= createClient();
67                 boolean result = client.exists(path);
68                 assertEquals("path not found", false, result);
69         }
70         
71         @Test
72         public void verifyDelete() {
73                 AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test2");
74                 wireMockRule.stubFor(get(
75                                 urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build()))
76                                 .willReturn(
77                                         aResponse()
78                                         .withHeader("Content-Type", "application/json")
79                                         .withBodyFile("aai/resources/mockObject.json")
80                                         .withStatus(200)));
81                 wireMockRule.stubFor(delete(
82                                 urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build()))
83                                 .withQueryParam("resource-version", equalTo("1234"))
84                                 .willReturn(
85                                         aResponse()
86                                         .withStatus(204)));
87                 AAIResourcesClient client= createClient();
88                 client.delete(path);
89         }
90         
91         @Test
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=="))
97                                 .willReturn(
98                                         aResponse()
99                                         .withHeader("Content-Type", "application/json")
100                                         .withBodyFile("aai/resources/mockObject.json")
101                                         .withStatus(200)));
102                 AAIResourcesClient client= createClient();
103                 client.get(path);
104         }
105         
106         @Test
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"))
112                                 .willReturn(
113                                         aResponse()
114                                         .withHeader("Content-Type", "application/json")
115                                         .withStatus(200)));
116                 
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());
121         }
122         
123         @Test
124         public void verifyDisconnect() {
125                 AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test2");
126                 AAIResourceUri path2 = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test3");
127                 
128                 wireMockRule.stubFor(post(
129                                 urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build() + "/relationship-list/relationship"))
130                                 .willReturn(
131                                         aResponse()
132                                         .withStatus(204)));
133                 
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());
138         }
139         
140         @Test
141         public void verifyPatch() {
142                 AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test2");
143                 
144                 wireMockRule.stubFor(post(
145                                 urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build()))
146                                 .willReturn(
147                                         aResponse()
148                                         .withStatus(200)));
149                 
150                 AAIResourcesClient client= createClient();
151
152                 client.update(path, "{}");
153         }
154         
155         @Test
156         public void verifyNotExistsGet() {
157                 AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test");
158                 wireMockRule.stubFor(get(
159                                 urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build()))
160                                 .willReturn(
161                                         aResponse()
162                                         .withHeader("Content-Type", "text/plain")
163                                         .withBody("hello")
164                                         .withStatus(404)));
165                 AAIResourcesClient client= createClient();
166                 AAIResultWrapper result = client.get(path);
167                 assertEquals("is empty", true, result.isEmpty());
168         }
169         
170         @Test
171         public void verifyNotExistsGetException() {
172                 AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test");
173                 wireMockRule.stubFor(get(
174                                 urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build()))
175                                 .willReturn(
176                                         aResponse()
177                                         .withHeader("Content-Type", "text/plain")
178                                         .withBody("hello")
179                                         .withStatus(404)));
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);
184         }
185         
186         private AAIResourcesClient createClient() {
187                 AAIResourcesClient client = spy(new AAIResourcesClient());
188                 doReturn(new DefaultAAIPropertiesImpl()).when(client).getRestProperties();
189                 return client;
190         }
191 }