Add unit test for DashboardUserManager
[nonrtric.git] / dashboard / webapp-backend / src / test / java / org / oransc / ric / portal / dashboard / DashboardUserManagerTest.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 AT&T Intellectual Property
6  * %%
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ========================LICENSE_END===================================
19  */
20 package org.oransc.ric.portal.dashboard;
21
22 import java.lang.invoke.MethodHandles;
23 import java.util.HashSet;
24 import java.util.Set;
25
26 import org.junit.Assert;
27 import org.junit.jupiter.api.Test;
28 import org.onap.portalsdk.core.onboarding.exception.PortalAPIException;
29 import org.onap.portalsdk.core.restful.domain.EcompRole;
30 import org.onap.portalsdk.core.restful.domain.EcompUser;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.test.context.ActiveProfiles;
34
35 @ActiveProfiles("test")
36 public class DashboardUserManagerTest {
37
38     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
39
40     public static EcompUser createEcompUser(String loginId) {
41         EcompUser user = new EcompUser();
42         user.setActive(true);
43         user.setLoginId(loginId);
44         user.setFirstName("First");
45         user.setLastName("Last");
46         EcompRole role = new EcompRole();
47         role.setId(1L);
48         role.setName(DashboardConstants.ROLE_NAME_ADMIN);
49         Set<EcompRole> roles = new HashSet<>();
50         roles.add(role);
51         user.setRoles(roles);
52         return user;
53     }
54
55     @Test
56     public void testUserMgr() throws Exception {
57         final String loginId = "demo";
58         DashboardUserManager dum = new DashboardUserManager(true);
59         EcompUser user = createEcompUser(loginId);
60         dum.createUser(user);
61         logger.info("Created user {}", user);
62         try {
63             dum.createUser(user);
64             throw new Exception("Unexpected success");
65         } catch (PortalAPIException ex) {
66             logger.info("caught expected exception: {}", ex.toString());
67         }
68         Assert.assertFalse(dum.getUsers().isEmpty());
69         EcompUser fetched = dum.getUser(loginId);
70         Assert.assertEquals(fetched, user);
71         fetched.setLastName("Lastier");
72         dum.updateUser(loginId, fetched);
73         EcompUser missing = dum.getUser("foo");
74         Assert.assertNull(missing);
75         EcompUser unk = createEcompUser("unknown");
76         try {
77             dum.updateUser("unk", unk);
78         } catch (PortalAPIException ex) {
79             logger.info("caught expected exception: {}", ex.toString());
80         }
81     }
82
83 }