001/*
002 * Copyright 2020 Global Biodiversity Information Facility (GBIF)
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.gbif.api.model.pipelines;
017
018import java.time.OffsetDateTime;
019import java.util.Objects;
020
021import org.junit.jupiter.api.Test;
022
023import static org.junit.jupiter.api.Assertions.assertEquals;
024
025/** Tests the {@link PipelineProcess}. */
026public class PipelineProcessTest {
027
028  @Test
029  public void pipelineProcessComparatorByLatestStepTest() {
030    PipelineProcess p1 = new PipelineProcess();
031    PipelineProcess p2 = new PipelineProcess();
032
033    // process without steps
034    PipelineExecution e1 = new PipelineExecution().setCreated(OffsetDateTime.now());
035    p1.addExecution(e1);
036    PipelineExecution e2 = new PipelineExecution().setCreated(OffsetDateTime.now());
037    p2.addExecution(e2);
038    assertEquals(
039        0, Objects.compare(p1, p2, PipelineProcess.PIPELINE_PROCESS_BY_LATEST_STEP_RUNNING_ASC));
040
041    // one process without steps
042    PipelineStep p1e1s1 =
043        new PipelineStep()
044            .setStarted(OffsetDateTime.now().minusMinutes(30))
045            .setState(PipelineStep.Status.RUNNING);
046    e1.addStep(p1e1s1);
047    assertEquals(
048        -1, Objects.compare(p1, p2, PipelineProcess.PIPELINE_PROCESS_BY_LATEST_STEP_RUNNING_ASC));
049
050    // both processes with steps
051    PipelineStep p2e2s1 =
052        new PipelineStep()
053            .setStarted(OffsetDateTime.now().minusMinutes(35))
054            .setState(PipelineStep.Status.RUNNING);
055    e2.addStep(p2e2s1);
056    assertEquals(
057        1, Objects.compare(p1, p2, PipelineProcess.PIPELINE_PROCESS_BY_LATEST_STEP_RUNNING_ASC));
058
059    // completed steps have less preference over running ones
060    PipelineStep p1e1s2 =
061        new PipelineStep()
062            .setStarted(OffsetDateTime.now().minusMinutes(40))
063            .setState(PipelineStep.Status.COMPLETED);
064    e1.addStep(p1e1s2);
065    assertEquals(
066        1, Objects.compare(p1, p2, PipelineProcess.PIPELINE_PROCESS_BY_LATEST_STEP_RUNNING_ASC));
067
068    // steps with null values
069    PipelineStep p1e1s3 = new PipelineStep();
070    e1.addStep(p1e1s3);
071    assertEquals(
072        1, Objects.compare(p1, p2, PipelineProcess.PIPELINE_PROCESS_BY_LATEST_STEP_RUNNING_ASC));
073
074    // steps with start date but no state
075    PipelineStep p1e1s4 = new PipelineStep().setStarted(OffsetDateTime.now().minusMinutes(15));
076    e1.addStep(p1e1s4);
077    assertEquals(
078        1, Objects.compare(p1, p2, PipelineProcess.PIPELINE_PROCESS_BY_LATEST_STEP_RUNNING_ASC));
079  }
080
081  @Test
082  public void pipelineProcessComparatorByLatestExecutionTest() {
083    PipelineProcess p1 = new PipelineProcess();
084    PipelineProcess p2 = new PipelineProcess();
085
086    assertEquals(0, Objects.compare(p1, p2, PipelineProcess.PIPELINE_PROCESS_BY_LATEST_EXEUCTION_ASC));
087
088    PipelineExecution e1 = new PipelineExecution().setCreated(OffsetDateTime.now().minusMinutes(20));
089    p1.addExecution(e1);
090    assertEquals(-1, Objects.compare(p1, p2, PipelineProcess.PIPELINE_PROCESS_BY_LATEST_EXEUCTION_ASC));
091
092    PipelineExecution e2 = new PipelineExecution().setCreated(OffsetDateTime.now().minusMinutes(30));
093    p2.addExecution(e2);
094    assertEquals(1, Objects.compare(p1, p2, PipelineProcess.PIPELINE_PROCESS_BY_LATEST_EXEUCTION_ASC));
095
096    PipelineExecution e3 = new PipelineExecution().setCreated(OffsetDateTime.now().minusMinutes(10));
097    p2.addExecution(e3);
098    assertEquals(-1, Objects.compare(p1, p2, PipelineProcess.PIPELINE_PROCESS_BY_LATEST_EXEUCTION_ASC));
099  }
100}