Merge "Reformat ONAP-PDP-REST test cases"
[policy/engine.git] / ONAP-SDK-APP / src / test / java / org / onap / portalapp / service / AdminAuthExtensionTest.java
1 /*-
2  * ================================================================================
3  * ONAP Portal SDK
4  * ================================================================================
5  * Copyright (C) 2018 AT&T Intellectual Property
6  * ================================================================================
7  * Modifications Copyright (C) 2019 Samsung
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  * ================================================================================
21  */
22 package org.onap.portalapp.service;
23
24 import static org.junit.Assert.*;
25 import java.util.SortedSet;
26 import java.util.TreeSet;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.mockito.Mockito;
30 import org.onap.policy.rest.dao.CommonClassDao;
31 import org.onap.portalsdk.core.domain.Role;
32 import org.onap.portalsdk.core.domain.User;
33
34 public class AdminAuthExtensionTest {
35
36     private CommonClassDao commonClassDao;
37     private AdminAuthExtension extension;
38     private User user;
39
40
41     @Before
42     public void setUp() {
43         extension = new AdminAuthExtension();
44         commonClassDao = Mockito.mock(CommonClassDao.class);
45         Mockito.doNothing().when(commonClassDao).updateQuery("");
46         Mockito.doNothing().when(commonClassDao).save(new Object());
47         extension.setCommonClassDao(commonClassDao);
48         user = new User();
49         user.setFirstName("Test");
50         user.setLoginId("Test");
51     }
52
53     @Test
54     public void testAdminAuthExtension() {
55         try {
56             callSaveUserFunction("Policy Super Admin");
57             callSaveUserFunction("Policy Super Editor");
58             callSaveUserFunction("Policy Super Guest");
59             callSaveUserFunction("Policy Admin");
60             callSaveUserFunction("Policy Editor");
61             callSaveUserFunction("Policy Guest");
62             extension.editUserExtension(user);
63             extension.saveUserRoleExtension(null, user);
64         } catch (Exception e) {
65             fail("Not Expecting any exception.");
66         }
67     }
68
69     @Test
70     public void expectException() {
71         try {
72             extension.saveUserExtension(null);
73         } catch (Exception e) {
74             assertEquals(NullPointerException.class, e.getClass());
75         }
76     }
77
78     public void callSaveUserFunction(String roleName) {
79         SortedSet<Role> roles = new TreeSet<>();
80         Role role = new Role();
81         role.setName(roleName);
82         roles.add(role);
83         user.setRoles(roles);
84         extension.saveUserExtension(user);
85     }
86 }