Merge "fixed CandidateType json serialization"
[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.Before;
39 import org.junit.Rule;
40 import org.junit.Test;
41 import org.junit.rules.ExpectedException;
42 import org.junit.runner.RunWith;
43 import org.mockito.InjectMocks;
44 import org.mockito.Spy;
45 import org.mockito.junit.MockitoJUnitRunner;
46 import org.onap.aai.domain.yang.Relationship;
47 import org.onap.so.client.aai.entities.AAIEdgeLabel;
48 import org.onap.so.client.aai.entities.AAIResultWrapper;
49 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
50 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
51 import org.onap.so.client.defaultproperties.DefaultAAIPropertiesImpl;
52 import org.onap.so.client.graphinventory.GraphInventoryClient;
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().port(8443));
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         @Before
75         public void beforeTest() {
76                 doReturn(new DefaultAAIPropertiesImpl()).when(client).getRestProperties();
77         }
78         
79         @Test
80         public void verifyNotExists() {
81                 AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test");
82                 wireMockRule.stubFor(get(
83                                 urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build()))
84                                 .willReturn(
85                                         aResponse()
86                                         .withHeader("Content-Type", "text/plain")
87                                         .withBody("hello")
88                                         .withStatus(404)));
89                 AAIResourcesClient client= aaiClient;
90                 boolean result = client.exists(path);
91                 assertEquals("path not found", false, result);
92         }
93         
94         @Test
95         public void verifyDelete() {
96                 AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test2");
97                 wireMockRule.stubFor(get(
98                                 urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build()))
99                                 .willReturn(
100                                         aResponse()
101                                         .withHeader("Content-Type", "application/json")
102                                         .withBodyFile("aai/resources/mockObject.json")
103                                         .withStatus(200)));
104                 wireMockRule.stubFor(delete(
105                                 urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build()))
106                                 .withQueryParam("resource-version", equalTo("1234"))
107                                 .willReturn(
108                                         aResponse()
109                                         .withStatus(204)));
110                 AAIResourcesClient client= aaiClient;
111                 client.delete(path);
112         }
113         
114         @Test
115         public void verifyBasicAuth() {
116                 AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test3");
117                 wireMockRule.stubFor(get(
118                                 urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build().toString()))
119                                 .withHeader("Authorization", equalTo("Basic dGVzdDp0ZXN0"))
120                                 .willReturn(
121                                         aResponse()
122                                         .withHeader("Content-Type", "application/json")
123                                         .withBodyFile("aai/resources/mockObject.json")
124                                         .withStatus(200)));
125                 AAIResourcesClient client= aaiClient;
126                 client.get(path);
127         }
128         
129         @Test
130         public void verifyConnect() {
131                 AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test2");
132                 AAIResourceUri path2 = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test3");
133                 wireMockRule.stubFor(put(
134                                 urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build() + "/relationship-list/relationship"))
135                                 .willReturn(
136                                         aResponse()
137                                         .withHeader("Content-Type", "application/json")
138                                         .withStatus(200)));
139                 
140                 AAIResourceUri pathClone = path.clone();
141                 AAIResourcesClient client= aaiClient;
142                 client.connect(path, path2);
143                 assertEquals("uri not modified", pathClone.build().toString(), path.build().toString());
144         }
145         
146         @Test
147         public void verifyDisconnect() {
148                 AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test2");
149                 AAIResourceUri path2 = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test3");
150                 
151                 wireMockRule.stubFor(post(
152                                 urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build() + "/relationship-list/relationship"))
153                                 .willReturn(
154                                         aResponse()
155                                         .withStatus(204)));
156                 
157                 AAIResourceUri pathClone = path.clone();
158                 AAIResourcesClient client= aaiClient;
159                 client.disconnect(path, path2);
160                 assertEquals("uri not modified", pathClone.build().toString(), path.build().toString());
161         }
162         
163         @Test
164         public void verifyPatch() {
165                 AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test2");
166                 
167                 wireMockRule.stubFor(post(
168                                 urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build()))
169                                 .willReturn(
170                                         aResponse()
171                                         .withStatus(200)));
172                 
173                 AAIResourcesClient client= aaiClient;
174
175                 client.update(path, "{}");
176         }
177         
178         @Test
179         public void verifyNotExistsGet() {
180                 AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test");
181                 wireMockRule.stubFor(get(
182                                 urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build()))
183                                 .willReturn(
184                                         aResponse()
185                                         .withHeader("Content-Type", "text/plain")
186                                         .withBody("hello")
187                                         .withStatus(404)));
188                 AAIResourcesClient client= aaiClient;
189                 AAIResultWrapper result = client.get(path);
190                 assertEquals("is empty", true, result.isEmpty());
191         }
192         
193         @Test
194         public void verifyNotExistsGetException() {
195                 AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test");
196                 wireMockRule.stubFor(get(
197                                 urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build()))
198                                 .willReturn(
199                                         aResponse()
200                                         .withHeader("Content-Type", "text/plain")
201                                         .withBody("hello")
202                                         .withStatus(404)));
203                 AAIResourcesClient client= aaiClient;
204                 thrown.expect(NotFoundException.class);
205                 thrown.expectMessage(containsString(path.build() + " not found in A&AI"));
206                 AAIResultWrapper result = client.get(path, NotFoundException.class);
207         }
208         
209         @Test
210         public void buildRelationshipTest() {
211                 AAIResourcesClient client = aaiClient;
212                 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test");
213                 Relationship relationship = new Relationship();
214                 relationship.setRelatedLink(uri.build().toString());
215                 Relationship actual = client.buildRelationship(uri);
216                 assertThat("expect equal no label", actual, sameBeanAs(relationship));
217                 
218                 relationship.setRelationshipLabel(AAIEdgeLabel.USES.toString());
219                 actual = client.buildRelationship(uri, AAIEdgeLabel.USES);
220                 assertThat("expect equal has label", actual, sameBeanAs(relationship));
221                 
222         }
223
224 }