|
1 | 1 | package biz.netcentric.cq.tools.actool.impl; |
2 | 2 |
|
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | + |
3 | 5 | /*- |
4 | 6 | * #%L |
5 | 7 | * Access Control Tool Bundle |
|
17 | 19 | import static org.mockito.Mockito.when; |
18 | 20 |
|
19 | 21 | import java.util.Arrays; |
| 22 | +import java.util.HashMap; |
20 | 23 | import java.util.Iterator; |
21 | 24 | import java.util.List; |
| 25 | +import java.util.Map; |
22 | 26 |
|
23 | 27 | import javax.jcr.RepositoryException; |
24 | 28 |
|
25 | 29 | import org.apache.jackrabbit.api.security.user.Authorizable; |
26 | 30 | import org.apache.jackrabbit.api.security.user.Group; |
27 | 31 | import org.apache.jackrabbit.api.security.user.User; |
| 32 | +import org.apache.sling.event.jobs.Job; |
| 33 | +import org.apache.sling.event.jobs.JobBuilder; |
| 34 | +import org.apache.sling.event.jobs.JobManager; |
28 | 35 | import org.junit.jupiter.api.Test; |
29 | 36 | import org.junit.jupiter.api.extension.ExtendWith; |
30 | 37 | import org.mockito.Mock; |
| 38 | +import org.mockito.Mockito; |
31 | 39 | import org.mockito.invocation.InvocationOnMock; |
32 | 40 | import org.mockito.junit.jupiter.MockitoExtension; |
33 | 41 | import org.mockito.stubbing.Answer; |
34 | 42 |
|
| 43 | +import biz.netcentric.cq.tools.actool.api.InstallationOptionsBuilder; |
| 44 | + |
35 | 45 | @ExtendWith(MockitoExtension.class) |
36 | | -public class AcInstallationServiceImplTest { |
| 46 | +class AcInstallationServiceImplTest { |
37 | 47 |
|
38 | 48 | AcInstallationServiceImpl acInstallationServiceImpl = new AcInstallationServiceImpl(); |
39 | 49 |
|
@@ -65,7 +75,7 @@ public class AcInstallationServiceImplTest { |
65 | 75 | User user2; |
66 | 76 |
|
67 | 77 | @Test |
68 | | - public void testSortAuthorizablesForDeletion() throws RepositoryException { |
| 78 | + void testSortAuthorizablesForDeletion() throws RepositoryException { |
69 | 79 |
|
70 | 80 | when(group1.getID()).thenReturn("group1"); |
71 | 81 | when(group2.getID()).thenReturn("group2"); |
@@ -129,4 +139,39 @@ public Iterator<Group> answer(InvocationOnMock invocation) throws Throwable { |
129 | 139 | return groups.iterator(); |
130 | 140 | } |
131 | 141 | } |
| 142 | + |
| 143 | + @Test |
| 144 | + void testAsynchronousInstallationInDistributedSetups() { |
| 145 | + JobManager jobManager = Mockito.mock(JobManager.class); |
| 146 | + Job job = Mockito.mock(Job.class); |
| 147 | + JobBuilder jobBuilder = Mockito.mock(JobBuilder.class); |
| 148 | + Map<String, Object> jobProps = new HashMap<String, Object>(); |
| 149 | + |
| 150 | + when(jobManager.createJob(Mockito.anyString())).thenReturn(jobBuilder); |
| 151 | + when(jobBuilder.add()).thenReturn(job); |
| 152 | + when(job.getId()).thenReturn("id"); |
| 153 | + when(jobBuilder.properties(Mockito.any())).thenAnswer(new Answer<JobBuilder>() { |
| 154 | + @Override |
| 155 | + public JobBuilder answer(InvocationOnMock invocation) throws Throwable { |
| 156 | + Map<String, String> props = invocation.getArgument(0); |
| 157 | + jobProps.putAll(props); |
| 158 | + return jobBuilder; |
| 159 | + } |
| 160 | + }); |
| 161 | + when(jobManager.createJob(Mockito.anyString())).thenReturn(jobBuilder); |
| 162 | + when(job.getPropertyNames()).thenReturn(jobProps.keySet()); |
| 163 | + when(job.getProperty(Mockito.anyString())).thenAnswer(new Answer<Object>() { |
| 164 | + @Override |
| 165 | + public Object answer(InvocationOnMock invocation) throws Throwable { |
| 166 | + return jobProps.get(invocation.getArgument(0)); |
| 167 | + } |
| 168 | + }); |
| 169 | + |
| 170 | + acInstallationServiceImpl.jobManager = jobManager; |
| 171 | + // schedule in one server |
| 172 | + assertEquals("id", acInstallationServiceImpl.applyAsynchronously(new InstallationOptionsBuilder().withConfigurationRootPath("/apps").build())); |
| 173 | + // and process in another server (with another instance of the service) |
| 174 | + AcInstallationServiceImpl acInstallationServiceImpl2 = new AcInstallationServiceImpl(); |
| 175 | + acInstallationServiceImpl2.process(job); |
| 176 | + } |
132 | 177 | } |
0 commit comments