Change all the packages from openecomp to onap
[aai/traversal.git] / aai-traversal / src / test / java / org / onap / aai / rest / util / ValidateEncodingTest.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.rest.util;
23
24 import static org.junit.Assert.*;
25
26 import java.io.UnsupportedEncodingException;
27
28 import javax.ws.rs.core.MultivaluedHashMap;
29 import javax.ws.rs.core.MultivaluedMap;
30 import javax.ws.rs.core.UriInfo;
31
32 import org.junit.Test;
33 import org.mockito.Mockito;
34
35 public class ValidateEncodingTest {
36
37         
38         @Test
39         public void badPath() throws UnsupportedEncodingException {
40                 String badPath = "/aai/v6/network/vces/vce/blahh::blach/others/other/jklfea{}";
41                 UriInfo mockUriInfo = getMockUriInfo(badPath, new MultivaluedHashMap<String, String>());
42                 ValidateEncoding validator = ValidateEncoding.getInstance();
43                 
44                 assertEquals(false, validator.validate(mockUriInfo));
45         }
46         
47         @Test
48         public void goodPath() throws UnsupportedEncodingException {
49                 String goodPath = "/aai/v6/network/vces/vce/blahh%3A%3Ablach/others/other/jklfea%7B%7D";
50                 UriInfo mockUriInfo = getMockUriInfo(goodPath, new MultivaluedHashMap<String, String>());
51                 ValidateEncoding validator = ValidateEncoding.getInstance();
52                 
53                 assertEquals(true, validator.validate(mockUriInfo));    
54         }
55         
56         @Test
57         public void badQueryParamsKey() throws UnsupportedEncodingException {
58                 MultivaluedHashMap<String, String> map = new MultivaluedHashMap<String, String>();
59                 map.putSingle("blahblah", "test");
60                 map.putSingle("blahblah", "test2");
61                 map.putSingle("bad::bad", "test3");
62                 UriInfo mockUriInfo = getMockUriInfo("", map);
63
64                 ValidateEncoding validator = ValidateEncoding.getInstance();
65                 
66                 assertEquals(false, validator.validate(mockUriInfo));
67                 
68         }
69         @Test
70         public void badQueryParamsValue() throws UnsupportedEncodingException {
71                 MultivaluedHashMap<String, String> map = new MultivaluedHashMap<String, String>();
72                 map.putSingle("blahblah", "test");
73                 map.putSingle("blahblah", "test//:2");
74                 map.putSingle("badbad", "test3");
75                 UriInfo mockUriInfo = getMockUriInfo("", map);
76
77                 ValidateEncoding validator = ValidateEncoding.getInstance();
78                 
79                 assertEquals(false, validator.validate(mockUriInfo));
80         }
81         @Test
82         public void goodQueryParams() throws UnsupportedEncodingException {
83                 MultivaluedHashMap<String, String> map = new MultivaluedHashMap<String, String>();
84                 map.putSingle("blahblah", "test");
85                 map.putSingle("blahblah", "test2");
86                 map.putSingle("badbad", "~test%2F%2F%3A3");
87                 UriInfo mockUriInfo = getMockUriInfo("", map);
88
89                 ValidateEncoding validator = ValidateEncoding.getInstance();
90                 
91                 assertEquals(true, validator.validate(mockUriInfo));
92         }
93         
94         private UriInfo getMockUriInfo(String path, MultivaluedMap<String, String> map) {
95                 UriInfo mockUriInfo = Mockito.mock(UriInfo.class);
96                 Mockito.when(mockUriInfo.getPath(false)).thenReturn(path);
97                 Mockito.when(mockUriInfo.getQueryParameters(false)).thenReturn(map);
98                 
99                 return mockUriInfo;
100         }
101         
102 }