2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2019 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=========================================================
21 package org.onap.vid.controller;
24 import org.junit.Assert;
25 import org.mockito.Mockito;
26 import org.onap.vid.controller.filter.ClientCredentialsFilter;
27 import org.testng.annotations.DataProvider;
28 import org.testng.annotations.Test;
30 import javax.servlet.FilterChain;
31 import javax.servlet.ServletException;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpServletResponse;
34 import java.io.IOException;
36 import static org.mockito.ArgumentMatchers.any;
40 * Created by amichai on 16/05/2018.
42 public class ClientCredentialsFilterTest {
45 public static Object[][] authorizedData() {
46 return new Object[][] {
47 {"Basic 123==", null},
51 {"Basic 123==", "Basic 123=="}
56 public static Object[][] notAuthorizedData() {
57 return new Object[][] {
58 {null, "Basic 123=="},
60 {"not null but not as expected", "Basic 123=="},
61 {"basic 123==", "Basic 123=="}
66 public static Object[][] clientVerified() {
67 return new Object[][] {
73 @Test(dataProvider = "authorizedData")
74 public void givenAuthorizationHeader_Authorized(String actualAuth, String expectedAuth){
75 ClientCredentialsFilter filter = new ClientCredentialsFilter();
76 Assert.assertTrue(filter.verifyClientCredentials(actualAuth, expectedAuth));
79 @Test(dataProvider = "notAuthorizedData")
80 public void givenAuthorizationHeader_NotAuthorized(String actualAuth, String expectedAuth){
81 ClientCredentialsFilter filter = new ClientCredentialsFilter();
82 Assert.assertFalse(filter.verifyClientCredentials(actualAuth, expectedAuth));
85 //@Test(dataProvider = "clientVerified")
86 public void notAuthorized_return401(Boolean clientVerified) throws IOException, ServletException {
87 ClientCredentialsFilter filter = Mockito.mock(ClientCredentialsFilter.class);
88 HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
89 HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
90 FilterChain chain = Mockito.mock(FilterChain.class);
93 Mockito.when(filter.verifyClientCredentials(any(),any())).thenReturn(clientVerified);
94 Mockito.doNothing().when(response).sendError(401);
96 Mockito.doCallRealMethod().when(filter).doFilter(request,response,chain);
97 filter.doFilter(request,response,chain);
101 Mockito.verify(chain).doFilter(request,response);
105 Mockito.verify(response).sendError(401);