Initial commit for AAI-UI(sparky-backend)
[aai/sparky-be.git] / src / test / java / org / openecomp / sparky / security / portal / TestPortalRestAPIServiceImpl.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.openecomp.sparky.security.portal;
27
28 import static org.hamcrest.Matchers.empty;
29 import static org.hamcrest.Matchers.is;
30 import static org.hamcrest.Matchers.nullValue;
31 import static org.junit.Assert.assertThat;
32 import static org.mockito.Mockito.when;
33
34 import java.io.File;
35 import java.nio.file.Files;
36 import java.nio.file.Paths;
37 import java.util.ArrayList;
38 import java.util.HashSet;
39 import java.util.LinkedHashSet;
40 import java.util.List;
41 import java.util.Set;
42
43 import javax.servlet.http.HttpServletRequest;
44
45 import org.junit.After;
46 import org.junit.AfterClass;
47 import org.junit.Before;
48 import org.junit.BeforeClass;
49 import org.junit.Test;
50 import org.junit.runner.RunWith;
51 import org.mockito.InjectMocks;
52 import org.mockito.Mock;
53 import org.mockito.Mockito;
54 import org.openecomp.portalsdk.core.onboarding.exception.PortalAPIException;
55 import org.openecomp.portalsdk.core.restful.domain.EcompRole;
56 import org.openecomp.portalsdk.core.restful.domain.EcompUser;
57 import org.openecomp.sparky.security.portal.config.PortalAuthenticationConfig;
58 import org.openecomp.sparky.security.portal.config.RolesConfig;
59 import org.powermock.core.classloader.annotations.PowerMockIgnore;
60 import org.powermock.core.classloader.annotations.PrepareForTest;
61 import org.powermock.modules.junit4.PowerMockRunner;
62 import org.powermock.reflect.Whitebox;
63
64 @PowerMockIgnore({ "javax.crypto.*" })
65 @RunWith(PowerMockRunner.class)
66 @PrepareForTest({ PortalAuthenticationConfig.class, RolesConfig.class })
67 public class TestPortalRestAPIServiceImpl {
68
69   private static File testUsersFile;
70   private static final String LOGINID_1 = "200";
71   private static final String LOGINID_2 = "201";
72   private static final String VIEW_ROLE = "View";
73
74   enum TestData {
75     // @formatter:off
76     TEST_USERS                       ("src/test/resources/portal/test-users.config"),
77     PORTAL_AUTHENTICATION_PROPERTIES ("src/test/resources/portal/portal-authentication.properties"),
78     ROLES_CONFIG_FILE                ("src/test/resources/portal/roles.config");
79
80     private String filename;
81     TestData(String filename) {this.filename = filename;}
82     public String getFilename() {return this.filename;}
83     // @formatter:on
84   }
85
86   @Mock
87   private UserManager userManager = new UserManager(testUsersFile);
88
89   @InjectMocks
90   private PortalRestAPIServiceImpl portalApi = new PortalRestAPIServiceImpl();
91
92   @BeforeClass
93   public static void setUpBeforeClass() throws Exception {
94     testUsersFile = Paths.get(TestData.TEST_USERS.getFilename()).toFile();
95   }
96
97   @AfterClass
98   public static void tearDownAfterClass() throws Exception {
99     Files.deleteIfExists(testUsersFile.toPath());
100   }
101
102   @Before
103   public void setUp() throws Exception {
104     Whitebox.setInternalState(RolesConfig.class, "ROLES_CONFIG_FILE",
105         TestData.ROLES_CONFIG_FILE.getFilename());
106   }
107
108   @After
109   public void tearDown() throws Exception {
110     Files.deleteIfExists(testUsersFile.toPath());
111   }
112
113   @Test
114   public void testPushAndGetUser() throws Exception {
115     EcompUser user = new EcompUser();
116     user.setLoginId(LOGINID_1);
117
118     portalApi.pushUser(user);
119     EcompUser storedUser = portalApi.getUser(user.getLoginId());
120
121     assertThat(storedUser.getLoginId(), is(user.getLoginId()));
122   }
123
124   @Test(expected = PortalAPIException.class)
125   public void testCannotPushUserTwice() throws Exception {
126     EcompUser user = new EcompUser();
127     user.setLoginId(LOGINID_1);
128
129     portalApi.pushUser(user);
130     portalApi.pushUser(user);
131   }
132
133   @Test(expected = PortalAPIException.class)
134   public void testGetUnknownUser() throws Exception {
135     EcompUser user = new EcompUser();
136     user.setLoginId(LOGINID_1);
137     portalApi.pushUser(user);
138
139     portalApi.getUser("does-not-exist");
140   }
141
142   @Test
143   public void testGetUsers() throws Exception {
144     EcompUser user = new EcompUser();
145     user.setLoginId(LOGINID_1);
146
147     EcompUser user2 = new EcompUser();
148     user2.setLoginId(LOGINID_2);
149
150     portalApi.pushUser(user);
151     portalApi.pushUser(user2);
152
153     List<EcompUser> users = portalApi.getUsers();
154
155     assertThat(users.size(), is(2));
156     assertThat(users.get(0).getLoginId(), is(LOGINID_1));
157     assertThat(users.get(1).getLoginId(), is(LOGINID_2));
158   }
159
160   @Test
161   public void testEditUser() throws Exception {
162     EcompUser user = new EcompUser();
163     user.setLoginId(LOGINID_1);
164     user.setFirstName("Bob");
165
166     portalApi.pushUser(user);
167
168     user.setFirstName("Jen");
169     portalApi.editUser(LOGINID_1, user);
170
171     assertThat(portalApi.getUser(LOGINID_1).getFirstName(), is("Jen"));
172   }
173
174   @Test(expected = PortalAPIException.class)
175   public void testEditUnknowUser() throws Exception {
176     EcompUser user = new EcompUser();
177     user.setLoginId(LOGINID_1);
178     portalApi.pushUser(user);
179
180     portalApi.editUser("does-no-exist", new EcompUser());
181   }
182
183   @Test
184   public void testGetRoles() throws Exception {
185     EcompUser user = new EcompUser();
186     user.setLoginId(LOGINID_1);
187     user.setRoles(new HashSet<>(portalApi.getAvailableRoles()));
188
189     portalApi.pushUser(user);
190
191     List<EcompRole> userRoles = portalApi.getUserRoles(LOGINID_1);
192
193     assertThat(userRoles.size(), is(1));
194     assertThat(userRoles.get(0).getId(), is(1L));
195     assertThat(userRoles.get(0).getName(), is(VIEW_ROLE));
196   }
197
198   @Test
199   public void testPushUserRoles() throws Exception {
200     EcompUser user = new EcompUser();
201     user.setLoginId(LOGINID_1);
202     portalApi.pushUser(user);
203
204     EcompUser storedUser = portalApi.getUser(LOGINID_1);
205     assertThat(storedUser.getRoles(), nullValue());
206
207     portalApi.pushUserRole(LOGINID_1, UserManager.getRoles());
208
209     Set<EcompRole> storedUserRoles = portalApi.getUser(LOGINID_1).getRoles();
210     ArrayList<EcompRole> rolesList = new ArrayList<>(storedUserRoles);
211
212     assertThat(rolesList.size(), is(1));
213     assertThat(rolesList.get(0).getId(), is(1L));
214     assertThat(rolesList.get(0).getName(), is(VIEW_ROLE));
215   }
216
217   @Test
218   public void testCannotPushRoleTwice() throws Exception {
219     EcompUser user = new EcompUser();
220     user.setLoginId(LOGINID_1);
221     portalApi.pushUser(user);
222
223     EcompUser storedUser = portalApi.getUser(LOGINID_1);
224     assertThat(storedUser.getRoles(), nullValue());
225
226     portalApi.pushUserRole(LOGINID_1, UserManager.getRoles());
227     portalApi.pushUserRole(LOGINID_1, UserManager.getRoles());
228
229     Set<EcompRole> storedUserRoles = portalApi.getUser(LOGINID_1).getRoles();
230     ArrayList<EcompRole> rolesList = new ArrayList<>(storedUserRoles);
231
232     assertThat(rolesList.size(), is(1));
233     assertThat(rolesList.get(0).getId(), is(1L));
234     assertThat(rolesList.get(0).getName(), is(VIEW_ROLE));
235   }
236
237   @Test
238   public void testDeleteUserRoles() throws Exception {
239     EcompUser user = new EcompUser();
240     user.setLoginId(LOGINID_1);
241     user.setFirstName("Bob");
242     List<EcompRole> availableRoles = portalApi.getAvailableRoles();
243     user.setRoles(new LinkedHashSet<EcompRole>(availableRoles));
244
245     portalApi.pushUser(user);
246
247     portalApi.pushUserRole(LOGINID_1, new ArrayList<EcompRole>());
248
249     EcompUser userWithNoRoles = portalApi.getUser(LOGINID_1);
250
251     assertThat(userWithNoRoles.getRoles(), empty());
252   }
253
254   @Test
255   public void testPushNullRoles() throws Exception {
256     EcompUser user = new EcompUser();
257     user.setLoginId(LOGINID_1);
258     user.setFirstName("Bob");
259     List<EcompRole> availableRoles = portalApi.getAvailableRoles();
260     user.setRoles(new LinkedHashSet<EcompRole>(availableRoles));
261
262     portalApi.pushUser(user);
263     portalApi.pushUserRole(LOGINID_1, null);
264
265     EcompUser userWithNoRoles = portalApi.getUser(LOGINID_1);
266
267     assertThat(userWithNoRoles.getRoles(), empty());
268   }
269 }