2 * ============LICENSE_START===================================================
3 * SPARKY (AAI UI service)
4 * ============================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
8 * ============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=====================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
26 package org.onap.aai.sparky.editattributes;
28 import static org.hamcrest.Matchers.is;
29 import static org.junit.Assert.assertThat;
30 import static org.mockito.Mockito.when;
33 import java.nio.file.Paths;
35 import org.apache.log4j.BasicConfigurator;
36 import org.junit.BeforeClass;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.mockito.InjectMocks;
40 import org.mockito.Mock;
41 import org.mockito.runners.MockitoJUnitRunner;
42 import org.onap.aai.sparky.editattributes.UserAuthorizationReader;
43 import org.onap.aai.sparky.editattributes.UserValidator;
46 * The Class TestUserValidator.
48 @RunWith(MockitoJUnitRunner.class)
49 public class TestUserValidator {
52 private UserAuthorizationReader userAuthorizationReader;
55 private UserValidator userValidator;
57 private static File userAuthFile;
58 private static File missingUserAuthFile;
61 * Sets the up before class.
63 * @throws Exception the exception
66 public static void setUpBeforeClass() throws Exception {
67 BasicConfigurator.configure();
68 userAuthFile = Paths.get(TestData.USER_AUTH_FILE.getFilename()).toFile();
69 missingUserAuthFile = Paths.get(TestData.MISSING_USER_AUTH_FILE.getFilename()).toFile();
78 "src/test/resources/user-validator/authorized-users.config"), MISSING_USER_AUTH_FILE(
79 "src/test/resources/user-validator/missing.config");
81 private String filename;
84 * Instantiates a new test data.
86 * @param filename the filename
88 TestData(String filename) {
89 this.filename = filename;
92 public String getFilename() {
99 * Test is authorized user.
101 * @throws Exception the exception
104 public void testIsAuthorizedUser() throws Exception {
105 when(userAuthorizationReader.getUsers()).thenCallRealMethod();
106 when(userAuthorizationReader.getUserAuthorizationFile()).thenReturn(userAuthFile);
108 boolean isAuthUser = userValidator.isAuthorizedUser("user1");
109 assertThat(isAuthUser, is(true));
111 boolean isAuthUser2 = userValidator.isAuthorizedUser("user2");
112 assertThat(isAuthUser2, is(false));
114 boolean isAuthUser3 = userValidator.isAuthorizedUser("user3");
115 assertThat(isAuthUser3, is(false));
117 boolean isAuthUser4 = userValidator.isAuthorizedUser("not-in-file");
118 assertThat(isAuthUser4, is(false));
120 boolean isAuthUser5 = userValidator.isAuthorizedUser("user4");
121 assertThat(isAuthUser5, is(true));
125 * Test not authorized if file not present.
127 * @throws Exception the exception
130 public void testNotAuthorizedIfFileNotPresent() throws Exception {
131 when(userAuthorizationReader.getUsers()).thenCallRealMethod();
132 when(userAuthorizationReader.getUserAuthorizationFile()).thenReturn(missingUserAuthFile);
134 boolean isAuthUser = userValidator.isAuthorizedUser("user1");
135 assertThat(isAuthUser, is(false));