1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
/*-
* ============LICENSE_START=======================================================
* ONAP - SO
* ================================================================================
* Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ============LICENSE_END=========================================================
*/
package org.onap.so.client.aai.entities.uri;
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Optional;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.core.UriBuilder;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.Matchers;
import org.onap.so.client.aai.AAIResourcesClient;
import org.onap.so.client.aai.entities.AAIResultWrapper;
import org.onap.so.client.defaultproperties.DefaultAAIPropertiesImpl;
import org.onap.so.client.graphinventory.exceptions.GraphInventoryPayloadException;
import org.onap.so.client.graphinventory.exceptions.GraphInventoryUriComputationException;
import org.onap.so.client.graphinventory.exceptions.GraphInventoryUriNotFoundException;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
public class ServiceInstanceUriTest {
private final static String AAI_JSON_FILE_LOCATION = "src/test/resources/__files/aai/resources/";
@Rule
public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().port(8443));
@Rule
public final ExpectedException exception = ExpectedException.none();
@Test
public void found() throws IOException {
final String content = new String(Files.readAllBytes(Paths.get(AAI_JSON_FILE_LOCATION + "service-instance-pathed-query.json")));
ServiceInstanceUri instance = new ServiceInstanceUri("key3");
final Optional<String> result = instance.extractRelatedLink(content);
final String expected = "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3";
assertEquals("result is equal", expected, result.get());
}
@Test
public void oneKey() throws IOException, URISyntaxException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
ServiceInstanceUri instance = new ServiceInstanceUri("key1");
ServiceInstanceUri spy = spy(instance);
doReturn("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3").when(spy).getObjectById(any(Object.class));
final URI result = spy.build();
final URI expected = UriBuilder.fromPath("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3").build();
assertEquals("result is equal", expected, result);
}
@Test
public void oneKeyQueryParams() throws IOException, URISyntaxException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
ServiceInstanceUri instance = new ServiceInstanceUri("key1");
ServiceInstanceUri spy = spy(instance);
doReturn("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3").when(spy).getObjectById(any(Object.class));
final URI result = spy.resourceVersion("1234").build();
final URI expected = UriBuilder.fromUri("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3?resource-version=1234").build();
assertEquals("result is equal", expected, result);
}
@Test
public void oneKeyEncoded() throws IOException, URISyntaxException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
ServiceInstanceUri instance = new ServiceInstanceUri("key1");
ServiceInstanceUri spy = spy(instance);
doReturn("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3%20space").when(spy).getObjectById(any(Object.class));
final URI result = spy.build();
final URI expected = UriBuilder.fromUri("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3%20space").build();
assertEquals("result is equal", expected, result);
}
@Test
public void oneKeyGetKeys() throws IOException, URISyntaxException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
ServiceInstanceUri instance = new ServiceInstanceUri("key1");
ServiceInstanceUri spy = spy(instance);
doReturn("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3%28space").when(spy).getObjectById(any(Object.class));
assertThat(spy.getURIKeys().values(), contains("key1", "key2", "key3(space"));
}
@Test
public void oneKeyClone() throws GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
ServiceInstanceUri instance = new ServiceInstanceUri("key1");
ServiceInstanceUri spy = spy(instance);
String uri = "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3";
doReturn(uri).when(spy).getObjectById(any(Object.class));
doReturn(Optional.of(uri)).when(spy).getCachedValue();
final URI result = spy.resourceVersion("1234").clone().build();
final URI expected = UriBuilder.fromUri("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3?resource-version=1234").build();
assertEquals("result is equal", expected, result);
}
@Test
public void threeKey() throws IOException {
ServiceInstanceUri instance = new ServiceInstanceUri("key1", "key2", "key3");
final URI result = instance.build();
final URI expected = UriBuilder.fromPath("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3").build();
assertEquals("result is equal", expected, result);
}
@Test
public void notfound() throws IOException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
final String content = new String(Files.readAllBytes(Paths.get(AAI_JSON_FILE_LOCATION + "empty-query-result.json")));
ServiceInstanceUri instance = new ServiceInstanceUri("key3");
ServiceInstanceUri spy = spy(instance);
AAIResourcesClient mockResourcesClient = mock(AAIResourcesClient.class);
AAIResultWrapper wrapper = mock(AAIResultWrapper.class);
when(mockResourcesClient.get(Matchers.<AAIResourceUri>any(AAIResourceUri.class), Matchers.<Class<NotFoundException>>any())).thenReturn(wrapper);
when(wrapper.getJson()).thenReturn(content);
when(spy.getResourcesClient()).thenReturn(mockResourcesClient);
exception.expect(GraphInventoryUriComputationException.class);
spy.build();
}
@Test
public void cloneTest() {
ServiceInstanceUri instance = new ServiceInstanceUri("key1", "key2", "key3");
final URI result = instance.build();
final URI result2 = instance.clone().queryParam("something", "new").build();
assertEquals("uris are not equal", false, result.toString().equals(result2.toString()));
}
@Test
public void noVertexFound() throws GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
ServiceInstanceUri instance = new ServiceInstanceUri("key3");
ServiceInstanceUri spy = spy(instance);
AAIResourcesClient client = createClient();
doReturn(client).when(spy).getResourcesClient();
stubFor(get(urlPathMatching("/aai/v[0-9]+/nodes/service-instances/service-instance/key3"))
.willReturn(aResponse()
.withStatus(404)
.withHeader("Content-Type", "application/json")
.withBodyFile("")));
exception.expect(NotFoundException.class);
spy.build();
}
private AAIResourcesClient createClient() {
AAIResourcesClient client = spy(new AAIResourcesClient());
doReturn(new DefaultAAIPropertiesImpl()).when(client).getRestProperties();
return client;
}
}
|