/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.job.internal;

import java.util.Arrays;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.xwiki.component.internal.ContextComponentManagerProvider;
import org.xwiki.component.manager.ComponentLookupException;
import org.xwiki.job.DefaultRequest;
import org.xwiki.job.JobExecutor;
import org.xwiki.job.JobGroupPath;
import org.xwiki.job.event.status.JobStatus.State;
import org.xwiki.job.test.TestBasicGroupedJob;
import org.xwiki.test.annotation.ComponentList;
import org.xwiki.test.mockito.MockitoComponentMockingRule;

import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;

/**
 * Validate {@link DefaultJobExecutor};
 * 
 * @version $Id$
 */
@ComponentList(ContextComponentManagerProvider.class)
public class DefaultJobExecutorTest
{
    @Rule
    public MockitoComponentMockingRule<JobExecutor> mocker =
        new MockitoComponentMockingRule<JobExecutor>(DefaultJobExecutor.class);

    @Before
    public void before() throws Exception
    {
    }

    private TestBasicGroupedJob groupedJob(String... path)
    {
        return new TestBasicGroupedJob("type", new JobGroupPath(Arrays.asList(path)), new DefaultRequest());
    }

    // Tests

    @Test
    public void testMatchingGroupPathAreBlocked() throws ComponentLookupException, InterruptedException
    {
        TestBasicGroupedJob jobA = groupedJob("A");
        TestBasicGroupedJob jobAB = groupedJob("A", "B");

        TestBasicGroupedJob job12 = groupedJob("1", "2");
        TestBasicGroupedJob job1 = groupedJob("1");

        // Pre-lock all jobs
        jobA.lock();
        jobAB.lock();
        job12.lock();
        job1.lock();

        // Give all jobs to JobExecutor
        this.mocker.getComponentUnderTest().execute(jobA);
        // Give enough time for the job to be fully taken into account
        Thread.sleep(10);

        this.mocker.getComponentUnderTest().execute(jobAB);
        // Give enough time for the job to be fully taken into account
        Thread.sleep(10);

        this.mocker.getComponentUnderTest().execute(job12);
        // Give enough time for the job to be fully taken into account
        Thread.sleep(10);

        this.mocker.getComponentUnderTest().execute(job1);
        // Give enough time for the job to be fully taken into account
        Thread.sleep(10);

        ////////////////////
        // A and A/B

        assertSame(State.WAITING, jobA.getStatus().getState());
        assertNull(jobAB.getStatus().getState());

        // Next job
        jobA.unlock();
        Thread.sleep(10);

        assertSame(State.FINISHED, jobA.getStatus().getState());
        assertSame(State.WAITING, jobAB.getStatus().getState());

        // Next job
        jobAB.unlock();
        Thread.sleep(10);

        assertSame(State.FINISHED, jobA.getStatus().getState());
        assertSame(State.FINISHED, jobAB.getStatus().getState());

        ////////////////////
        // 1/2 and 1

        assertSame(State.WAITING, job12.getStatus().getState());
        assertNull(job1.getStatus().getState());

        // Next job
        job12.unlock();
        Thread.sleep(10);

        assertSame(State.FINISHED, job12.getStatus().getState());
        assertSame(State.WAITING, job1.getStatus().getState());

        // Next job
        job1.unlock();
        Thread.sleep(10);

        assertSame(State.FINISHED, job1.getStatus().getState());
        assertSame(State.FINISHED, job1.getStatus().getState());
    }
}

@@ -967,55 +967,19 @@
 nit.
-Before;%0Aimport org.
 ju
-nit.Rule;%0Aimport org.junit
+piter.api
 .Tes
@@ -1054,69 +1054,8 @@
 er;%0A
-import org.xwiki.component.manager.ComponentLookupException;%0A
 impo
@@ -1091,42 +1091,8 @@
 st;%0A
-import org.xwiki.job.JobExecutor;%0A
 impo
@@ -1294,23 +1294,23 @@
 est.
-mockito.M
+junit5.m
 ockito
+.
 Comp
@@ -1314,27 +1314,79 @@
 omponent
-MockingRule
+Test;%0Aimport org.xwiki.test.junit5.mockito.InjectMockComponents
 ;%0A%0Aimpor
@@ -1400,30 +1400,46 @@
 c org.junit.
+jupiter.api.
 Assert
+ions
 .assertNull;
@@ -1463,22 +1463,38 @@
 g.junit.
+jupiter.api.
 Assert
+ions
 .assertS
@@ -1568,16 +1568,31 @@
 Id$%0A */%0A
+@ComponentTest%0A
 @Compone
@@ -1684,31 +1684,18 @@
    @
-Rule%0A    public 
+Inject
 Mock
-ito
 Comp
@@ -1703,383 +1703,50 @@
 nent
-MockingRule%3CJobExecutor%3E mocker =%0A        new MockitoComponentMockingRule%3CJobExecutor%3E(DefaultJobExecutor.class);%0A%0A    @Before%0A    public void before() throws Exception%0A    %7B%0A    %7D%0A%0A    private TestBasicGroupedJob groupedJob(String... path)%0A    %7B%0A        return new TestBasicGroupedJob(%22type%22, new JobGroupPath(Arrays.asList(path)), new DefaultRequest());%0A    %7D%0A%0A    // Tests
+s%0A    private DefaultJobExecutor executor;
 %0A%0A  
@@ -1773,13 +1773,9 @@
 oid 
-testM
+m
 atch
@@ -1809,34 +1809,8 @@
 rows
- ComponentLookupException,
  Int
@@ -2228,38 +2228,16 @@
 his.
-mocker.getComponentUnderTest()
+executor
 .exe
@@ -2363,38 +2363,16 @@
 his.
-mocker.getComponentUnderTest()
+executor
 .exe
@@ -2499,38 +2499,16 @@
 his.
-mocker.getComponentUnderTest()
+executor
 .exe
@@ -2635,38 +2635,16 @@
 his.
-mocker.getComponentUnderTest()
+executor
 .exe
@@ -3896,11 +3896,192 @@
 );%0A    %7D
+%0A%0A    private TestBasicGroupedJob groupedJob(String... path)%0A    %7B%0A        return new TestBasicGroupedJob(%22type%22, new JobGroupPath(Arrays.asList(path)), new DefaultRequest());%0A    %7D
 %0A%7D%0A

Convert test to JUnit5