Make URIParser code more readable
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / parsers / uri / URIParserTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
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.aai.parsers.uri;
22
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;
34
35 import java.io.UnsupportedEncodingException;
36 import java.net.URI;
37 import java.util.Arrays;
38 import java.util.Collections;
39 import java.util.HashSet;
40
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;
45
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;
59
60
61 public class URIParserTest extends AAISetup {
62
63     private Loader loader;
64
65     @Rule
66     public ExpectedException thrown = ExpectedException.none();
67
68     /**
69      * Invalid path.
70      *
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
75      */
76     @PostConstruct
77     public void createLoader() {
78         loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, new SchemaVersion("v10"));
79     }
80
81     @Test
82     public void invalidPath()
83             throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
84         URI uri =
85                 UriBuilder
86                         .fromPath("/aai/" + loader.getVersion()
87                                 + "/network/tenants/tenant/key1/vservers/vserver/key2/l-interfaces/l-interface/key3")
88                         .build();
89
90         thrown.expect(AAIException.class);
91         thrown.expect(hasProperty("code", is("AAI_3001")));
92
93         new URIToDBKey(loader, uri);
94     }
95
96     /**
97      * Invalid path no name space.
98      *
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
103      */
104     @Test
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();
109
110         thrown.expect(AAIException.class);
111         thrown.expect(hasProperty("code", is("AAI_3000")));
112
113         new URIToDBKey(loader, uri);
114     }
115
116     /**
117      * Invalid path partial.
118      *
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
123      */
124     @Test
125     public void invalidPathPartial()
126             throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
127         URI uri = UriBuilder.fromPath("vservers/vserver/key2/l-interfaces/l-interface/key3").build();
128
129         thrown.expect(AAIException.class);
130         thrown.expect(hasProperty("code", is("AAI_3000")));
131
132         new URIToDBKey(loader, uri);
133     }
134
135     @Test
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")
141         .build();
142         Parsable parsable = mock(Parsable.class);
143
144         URIParser uriParser = new URIParser(loader, uri);
145         uriParser.parse(parsable);
146
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());
155     }
156 }