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  
27  import java.util.HashSet;
28  import java.util.List;
29  import java.util.Optional;
30  import java.util.Set;
31  import java.util.stream.Collectors;
32  import org.apache.syncope.common.lib.SyncopeConstants;
33  import org.apache.syncope.common.lib.types.AnyTypeKind;
34  import org.apache.syncope.common.lib.types.IdRepoEntitlement;
35  import org.apache.syncope.common.lib.types.ResourceOperation;
36  import org.apache.syncope.common.lib.types.TaskType;
37  import org.apache.syncope.core.persistence.api.dao.ExternalResourceDAO;
38  import org.apache.syncope.core.persistence.api.dao.TaskDAO;
39  import org.apache.syncope.core.persistence.api.dao.UserDAO;
40  import org.apache.syncope.core.persistence.api.entity.ExternalResource;
41  import org.apache.syncope.core.persistence.api.entity.task.PropagationData;
42  import org.apache.syncope.core.persistence.api.entity.task.PropagationTask;
43  import org.apache.syncope.core.persistence.api.entity.task.SchedTask;
44  import org.apache.syncope.core.persistence.api.entity.user.User;
45  import org.apache.syncope.core.persistence.jpa.AbstractTest;
46  import org.apache.syncope.core.spring.security.SyncopeAuthenticationDetails;
47  import org.apache.syncope.core.spring.security.SyncopeGrantedAuthority;
48  import org.identityconnectors.framework.common.objects.Attribute;
49  import org.identityconnectors.framework.common.objects.AttributeBuilder;
50  import org.junit.jupiter.api.Test;
51  import org.springframework.beans.factory.annotation.Autowired;
52  import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
53  import org.springframework.security.core.GrantedAuthority;
54  import org.springframework.security.core.context.SecurityContextHolder;
55  import org.springframework.transaction.annotation.Transactional;
56  
57  @Transactional("Master")
58  public class TaskTest extends AbstractTest {
59  
60      @Autowired
61      private TaskDAO taskDAO;
62  
63      @Autowired
64      private ExternalResourceDAO resourceDAO;
65  
66      @Autowired
67      private UserDAO userDAO;
68  
69      @Test
70      public void findByName() {
71          Optional<SchedTask> task = taskDAO.findByName(TaskType.SCHEDULED, "SampleJob Task");
72          assertTrue(task.isPresent());
73          assertEquals(taskDAO.find(TaskType.SCHEDULED, "e95555d2-1b09-42c8-b25b-f4c4ec597979"), task.get());
74      }
75  
76      @Test
77      public void findWithoutExecs() {
78          List<PropagationTask> tasks = taskDAO.findToExec(TaskType.PROPAGATION);
79          assertNotNull(tasks);
80          assertEquals(3, tasks.size());
81      }
82  
83      @Test
84      public void findPaginated() {
85          List<PropagationTask> tasks = taskDAO.findAll(TaskType.PROPAGATION, null, null, null, null, 1, 2, List.of());
86          assertNotNull(tasks);
87          assertEquals(2, tasks.size());
88  
89          for (PropagationTask task : tasks) {
90              assertNotNull(task);
91          }
92  
93          tasks = taskDAO.findAll(TaskType.PROPAGATION, null, null, null, null, 2, 2, List.of());
94          assertNotNull(tasks);
95          assertEquals(2, tasks.size());
96  
97          for (PropagationTask task : tasks) {
98              assertNotNull(task);
99          }
100 
101         tasks = taskDAO.findAll(TaskType.PROPAGATION, null, null, null, null, 1000, 2, List.of());
102         assertNotNull(tasks);
103         assertTrue(tasks.isEmpty());
104 
105         assertEquals(6, taskDAO.count(TaskType.PROPAGATION, null, null, null, null));
106     }
107 
108     @Test
109     public void findAll() {
110         assertEquals(6, taskDAO.findAll(TaskType.PROPAGATION).size());
111         assertEquals(1, taskDAO.findAll(TaskType.NOTIFICATION).size());
112         assertEquals(3, taskDAO.findAll(TaskType.SCHEDULED).size());
113         assertEquals(10, taskDAO.findAll(TaskType.PULL).size());
114         assertEquals(11, taskDAO.findAll(TaskType.PUSH).size());
115 
116         List<GrantedAuthority> authorities = IdRepoEntitlement.values().stream().
117                 map(entitlement -> new SyncopeGrantedAuthority(entitlement, SyncopeConstants.ROOT_REALM)).
118                 collect(Collectors.toList());
119         UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(
120                 new org.springframework.security.core.userdetails.User(
121                         "admin", "FAKE_PASSWORD", authorities), "FAKE_PASSWORD", authorities);
122         auth.setDetails(new SyncopeAuthenticationDetails(SyncopeConstants.MASTER_DOMAIN, null));
123         SecurityContextHolder.getContext().setAuthentication(auth);
124         try {
125             assertEquals(0, taskDAO.findAll(TaskType.MACRO).size());
126         } finally {
127             SecurityContextHolder.getContext().setAuthentication(null);
128         }
129     }
130 
131     @Test
132     public void savePropagationTask() {
133         ExternalResource resource = resourceDAO.find("ws-target-resource-1");
134         assertNotNull(resource);
135 
136         User user = userDAO.find("74cd8ece-715a-44a4-a736-e17b46c4e7e6");
137         assertNotNull(user);
138 
139         PropagationTask task = entityFactory.newEntity(PropagationTask.class);
140         task.setResource(resource);
141         task.setAnyTypeKind(AnyTypeKind.USER);
142         task.setAnyType(AnyTypeKind.USER.name());
143         task.setOperation(ResourceOperation.CREATE);
144         task.setConnObjectKey("one@two.com");
145 
146         Set<Attribute> attributes = new HashSet<>();
147         attributes.add(AttributeBuilder.build("testAttribute", "testValue1", "testValue2"));
148         attributes.add(AttributeBuilder.buildPassword("password".toCharArray()));
149         task.setPropagationData(new PropagationData(attributes));
150 
151         task = taskDAO.save(task);
152         assertNotNull(task);
153 
154         PropagationTask actual = taskDAO.find(TaskType.PROPAGATION, task.getKey());
155         assertEquals(task, actual);
156     }
157 
158     @Test
159     public void delete() {
160         PropagationTask task = taskDAO.find(TaskType.PROPAGATION, "1e697572-b896-484c-ae7f-0c8f63fcbc6c");
161         assertNotNull(task);
162 
163         ExternalResource resource = task.getResource();
164         assertNotNull(resource);
165 
166         taskDAO.delete(task);
167         task = taskDAO.find(TaskType.PROPAGATION, "1e697572-b896-484c-ae7f-0c8f63fcbc6c");
168         assertNull(task);
169 
170         resource = resourceDAO.find(resource.getKey());
171         assertNotNull(resource);
172         assertFalse(taskDAO.<PropagationTask>findAll(
173                 TaskType.PROPAGATION, resource, null, null, null, -1, -1, List.of()).
174                 contains(task));
175     }
176 }