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=========================================================
20 package org.onap.aai.rest.util;
22 import static org.junit.Assert.*;
24 import java.io.UnsupportedEncodingException;
26 import javax.ws.rs.core.MultivaluedHashMap;
27 import javax.ws.rs.core.MultivaluedMap;
28 import javax.ws.rs.core.UriInfo;
30 import org.junit.Test;
31 import org.mockito.Mockito;
33 public class ValidateEncodingTest {
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();
42 assertEquals(false, validator.validate(mockUriInfo));
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();
51 assertEquals(true, validator.validate(mockUriInfo));
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);
62 ValidateEncoding validator = ValidateEncoding.getInstance();
64 assertEquals(false, validator.validate(mockUriInfo));
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);
75 ValidateEncoding validator = ValidateEncoding.getInstance();
77 assertEquals(false, validator.validate(mockUriInfo));
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);
87 ValidateEncoding validator = ValidateEncoding.getInstance();
89 assertEquals(true, validator.validate(mockUriInfo));
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);