0b61af8df6ea2ad8e5ee46c9edd120ad57458d83
[portal/sdk.git] /
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal SDK
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software 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  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.onap.portalsdk.core.service;
39
40 import java.io.IOException;
41 import java.util.List;
42
43 import org.junit.Assert;
44 import org.junit.Test;
45 import org.junit.runner.RunWith;
46 import org.mockito.InjectMocks;
47 import org.mockito.Mock;
48 import org.mockito.Mockito;
49 import org.onap.portalsdk.core.domain.Role;
50 import org.onap.portalsdk.core.domain.RoleFunction;
51 import org.powermock.modules.junit4.PowerMockRunner;
52
53 @RunWith(PowerMockRunner.class)
54 public class RoleServiceCentralizedAccessTest {
55
56         @InjectMocks
57         private RoleServiceCentralizedAccess roleServiceCntrlAccess;
58         
59         @Mock
60         private RestApiRequestBuilder restApiRequestBuilder;
61
62         @Test
63         public void getRoleFunctionsTest() throws Exception {
64                 String loginId ="1234";
65                 String response ="[    {        \"code\" : \"abc\",        \"name\" : \"xyz\"    },    {        \"code\" : \"pqr\",        \"name\" : \"str\"    } ]";
66                 Mockito.when(restApiRequestBuilder.getViaREST("/functions", true, loginId)).thenReturn(response);
67                 List<RoleFunction> roleFunctions = roleServiceCntrlAccess.getRoleFunctions(loginId);
68                 Assert.assertTrue(roleFunctions.size() > 0);
69         }
70         
71         @Test
72         public void getAvailableChildRolesWithEmptyRoleIdTest() throws Exception {
73                 String loginId = "123";
74                 Long roleId = null;
75                 String response ="[    {        \"active\" : true,        \"name\" : \"xyz\"    } ]";
76                 Mockito.when(restApiRequestBuilder.getViaREST("/roles", true, loginId)).thenReturn(response);
77                 List<Role> roles = roleServiceCntrlAccess.getAvailableChildRoles(loginId, roleId);
78                 Assert.assertNotNull(roles);
79         }
80         
81         @Test
82         public void getAvailableChildRolesWithZeroRoleIdTest() throws Exception {
83                 String loginId = "123";
84                 Long roleId = 0L;
85                 String response ="[    {        \"active\" : true,        \"name\" : \"xyz\"    } ]";
86                 Mockito.when(restApiRequestBuilder.getViaREST("/roles", true, loginId)).thenReturn(response);
87                 List<Role> roles = roleServiceCntrlAccess.getAvailableChildRoles(loginId, roleId);
88                 Assert.assertNotNull(roles);
89         }
90         
91         @Test
92         public void getAvailableChildRolesTest() throws Exception {
93                 String loginId = "123";
94                 Long roleId = 123L;
95                 String response ="[    {        \"active\" : false,        \"name\" : \"xyz\"    } ]";
96                 Mockito.when(restApiRequestBuilder.getViaREST("/roles", true, loginId)).thenReturn(response);
97                 String roleResponse =" {        \"active\" : true,        \"name\" : \"xyz\", \"roleFunctions\" : [    {        \"code\" : \"abc\",        \"name\" : \"RF1\"    },    {        \"code\" : \"pqr\",        \"name\" : \"RF2\"    } ]    ,  \"parentRoles\": [   {\"active\" : false,        \"name\" : \"XYZ-ABC\"}, {\"active\" : true,        \"name\" : \"ABC\"}  ]    } ";
98                 Mockito.when(restApiRequestBuilder.getViaREST("/role/" + roleId, true, loginId)).thenReturn(roleResponse);
99                 roleServiceCntrlAccess.getAvailableChildRoles(loginId, roleId);
100                 Assert.assertTrue(true);
101         }
102         
103         @Test
104         public void saveRoleTest() throws Exception {
105                 Role role = new Role();
106                 role.setName("Role");
107                 roleServiceCntrlAccess.saveRole("123", role);
108                 Assert.assertTrue(true);
109         }
110         
111         @Test
112         public void deleteRoleTest() throws Exception {
113                 Role role = new Role();
114                 role.setName("Role");
115                 role.setId(123l);
116                 roleServiceCntrlAccess.deleteRole("123", role);
117                 Assert.assertTrue(true);
118         }
119         
120         @Test
121         public void getActiveRolesTest() throws Exception {
122                 String requestedLoginId ="1234";
123                 String response ="[    {        \"active\" : true,        \"name\" : \"role1\"    }, {        \"active\" : false,        \"name\" : \"role2\"    } ]";
124                 Mockito.when(restApiRequestBuilder.getViaREST("/activeRoles", true, requestedLoginId)).thenReturn(response);
125                 List<Role> roles = roleServiceCntrlAccess.getActiveRoles(requestedLoginId);
126                 Assert.assertNotNull(roles);
127         }
128         
129         @Test
130         public void getRoleFunctionTest() throws IOException {
131                 String requestedLoginId = "xyz";
132                 String code ="abc";
133                 
134                 String responseString = " {        \"code\" : \"abc\",        \"name\" : \"xyz\"    }";
135                 Mockito.when(restApiRequestBuilder.getViaREST("/function/" + code, true, requestedLoginId)).thenReturn(responseString);
136                 RoleFunction roleFunction = roleServiceCntrlAccess.getRoleFunction(requestedLoginId, code);
137                 Assert.assertNotNull(roleFunction);
138         }
139         
140         @Test
141         public void saveRoleFunctionTest() throws IOException {
142                 String requestedLoginId ="123";
143                 RoleFunction domainRoleFunction = new RoleFunction();
144                 domainRoleFunction.setId(1234L);
145                 roleServiceCntrlAccess.saveRoleFunction(requestedLoginId, domainRoleFunction);
146                 Assert.assertTrue(true);
147         }
148
149         @Test
150         public void deleteRoleFunctionTest() throws IOException {
151                 String requestedLoginId ="123";
152                 RoleFunction domainRoleFunction = new RoleFunction();
153                 domainRoleFunction.setId(1234L);
154                 roleServiceCntrlAccess.deleteRoleFunction(requestedLoginId, domainRoleFunction);
155                 Assert.assertTrue(true);
156         }
157
158         @Test
159         public void deleteDependcyRoleRecord() throws IOException {
160                 String requestedLoginId = "123";
161                 Long id = 123L;
162                 roleServiceCntrlAccess.deleteDependcyRoleRecord(requestedLoginId, id);
163                 Assert.assertTrue(true);
164         }
165 }