2 * ============LICENSE_START=======================================================
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
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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 package org.onap.aai.rest.util;
24 import static org.junit.Assert.*;
26 import java.io.UnsupportedEncodingException;
28 import javax.ws.rs.core.MultivaluedHashMap;
29 import javax.ws.rs.core.MultivaluedMap;
30 import javax.ws.rs.core.UriInfo;
32 import org.junit.Test;
33 import org.mockito.Mockito;
35 public class ValidateEncodingTest {
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();
44 assertEquals(false, validator.validate(mockUriInfo));
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();
53 assertEquals(true, validator.validate(mockUriInfo));
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);
64 ValidateEncoding validator = ValidateEncoding.getInstance();
66 assertEquals(false, validator.validate(mockUriInfo));
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);
77 ValidateEncoding validator = ValidateEncoding.getInstance();
79 assertEquals(false, validator.validate(mockUriInfo));
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);
89 ValidateEncoding validator = ValidateEncoding.getInstance();
91 assertEquals(true, validator.validate(mockUriInfo));
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);