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.core.persistence.jpa.inner;
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.HashSet;
29  import java.util.List;
30  import java.util.Map;
31  import java.util.Optional;
32  import java.util.Set;
33  import java.util.stream.Collectors;
34  import org.apache.syncope.common.lib.SyncopeConstants;
35  import org.apache.syncope.common.lib.form.FormPropertyType;
36  import org.apache.syncope.common.lib.types.AnyTypeKind;
37  import org.apache.syncope.common.lib.types.IdRepoEntitlement;
38  import org.apache.syncope.common.lib.types.IdRepoImplementationType;
39  import org.apache.syncope.common.lib.types.ImplementationEngine;
40  import org.apache.syncope.common.lib.types.ResourceOperation;
41  import org.apache.syncope.common.lib.types.TaskType;
42  import org.apache.syncope.core.persistence.api.attrvalue.validation.InvalidEntityException;
43  import org.apache.syncope.core.persistence.api.dao.ExternalResourceDAO;
44  import org.apache.syncope.core.persistence.api.dao.ImplementationDAO;
45  import org.apache.syncope.core.persistence.api.dao.RealmDAO;
46  import org.apache.syncope.core.persistence.api.dao.TaskDAO;
47  import org.apache.syncope.core.persistence.api.dao.UserDAO;
48  import org.apache.syncope.core.persistence.api.entity.ExternalResource;
49  import org.apache.syncope.core.persistence.api.entity.Implementation;
50  import org.apache.syncope.core.persistence.api.entity.task.FormPropertyDef;
51  import org.apache.syncope.core.persistence.api.entity.task.MacroTask;
52  import org.apache.syncope.core.persistence.api.entity.task.MacroTaskCommand;
53  import org.apache.syncope.core.persistence.api.entity.task.PropagationData;
54  import org.apache.syncope.core.persistence.api.entity.task.PropagationTask;
55  import org.apache.syncope.core.persistence.api.entity.task.SchedTask;
56  import org.apache.syncope.core.persistence.api.entity.user.User;
57  import org.apache.syncope.core.persistence.jpa.AbstractTest;
58  import org.apache.syncope.core.spring.security.SyncopeAuthenticationDetails;
59  import org.apache.syncope.core.spring.security.SyncopeGrantedAuthority;
60  import org.identityconnectors.framework.common.objects.Attribute;
61  import org.identityconnectors.framework.common.objects.AttributeBuilder;
62  import org.junit.jupiter.api.Test;
63  import org.springframework.beans.factory.annotation.Autowired;
64  import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
65  import org.springframework.security.core.GrantedAuthority;
66  import org.springframework.security.core.context.SecurityContextHolder;
67  import org.springframework.transaction.annotation.Transactional;
68  
69  @Transactional("Master")
70  public class TaskTest extends AbstractTest {
71  
72      @Autowired
73      private TaskDAO taskDAO;
74  
75      @Autowired
76      private ExternalResourceDAO resourceDAO;
77  
78      @Autowired
79      private UserDAO userDAO;
80  
81      @Autowired
82      private RealmDAO realmDAO;
83  
84      @Autowired
85      private ImplementationDAO implementationDAO;
86  
87      @Test
88      public void findByName() {
89          Optional<SchedTask> task = taskDAO.findByName(TaskType.SCHEDULED, "SampleJob Task");
90          assertTrue(task.isPresent());
91          assertEquals(taskDAO.find(TaskType.SCHEDULED, "e95555d2-1b09-42c8-b25b-f4c4ec597979"), task.get());
92      }
93  
94      @Test
95      public void findWithoutExecs() {
96          List<PropagationTask> tasks = taskDAO.findToExec(TaskType.PROPAGATION);
97          assertNotNull(tasks);
98          assertEquals(3, tasks.size());
99      }
100 
101     @Test
102     public void findPaginated() {
103         List<PropagationTask> tasks = taskDAO.findAll(TaskType.PROPAGATION, null, null, null, null, 1, 2, List.of());
104         assertNotNull(tasks);
105         assertEquals(2, tasks.size());
106 
107         for (PropagationTask task : tasks) {
108             assertNotNull(task);
109         }
110 
111         tasks = taskDAO.findAll(TaskType.PROPAGATION, null, null, null, null, 2, 2, List.of());
112         assertNotNull(tasks);
113         assertEquals(2, tasks.size());
114 
115         for (PropagationTask task : tasks) {
116             assertNotNull(task);
117         }
118 
119         tasks = taskDAO.findAll(TaskType.PROPAGATION, null, null, null, null, 1000, 2, List.of());
120         assertNotNull(tasks);
121         assertTrue(tasks.isEmpty());
122 
123         assertEquals(6, taskDAO.count(TaskType.PROPAGATION, null, null, null, null));
124     }
125 
126     @Test
127     public void findAll() {
128         assertEquals(6, taskDAO.findAll(TaskType.PROPAGATION).size());
129         assertEquals(1, taskDAO.findAll(TaskType.NOTIFICATION).size());
130         assertEquals(3, taskDAO.findAll(TaskType.SCHEDULED).size());
131         assertEquals(10, taskDAO.findAll(TaskType.PULL).size());
132         assertEquals(11, taskDAO.findAll(TaskType.PUSH).size());
133 
134         List<GrantedAuthority> authorities = IdRepoEntitlement.values().stream().
135                 map(entitlement -> new SyncopeGrantedAuthority(entitlement, SyncopeConstants.ROOT_REALM)).
136                 collect(Collectors.toList());
137         UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(
138                 new org.springframework.security.core.userdetails.User(
139                         "admin", "FAKE_PASSWORD", authorities), "FAKE_PASSWORD", authorities);
140         auth.setDetails(new SyncopeAuthenticationDetails(SyncopeConstants.MASTER_DOMAIN, null));
141         SecurityContextHolder.getContext().setAuthentication(auth);
142         try {
143             assertEquals(0, taskDAO.findAll(TaskType.MACRO).size());
144         } finally {
145             SecurityContextHolder.getContext().setAuthentication(null);
146         }
147     }
148 
149     @Test
150     public void savePropagationTask() {
151         ExternalResource resource = resourceDAO.find("ws-target-resource-1");
152         assertNotNull(resource);
153 
154         User user = userDAO.find("74cd8ece-715a-44a4-a736-e17b46c4e7e6");
155         assertNotNull(user);
156 
157         PropagationTask task = entityFactory.newEntity(PropagationTask.class);
158         task.setResource(resource);
159         task.setAnyTypeKind(AnyTypeKind.USER);
160         task.setAnyType(AnyTypeKind.USER.name());
161         task.setOperation(ResourceOperation.CREATE);
162         task.setConnObjectKey("one@two.com");
163 
164         Set<Attribute> attributes = new HashSet<>();
165         attributes.add(AttributeBuilder.build("testAttribute", "testValue1", "testValue2"));
166         attributes.add(AttributeBuilder.buildPassword("password".toCharArray()));
167         task.setPropagationData(new PropagationData(attributes));
168 
169         task = taskDAO.save(task);
170         assertNotNull(task);
171 
172         PropagationTask actual = taskDAO.find(TaskType.PROPAGATION, task.getKey());
173         assertEquals(task, actual);
174     }
175 
176     @Test
177     public void saveMacroTask() throws Exception {
178         MacroTask task = entityFactory.newEntity(MacroTask.class);
179         task.setRealm(realmDAO.getRoot());
180         task.setJobDelegate(implementationDAO.find("MacroJobDelegate"));
181         task.setName("Macro test");
182         task.setContinueOnError(true);
183 
184         Implementation command = entityFactory.newEntity(Implementation.class);
185         command.setKey("command");
186         command.setType(IdRepoImplementationType.COMMAND);
187         command.setEngine(ImplementationEngine.JAVA);
188         command.setBody("clazz");
189         command = implementationDAO.save(command);
190         assertNotNull(command);
191 
192         MacroTaskCommand macroTaskCommand = entityFactory.newEntity(MacroTaskCommand.class);
193         macroTaskCommand.setCommand(command);
194         macroTaskCommand.setMacroTask(task);
195         task.add(macroTaskCommand);
196 
197         FormPropertyDef formPropertyDef = entityFactory.newEntity(FormPropertyDef.class);
198         formPropertyDef.setKey("one");
199         formPropertyDef.setName("One");
200         formPropertyDef.setType(FormPropertyType.Enum);
201         task.add(formPropertyDef);
202 
203         Implementation macroActions = entityFactory.newEntity(Implementation.class);
204         macroActions.setKey("macroActions");
205         macroActions.setType(IdRepoImplementationType.MACRO_ACTIONS);
206         macroActions.setEngine(ImplementationEngine.JAVA);
207         macroActions.setBody("clazz");
208         macroActions = implementationDAO.save(macroActions);
209         assertNotNull(macroActions);
210         task.setMacroAction(macroActions);
211 
212         try {
213             taskDAO.save(task);
214             fail();
215         } catch (InvalidEntityException e) {
216             assertNotNull(e);
217         }
218         formPropertyDef.setEnumValues(Map.of("key", "value"));
219 
220         task = (MacroTask) taskDAO.save(task);
221         assertNotNull(task);
222         assertEquals(1, task.getCommands().size());
223         assertEquals(command, task.getCommands().get(0).getCommand());
224         assertEquals(1, task.getFormPropertyDefs().size());
225         assertEquals(formPropertyDef, task.getFormPropertyDefs().get(0));
226 
227         MacroTask actual = (MacroTask) taskDAO.find(TaskType.MACRO, task.getKey());
228         assertEquals(task, actual);
229     }
230 
231     @Test
232     public void delete() {
233         PropagationTask task = taskDAO.find(TaskType.PROPAGATION, "1e697572-b896-484c-ae7f-0c8f63fcbc6c");
234         assertNotNull(task);
235 
236         ExternalResource resource = task.getResource();
237         assertNotNull(resource);
238 
239         taskDAO.delete(task);
240         task = taskDAO.find(TaskType.PROPAGATION, "1e697572-b896-484c-ae7f-0c8f63fcbc6c");
241         assertNull(task);
242 
243         resource = resourceDAO.find(resource.getKey());
244         assertNotNull(resource);
245         assertFalse(taskDAO.<PropagationTask>findAll(
246                 TaskType.PROPAGATION, resource, null, null, null, -1, -1, List.of()).
247                 contains(task));
248     }
249 }