/*
 * Copyright 2020 Global Biodiversity Information Facility (GBIF)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.gbif.registry.pipelines;

import org.gbif.api.model.pipelines.PipelineExecution;
import org.gbif.api.model.pipelines.PipelineProcess;
import org.gbif.api.model.pipelines.PipelineStep;
import org.gbif.api.model.pipelines.StepType;
import org.gbif.api.service.registry.DatasetService;
import org.gbif.common.messaging.api.MessagePublisher;
import org.gbif.registry.persistence.mapper.pipelines.PipelineProcessMapper;

import java.time.LocalDateTime;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import com.fasterxml.jackson.databind.ObjectMapper;

import static org.junit.Assert.assertEquals;

@RunWith(MockitoJUnitRunner.class)
public class PipelinesHistoryTrackingServiceTest {

  @Mock private PipelineProcessMapper pipelineProcessMapper;

  @Mock private DatasetService datasetService;

  @Mock private MessagePublisher messagePublisher;

  @Test
  public void getLatestSuccesfulStepTest() {
    DefaultRegistryPipelinesHistoryTrackingService trackingService =
        new DefaultRegistryPipelinesHistoryTrackingService(
            new ObjectMapper(), messagePublisher, pipelineProcessMapper, datasetService, 1);

    PipelineProcess process = new PipelineProcess();
    PipelineExecution execution = new PipelineExecution().setCreated(LocalDateTime.now());
    process.addExecution(execution);

    PipelineStep s1 =
        new PipelineStep()
            .setType(StepType.ABCD_TO_VERBATIM)
            .setStarted(LocalDateTime.now().minusMinutes(50));
    execution.addStep(s1);

    PipelineStep s2 =
        new PipelineStep()
            .setType(StepType.ABCD_TO_VERBATIM)
            .setStarted(LocalDateTime.now().minusMinutes(60));
    execution.addStep(s1);

    assertEquals(
        s1, trackingService.getLatestSuccessfulStep(process, StepType.ABCD_TO_VERBATIM).get());

    // add newer step
    PipelineStep s3 =
        new PipelineStep()
            .setType(StepType.ABCD_TO_VERBATIM)
            .setStarted(LocalDateTime.now().minusMinutes(40));
    execution.addStep(s3);
    assertEquals(
        s3, trackingService.getLatestSuccessfulStep(process, StepType.ABCD_TO_VERBATIM).get());

    // add older execution with newer step
    PipelineExecution execution2 =
        new PipelineExecution().setCreated(LocalDateTime.now().minusHours(1));
    process.addExecution(execution2);

    PipelineStep s4 =
        new PipelineStep()
            .setType(StepType.ABCD_TO_VERBATIM)
            .setStarted(LocalDateTime.now().minusMinutes(30));
    execution2.addStep(s4);
    assertEquals(
        s4, trackingService.getLatestSuccessfulStep(process, StepType.ABCD_TO_VERBATIM).get());
  }
}

@@ -1083,16 +1083,43 @@
 ateTime;
+%0Aimport java.util.Optional;
 %0A%0Aimport
@@ -1129,16 +1129,28 @@
 g.junit.
+jupiter.api.
 Test;%0Aim
@@ -1168,18 +1168,36 @@
 nit.
-runner.Run
+jupiter.api.extension.Extend
 With
@@ -1246,23 +1246,29 @@
 ito.
-runn
+junit.jupit
 er
-s
 .Mockito
 JUni
@@ -1263,27 +1263,25 @@
 .Mockito
-JUnitRunner
+Extension
 ;%0A%0Aimpor
@@ -1356,14 +1356,30 @@
 nit.
+jupiter.api.
 Assert
+ions
 .ass
@@ -1393,13 +1393,75 @@
 ls;%0A
-%0A@Run
+import static org.junit.jupiter.api.Assertions.assertTrue;%0A%0A@Extend
 With
@@ -1472,19 +1472,17 @@
 kito
-JUnitRunner
+Extension
 .cla
@@ -2528,25 +2528,25 @@
 ddStep(s
-1
+2
 );%0A%0A    
 assertEq
@@ -2537,37 +2537,55 @@
 );%0A%0A    
-assertEquals(
+Optional%3CPipelineStep%3E actual =
 %0A       
  s1, tra
@@ -2580,12 +2580,8 @@
     
- s1,
  tra
@@ -2644,32 +2644,97 @@
 BCD_TO_VERBATIM)
+;%0A    assertTrue(actual.isPresent());%0A    assertEquals(s1, actual
 .get());%0A%0A    //
@@ -2940,37 +2940,21 @@
 3);%0A
+%0A
     a
-ssertEquals(%0A        s3,
+ctual =
  tra
@@ -3017,32 +3017,97 @@
 BCD_TO_VERBATIM)
+;%0A    assertTrue(actual.isPresent());%0A    assertEquals(s3, actual
 .get());%0A%0A    //
@@ -3488,37 +3488,21 @@
 4);%0A
+%0A
     a
-ssertEquals(%0A        s4,
+ctual =
  tra
@@ -3573,16 +3573,81 @@
 ERBATIM)
+;%0A    assertTrue(actual.isPresent());%0A    assertEquals(s4, actual
 .get());

Migrate PipelinesHistoryTrackingServiceTest