Adding interfaces in documentation
[aai/sparky-be.git] / 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.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;
44
45 /**
46  * The Class TestUserValidator.
47  */
48 @RunWith(MockitoJUnitRunner.class)
49 public class TestUserValidator {
50
51   @Mock
52   private UserAuthorizationReader userAuthorizationReader;
53
54   @InjectMocks
55   private UserValidator userValidator;
56
57   private static File userAuthFile;
58   private static File missingUserAuthFile;
59
60   /**
61    * Sets the up before class.
62    *
63    * @throws Exception the exception
64    */
65   @BeforeClass
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();
70   }
71
72   /**
73    * The Enum TestData.
74    */
75   enum TestData {
76     // @formatter:off
77     USER_AUTH_FILE(
78         "src/test/resources/user-validator/authorized-users.config"), MISSING_USER_AUTH_FILE(
79             "src/test/resources/user-validator/missing.config");
80
81     private String filename;
82
83     /**
84      * Instantiates a new test data.
85      *
86      * @param filename the filename
87      */
88     TestData(String filename) {
89       this.filename = filename;
90     }
91
92     public String getFilename() {
93       return this.filename;
94     }
95     // @formatter:on
96   }
97
98   /**
99    * Test is authorized user.
100    *
101    * @throws Exception the exception
102    */
103   @Test
104   public void testIsAuthorizedUser() throws Exception {
105     when(userAuthorizationReader.getUsers()).thenCallRealMethod();
106     when(userAuthorizationReader.getUserAuthorizationFile()).thenReturn(userAuthFile);
107
108     boolean isAuthUser = userValidator.isAuthorizedUser("user1");
109     assertThat(isAuthUser, is(true));
110
111     boolean isAuthUser2 = userValidator.isAuthorizedUser("user2");
112     assertThat(isAuthUser2, is(false));
113
114     boolean isAuthUser3 = userValidator.isAuthorizedUser("user3");
115     assertThat(isAuthUser3, is(false));
116
117     boolean isAuthUser4 = userValidator.isAuthorizedUser("not-in-file");
118     assertThat(isAuthUser4, is(false));
119
120     boolean isAuthUser5 = userValidator.isAuthorizedUser("user4");
121     assertThat(isAuthUser5, is(true));
122   }
123
124   /**
125    * Test not authorized if file not present.
126    *
127    * @throws Exception the exception
128    */
129   @Test
130   public void testNotAuthorizedIfFileNotPresent() throws Exception {
131     when(userAuthorizationReader.getUsers()).thenCallRealMethod();
132     when(userAuthorizationReader.getUserAuthorizationFile()).thenReturn(missingUserAuthFile);
133
134     boolean isAuthUser = userValidator.isAuthorizedUser("user1");
135     assertThat(isAuthUser, is(false));
136   }
137 }