View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.fit.core;
20  
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertFalse;
23  import static org.junit.jupiter.api.Assertions.assertNotNull;
24  import static org.junit.jupiter.api.Assertions.assertNull;
25  import static org.junit.jupiter.api.Assertions.assertTrue;
26  import static org.junit.jupiter.api.Assertions.fail;
27  
28  import java.util.UUID;
29  import java.util.stream.Collectors;
30  import javax.ws.rs.core.Response;
31  import org.apache.syncope.common.lib.SyncopeClientException;
32  import org.apache.syncope.common.lib.to.ApplicationTO;
33  import org.apache.syncope.common.lib.to.PrivilegeTO;
34  import org.apache.syncope.common.lib.to.RoleTO;
35  import org.apache.syncope.common.lib.types.ClientExceptionType;
36  import org.apache.syncope.common.rest.api.service.ApplicationService;
37  import org.apache.syncope.common.rest.api.service.RoleService;
38  import org.apache.syncope.fit.AbstractITCase;
39  import org.junit.jupiter.api.Test;
40  
41  public class ApplicationITCase extends AbstractITCase {
42  
43      @Test
44      public void read() {
45          ApplicationTO mightyApp = APPLICATION_SERVICE.read("mightyApp");
46          assertNotNull(mightyApp);
47          assertEquals(2, mightyApp.getPrivileges().size());
48          assertTrue(mightyApp.getPrivileges().stream().anyMatch(privilege -> "postMighty".equals(privilege.getKey())));
49  
50          PrivilegeTO getMighty = APPLICATION_SERVICE.readPrivilege("getMighty");
51          assertNotNull(getMighty);
52          assertEquals("mightyApp", getMighty.getApplication());
53  
54          RoleTO role = ROLE_SERVICE.read("Other");
55          assertFalse(role.getPrivileges().isEmpty());
56          assertEquals(1, role.getPrivileges().size());
57          assertTrue(role.getPrivileges().stream().anyMatch("postMighty"::equals));
58      }
59  
60      @Test
61      public void crud() {
62          // 1. create application
63          ApplicationTO application = new ApplicationTO();
64          application.setKey(UUID.randomUUID().toString());
65  
66          PrivilegeTO privilegeTO = new PrivilegeTO();
67          privilegeTO.setKey(UUID.randomUUID().toString());
68          privilegeTO.setSpec("{ \"one\": true }");
69          application.getPrivileges().add(privilegeTO);
70  
71          privilegeTO = new PrivilegeTO();
72          privilegeTO.setKey(UUID.randomUUID().toString());
73          privilegeTO.setSpec("{ \"two\": true }");
74          application.getPrivileges().add(privilegeTO);
75  
76          privilegeTO = new PrivilegeTO();
77          privilegeTO.setKey(UUID.randomUUID().toString());
78          privilegeTO.setSpec("{ \"three\": true }");
79          application.getPrivileges().add(privilegeTO);
80  
81          Response response = APPLICATION_SERVICE.create(application);
82          assertEquals(Response.Status.CREATED.getStatusCode(), response.getStatusInfo().getStatusCode());
83  
84          application = getObject(response.getLocation(), ApplicationService.class, ApplicationTO.class);
85          assertNotNull(application);
86          assertNull(application.getDescription());
87          assertEquals(3, application.getPrivileges().size());
88  
89          // 2. update application
90          application.setDescription("A description");
91          application.getPrivileges().remove(1);
92  
93          APPLICATION_SERVICE.update(application);
94  
95          application = APPLICATION_SERVICE.read(application.getKey());
96          assertNotNull(application);
97          assertNotNull(application.getDescription());
98          assertEquals(2, application.getPrivileges().size());
99  
100         // 3. assign application's privileges to a new role
101         RoleTO role = new RoleTO();
102         role.setKey("privileged");
103         role.getPrivileges().addAll(
104                 application.getPrivileges().stream().map(PrivilegeTO::getKey).collect(Collectors.toList()));
105 
106         response = ROLE_SERVICE.create(role);
107         assertEquals(Response.Status.CREATED.getStatusCode(), response.getStatusInfo().getStatusCode());
108 
109         role = getObject(response.getLocation(), RoleService.class, RoleTO.class);
110         assertNotNull(role);
111         assertEquals(2, role.getPrivileges().size());
112 
113         // 4. delete application => delete privileges
114         APPLICATION_SERVICE.delete(application.getKey());
115 
116         try {
117             APPLICATION_SERVICE.read(application.getKey());
118             fail("This should not happen");
119         } catch (SyncopeClientException e) {
120             assertEquals(ClientExceptionType.NotFound, e.getType());
121         }
122 
123         role = ROLE_SERVICE.read(role.getKey());
124         assertNotNull(role);
125         assertTrue(role.getPrivileges().isEmpty());
126     }
127 }