80ae70666fb3fbd05d694ef5b683d800d4ed754d
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / serialization / queryformats / utils / UrlBuilderTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.aai.serialization.queryformats.utils;
23
24 import org.apache.tinkerpop.gremlin.structure.Vertex;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.mockito.Mock;
28 import org.mockito.MockitoAnnotations;
29 import org.onap.aai.AAISetup;
30 import org.onap.aai.introspection.Version;
31 import org.onap.aai.serialization.db.DBSerializer;
32 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
33 import org.onap.aai.util.AAIConstants;
34
35 import java.io.UnsupportedEncodingException;
36 import java.net.URI;
37 import java.net.URISyntaxException;
38
39 import static org.junit.Assert.assertEquals;
40 import static org.mockito.Matchers.any;
41 import static org.mockito.Mockito.when;
42
43 public class UrlBuilderTest extends AAISetup {
44
45         @Mock
46         private DBSerializer serializer;
47         @Mock
48         private Vertex v;
49
50         private static final String uri = "/test/uri";
51         private static final Object vId = new Long(123);
52         private static final String protocolAndHost = "http://localhost/aai/";
53
54         @Before
55         public void before() throws UnsupportedEncodingException, URISyntaxException {
56                 MockitoAnnotations.initMocks(this);
57                 when(serializer.getURIForVertex(any(Vertex.class))).thenReturn(new URI(uri));
58                 when(v.id()).thenReturn(vId);
59         }
60
61         @Test
62         public void v11Pathed() throws UnsupportedEncodingException, URISyntaxException, AAIFormatVertexException {
63                 Version version = Version.v11;
64                 UrlBuilder builder = new UrlBuilder(version, serializer, protocolAndHost);
65                 String result = builder.pathed(v);
66                 
67                 assertEquals("has no protocol and host", AAIConstants.AAI_APP_ROOT + version + uri, result);
68                 
69         }
70
71         @Test
72         public void v11Id() throws UnsupportedEncodingException, URISyntaxException, AAIFormatVertexException {
73                 Version version = Version.v11;
74                 UrlBuilder builder = new UrlBuilder(version, serializer, protocolAndHost);
75                 String result = builder.id(v);
76                 
77                 assertEquals("has no protocol and host", AAIConstants.AAI_APP_ROOT + version + "/resources/id/" + vId, result);
78                 
79         }
80         
81         @Test
82         public void beforeV11Pathed() throws UnsupportedEncodingException, URISyntaxException, AAIFormatVertexException {
83                 Version version = Version.v10;
84                 UrlBuilder builder = new UrlBuilder(version, serializer, protocolAndHost);
85                 String result = builder.pathed(v);
86                 
87                 assertEquals("has protocol and host", protocolAndHost + version + uri, result);
88                 
89         }
90         
91         @Test
92         public void beforeV11Id() throws UnsupportedEncodingException, URISyntaxException, AAIFormatVertexException {
93                 Version version = Version.v10;
94                 UrlBuilder builder = new UrlBuilder(version, serializer, protocolAndHost);
95                 String result = builder.id(v);
96                 
97                 assertEquals("has protocol and host", protocolAndHost + version + "/resources/id/" + vId, result);
98                 
99         }
100         
101 }