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