2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.parsers.uri;
23 import static org.hamcrest.Matchers.hasProperty;
24 import static org.hamcrest.Matchers.is;
25 import static org.junit.Assert.assertEquals;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.ArgumentMatchers.anyString;
28 import static org.mockito.ArgumentMatchers.eq;
29 import static org.mockito.Mockito.inOrder;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.times;
32 import static org.mockito.Mockito.verify;
33 import static org.mockito.Mockito.when;
35 import java.io.UnsupportedEncodingException;
37 import java.util.Arrays;
38 import java.util.Collections;
39 import java.util.HashSet;
41 import javax.annotation.PostConstruct;
42 import javax.ws.rs.core.MultivaluedHashMap;
43 import javax.ws.rs.core.UriBuilder;
44 import javax.xml.bind.JAXBException;
46 import org.junit.Rule;
47 import org.junit.Test;
48 import org.junit.rules.ExpectedException;
49 import org.mockito.ArgumentCaptor;
50 import org.mockito.InOrder;
51 import org.onap.aai.AAISetup;
52 import org.onap.aai.edges.enums.EdgeType;
53 import org.onap.aai.exceptions.AAIException;
54 import org.onap.aai.introspection.Introspector;
55 import org.onap.aai.introspection.Loader;
56 import org.onap.aai.introspection.ModelType;
57 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
58 import org.onap.aai.setup.SchemaVersion;
61 public class URIParserTest extends AAISetup {
63 private Loader loader;
66 public ExpectedException thrown = ExpectedException.none();
71 * @throws JAXBException the JAXB exception
72 * @throws AAIException the AAI exception
73 * @throws IllegalArgumentException the illegal argument exception
74 * @throws UnsupportedEncodingException the unsupported encoding exception
77 public void createLoader() {
78 loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, new SchemaVersion("v10"));
82 public void invalidPath()
83 throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
86 .fromPath("/aai/" + loader.getVersion()
87 + "/network/tenants/tenant/key1/vservers/vserver/key2/l-interfaces/l-interface/key3")
90 thrown.expect(AAIException.class);
91 thrown.expect(hasProperty("code", is("AAI_3001")));
93 new URIToDBKey(loader, uri);
97 * Invalid path no name space.
99 * @throws JAXBException the JAXB exception
100 * @throws AAIException the AAI exception
101 * @throws IllegalArgumentException the illegal argument exception
102 * @throws UnsupportedEncodingException the unsupported encoding exception
105 public void invalidPathNoNameSpace()
106 throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
107 URI uri = UriBuilder.fromPath("/aai/" + loader.getVersion()
108 + "/tenants/tenant/key1/vservers/vserver/key2/l-interfaces/l-interface/key3").build();
110 thrown.expect(AAIException.class);
111 thrown.expect(hasProperty("code", is("AAI_3000")));
113 new URIToDBKey(loader, uri);
117 * Invalid path partial.
119 * @throws JAXBException the JAXB exception
120 * @throws AAIException the AAI exception
121 * @throws IllegalArgumentException the illegal argument exception
122 * @throws UnsupportedEncodingException the unsupported encoding exception
125 public void invalidPathPartial()
126 throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
127 URI uri = UriBuilder.fromPath("vservers/vserver/key2/l-interfaces/l-interface/key3").build();
129 thrown.expect(AAIException.class);
130 thrown.expect(hasProperty("code", is("AAI_3000")));
132 new URIToDBKey(loader, uri);
136 public void verifyParsableInvokation() throws UnsupportedEncodingException, AAIException {
137 ArgumentCaptor<Introspector> cloudInfrastructureArgument = ArgumentCaptor.forClass(Introspector.class);
138 ArgumentCaptor<Introspector> containersArgument = ArgumentCaptor.forClass(Introspector.class);
139 ArgumentCaptor<Introspector> objectArgument = ArgumentCaptor.forClass(Introspector.class);
140 URI uri = UriBuilder.fromPath("cloud-infrastructure/complexes/complex/key1/ctag-pools/ctag-pool/key2/key3")
142 Parsable parsable = mock(Parsable.class);
144 URIParser uriParser = new URIParser(loader, uri);
145 uriParser.parse(parsable);
147 verify(parsable).processNamespace(cloudInfrastructureArgument.capture());
148 verify(parsable, times(2)).processContainer(containersArgument.capture(),eq(EdgeType.TREE),eq(new MultivaluedHashMap<>()),eq(false));
149 verify(parsable, times(2)).processObject(objectArgument.capture(),eq(EdgeType.TREE),eq(new MultivaluedHashMap<>()));
150 assertEquals("cloud-infrastructure", cloudInfrastructureArgument.getValue().getName());
151 assertEquals("complexes", containersArgument.getAllValues().get(0).getName());
152 assertEquals("ctag-pools", containersArgument.getAllValues().get(1).getName());
153 assertEquals("complex", objectArgument.getAllValues().get(0).getName());
154 assertEquals("ctag-pool", objectArgument.getAllValues().get(1).getName());