Add multi-oxm support to sparky-be
[aai/sparky-be.git] / sparkybe-onap-service / src / test / java / org / onap / aai / sparky / editattributes / TestUserValidator.java
1 /**
2  * ============LICENSE_START===================================================
3  * SPARKY (AAI UI service)
4  * ============================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
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
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
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=====================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25
26 package org.onap.aai.sparky.editattributes;
27
28 import static org.hamcrest.Matchers.is;
29 import static org.junit.Assert.assertThat;
30 import static org.mockito.Mockito.when;
31
32 import java.io.File;
33 import java.nio.file.Paths;
34
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;
43
44 /**
45  * The Class TestUserValidator.
46  */
47 @RunWith(MockitoJUnitRunner.class)
48 public class TestUserValidator {
49
50   @Mock
51   private UserAuthorizationReader userAuthorizationReader;
52
53   @InjectMocks
54   private UserValidator userValidator;
55
56   private static File userAuthFile;
57   private static File missingUserAuthFile;
58
59   /**
60    * Sets the up before class.
61    *
62    * @throws Exception the exception
63    */
64   @BeforeClass
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();
68   }
69
70   /**
71    * The Enum TestData.
72    */
73   enum TestData {
74     // @formatter:off
75     USER_AUTH_FILE(
76         "src/test/resources/user-validator/authorized-users.config"), MISSING_USER_AUTH_FILE(
77             "src/test/resources/user-validator/missing.config");
78
79     private String filename;
80
81     /**
82      * Instantiates a new test data.
83      *
84      * @param filename the filename
85      */
86     TestData(String filename) {
87       this.filename = filename;
88     }
89
90     public String getFilename() {
91       return this.filename;
92     }
93     // @formatter:on
94   }
95
96   /**
97    * Test is authorized user.
98    *
99    * @throws Exception the exception
100    */
101   @Test
102   public void testIsAuthorizedUser() throws Exception {
103     when(userAuthorizationReader.getUsers()).thenCallRealMethod();
104     when(userAuthorizationReader.getUserAuthorizationFile()).thenReturn(userAuthFile);
105
106     boolean isAuthUser = userValidator.isAuthorizedUser("user1");
107     assertThat(isAuthUser, is(true));
108
109     boolean isAuthUser2 = userValidator.isAuthorizedUser("user2");
110     assertThat(isAuthUser2, is(false));
111
112     boolean isAuthUser3 = userValidator.isAuthorizedUser("user3");
113     assertThat(isAuthUser3, is(false));
114
115     boolean isAuthUser4 = userValidator.isAuthorizedUser("not-in-file");
116     assertThat(isAuthUser4, is(false));
117
118     boolean isAuthUser5 = userValidator.isAuthorizedUser("user4");
119     assertThat(isAuthUser5, is(true));
120   }
121
122   /**
123    * Test not authorized if file not present.
124    *
125    * @throws Exception the exception
126    */
127   @Test
128   public void testNotAuthorizedIfFileNotPresent() throws Exception {
129     when(userAuthorizationReader.getUsers()).thenCallRealMethod();
130     when(userAuthorizationReader.getUserAuthorizationFile()).thenReturn(missingUserAuthFile);
131
132     boolean isAuthUser = userValidator.isAuthorizedUser("user1");
133     assertThat(isAuthUser, is(false));
134   }
135 }