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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
|
/**
* ============LICENSE_START=======================================================
* org.onap.aai
* ================================================================================
* Copyright © 2017-2018 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.aai.parsers.uri;
import org.onap.aai.schema.enums.ObjectMetadata;
import org.onap.aai.setup.SchemaVersion;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.onap.aai.AAISetup;
import org.onap.aai.db.props.AAIProperties;
import org.onap.aai.exceptions.AAIException;
import org.onap.aai.introspection.*;
import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
import org.springframework.test.annotation.DirtiesContext;
import javax.annotation.PostConstruct;
import javax.ws.rs.core.UriBuilder;
import javax.xml.bind.JAXBException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.util.HashMap;
import static org.hamcrest.Matchers.hasProperty;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
public class URIToObjectTest extends AAISetup {
private SchemaVersion version ;
private SchemaVersion currentVersion;
private Loader loader ;
@Rule
public ExpectedException thrown = ExpectedException.none();
/**
* Uri.
*
* @throws JAXBException the JAXB exception
* @throws AAIException the AAI exception
* @throws IllegalArgumentException the illegal argument exception
* @throws UnsupportedEncodingException the unsupported encoding exception
*/
@PostConstruct
public void createLoader(){
version = schemaVersions.getRelatedLinkVersion();
currentVersion = schemaVersions.getDefaultVersion();
loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getRelatedLinkVersion());
}
@Test
public void uri() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
URI uri = UriBuilder.fromPath("/aai/" + loader.getVersion() + "/cloud-infrastructure/cloud-regions/cloud-region/mycloudowner/mycloudregionid/tenants/tenant/key1/vservers/vserver/key2/l-interfaces/l-interface/key3").build();
URIToObject parse = new URIToObject(loader, uri);
Introspector result = parse.getTopEntity();
String expected = "{\"cloud-owner\":\"mycloudowner\",\"cloud-region-id\":\"mycloudregionid\",\"tenants\":{\"tenant\":[{\"tenant-id\":\"key1\",\"vservers\":{\"vserver\":[{\"vserver-id\":\"key2\",\"l-interfaces\":{\"l-interface\":[{\"interface-name\":\"key3\"}]}}]}}]}}";
String topEntity = "cloud-region";
String entity = "l-interface";
testSet(result.marshal(false), parse, expected, topEntity, entity, version);
}
/**
* Uri no version.
*
* @throws JAXBException the JAXB exception
* @throws AAIException the AAI exception
* @throws IllegalArgumentException the illegal argument exception
* @throws UnsupportedEncodingException the unsupported encoding exception
* @throws AAIUnknownObjectException
*/
@Test
public void uriNoVersion() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException, AAIUnknownObjectException {
URI uri = UriBuilder.fromPath("/cloud-infrastructure/cloud-regions/cloud-region/mycloudowner/mycloudregionid/tenants/tenant/key1/vservers/vserver/key2/l-interfaces/l-interface/key3").build();
HashMap<String, Introspector> relatedObjects = new HashMap<>();
Introspector tenantObj = this.loader.introspectorFromName("tenant");
tenantObj.setValue("tenant-id", "key1");
tenantObj.setValue("tenant-name", "name1");
relatedObjects.put(tenantObj.getObjectId(), tenantObj);
Introspector vserverObj = this.loader.introspectorFromName("vserver");
vserverObj.setValue("vserver-id", "key2");
vserverObj.setValue("vserver-name", "name2");
relatedObjects.put(vserverObj.getObjectId(), vserverObj);
URIToObject parse = new URIToObject(loader, uri, relatedObjects);
Introspector result = parse.getTopEntity();
String expected = "{\"cloud-owner\":\"mycloudowner\",\"cloud-region-id\":\"mycloudregionid\",\"tenants\":{\"tenant\":[{\"tenant-id\":\"key1\",\"tenant-name\":\"name1\",\"vservers\":{\"vserver\":[{\"vserver-id\":\"key2\",\"vserver-name\":\"name2\",\"l-interfaces\":{\"l-interface\":[{\"interface-name\":\"key3\"}]}}]}}]}}";
String topEntity = "cloud-region";
String entity = "l-interface";
testSet(result.marshal(false), parse, expected, topEntity, entity, version);
}
/**
* Bad URI.
*
* @throws JAXBException the JAXB exception
* @throws AAIException the AAI exception
* @throws IllegalArgumentException the illegal argument exception
* @throws UnsupportedEncodingException the unsupported encoding exception
*/
@Test
public void badURI() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
URI uri = UriBuilder.fromPath("/aai/" + loader.getVersion() + "/cloud-infrastructure/cloud-regions/cloud-region/mycloudowner/mycloudregionid/tenants/tenant/key1/vservers/vserver/key2/l-interadsfaces/l-interface/key3").build();
thrown.expect(AAIException.class);
thrown.expect(hasProperty("code", is("AAI_3000")));
new URIToObject(loader, uri);
}
/**
* Starts with valid namespace.
*
* @throws JAXBException the JAXB exception
* @throws AAIException the AAI exception
* @throws IllegalArgumentException the illegal argument exception
* @throws UnsupportedEncodingException the unsupported encoding exception
*/
@Test
public void startsWithValidNamespace() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
URI uri = UriBuilder.fromPath("/cloud-infrastructure/cloud-regions/cloud-region/mycloudowner/mycloudregionid/tenants/tenant/key1/vservers/vserver/key2/l-interfaces/l-interface/key3").build();
URIToObject parse = new URIToObject(loader, uri);
Introspector result = parse.getTopEntity();
String expected = "{\"cloud-owner\":\"mycloudowner\",\"cloud-region-id\":\"mycloudregionid\",\"tenants\":{\"tenant\":[{\"tenant-id\":\"key1\",\"vservers\":{\"vserver\":[{\"vserver-id\":\"key2\",\"l-interfaces\":{\"l-interface\":[{\"interface-name\":\"key3\"}]}}]}}]}}";
String topEntity = "cloud-region";
String entity = "l-interface";
testSet(result.marshal(false), parse, expected, topEntity, entity, version);
}
/**
* Single top level.
*
* @throws JAXBException the JAXB exception
* @throws AAIException the AAI exception
* @throws IllegalArgumentException the illegal argument exception
* @throws UnsupportedEncodingException the unsupported encoding exception
*/
@Test
public void singleTopLevel() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
URI uri = UriBuilder.fromPath("/network/generic-vnfs/generic-vnf/key1").build();
URIToObject parse = new URIToObject(loader, uri);
Introspector result = parse.getTopEntity();
String expected = "{\"vnf-id\":\"key1\"}";
String topEntity = "generic-vnf";
String entity = "generic-vnf";
testSet(result.marshal(false), parse, expected, topEntity, entity, version);
}
/**
* Naming exceptions.
*
* @throws JAXBException the JAXB exception
* @throws AAIException the AAI exception
* @throws IllegalArgumentException the illegal argument exception
* @throws UnsupportedEncodingException the unsupported encoding exception
*/
@Test
@Ignore
public void namingExceptions() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
URI uri = UriBuilder.fromPath("network/vces/vce/key1/port-groups/port-group/key2/cvlan-tags/cvlan-tag/655").build();
URIToObject parse = new URIToObject(loader, uri);
Introspector result = parse.getTopEntity();
String expected = "{\"vnf-id\":\"key1\",\"port-groups\":{\"port-group\":[{\"interface-id\":\"key2\",\"cvlan-tags\":{\"cvlan-tag-entry\":[{\"cvlan-tag\":655}]}}]}}";
String topEntity = "vce";
String entity = "cvlan-tag";
testSet(result.marshal(false), parse, expected, topEntity, entity, version);
}
/**
* No list object.
*
* @throws IllegalArgumentException the illegal argument exception
* @throws UnsupportedEncodingException the unsupported encoding exception
* @throws AAIException the AAI exception
*/
@Test
@Ignore
public void noListObject() throws IllegalArgumentException, UnsupportedEncodingException, AAIException {
URI uri = UriBuilder.fromPath("/aai/v6/network/vpls-pes/vpls-pe/0e6189fd-9257-49b9-a3be-d7ba980ccfc9/lag-interfaces/lag-interface/8ae5aa76-d597-4382-b219-04f266fe5e37/l-interfaces/l-interface/9e141d03-467b-437f-b4eb-b3133ec1e205/l3-interface-ipv4-address-list/8f19f0ea-a81f-488e-8d5c-9b7b53696c11").build();
URIToObject parse = new URIToObject(loader, uri);
Introspector result = parse.getTopEntity();
String topEntity = "vpls-pe";
String entity = "l3-interface-ipv4-address-list";
String expected = "{\"equipment-name\":\"0e6189fd-9257-49b9-a3be-d7ba980ccfc9\",\"lag-interfaces\":{\"lag-interface\":[{\"interface-name\":\"8ae5aa76-d597-4382-b219-04f266fe5e37\",\"l-interfaces\":{\"l-interface\":[{\"interface-name\":\"9e141d03-467b-437f-b4eb-b3133ec1e205\",\"l3-interface-ipv4-address-list\":[{\"l3-interface-ipv4-address\":\"8f19f0ea-a81f-488e-8d5c-9b7b53696c11\"}]}]}}]}}";
testSet(result.marshal(false), parse, expected, topEntity, entity, version);
}
@Test
public void relativePath() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
URI uri = UriBuilder.fromPath("./l-interfaces/l-interface/key1").build();
URIToObject parse = new URIToObject(loader, uri);
Introspector result = parse.getEntity();
String expected = "{\"interface-name\":\"key1\"}";
String topEntity = "l-interface";
String entity = "l-interface";
testSet(result.marshal(false), parse, expected, topEntity, entity, version);
}
/**
* Test set.
*
* @param json the json
* @param parse the parse
* @param expected the expected
* @param topEntity the top entity
* @param entity the entity
* @param version the version
*/
public void testSet(String json, URIToObject parse, String expected, String topEntity, String entity, SchemaVersion version) {
assertEquals("blah", expected, json);
assertEquals("top entity", topEntity, parse.getTopEntityName());
assertEquals("entity", entity, parse.getEntityName());
assertEquals("entity object", entity, parse.getEntity().getDbName());
assertEquals("parent list object", 1, parse.getParentList().size());
assertEquals("object version", version, parse.getObjectVersion());
}
}
|