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.junit.BeforeClass;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.mockito.InjectMocks;
39 import org.mockito.Mock;
40 import org.mockito.runners.MockitoJUnitRunner;
41 import org.onap.aai.sparky.editattributes.UserAuthorizationReader;
42 import org.onap.aai.sparky.editattributes.UserValidator;
45 * The Class TestUserValidator.
47 @RunWith(MockitoJUnitRunner.class)
48 public class TestUserValidator {
51 private UserAuthorizationReader userAuthorizationReader;
54 private UserValidator userValidator;
56 private static File userAuthFile;
57 private static File missingUserAuthFile;
60 * Sets the up before class.
62 * @throws Exception the exception
65 public static void setUpBeforeClass() throws Exception {
66 userAuthFile = Paths.get(TestData.USER_AUTH_FILE.getFilename()).toFile();
67 missingUserAuthFile = Paths.get(TestData.MISSING_USER_AUTH_FILE.getFilename()).toFile();
76 "src/test/resources/user-validator/authorized-users.config"), MISSING_USER_AUTH_FILE(
77 "src/test/resources/user-validator/missing.config");
79 private String filename;
82 * Instantiates a new test data.
84 * @param filename the filename
86 TestData(String filename) {
87 this.filename = filename;
90 public String getFilename() {
97 * Test is authorized user.
99 * @throws Exception the exception
102 public void testIsAuthorizedUser() throws Exception {
103 when(userAuthorizationReader.getUsers()).thenCallRealMethod();
104 when(userAuthorizationReader.getUserAuthorizationFile()).thenReturn(userAuthFile);
106 boolean isAuthUser = userValidator.isAuthorizedUser("user1");
107 assertThat(isAuthUser, is(true));
109 boolean isAuthUser2 = userValidator.isAuthorizedUser("user2");
110 assertThat(isAuthUser2, is(false));
112 boolean isAuthUser3 = userValidator.isAuthorizedUser("user3");
113 assertThat(isAuthUser3, is(false));
115 boolean isAuthUser4 = userValidator.isAuthorizedUser("not-in-file");
116 assertThat(isAuthUser4, is(false));
118 boolean isAuthUser5 = userValidator.isAuthorizedUser("user4");
119 assertThat(isAuthUser5, is(true));
123 * Test not authorized if file not present.
125 * @throws Exception the exception
128 public void testNotAuthorizedIfFileNotPresent() throws Exception {
129 when(userAuthorizationReader.getUsers()).thenCallRealMethod();
130 when(userAuthorizationReader.getUserAuthorizationFile()).thenReturn(missingUserAuthFile);
132 boolean isAuthUser = userValidator.isAuthorizedUser("user1");
133 assertThat(isAuthUser, is(false));