package fr.synchrotron.soleil.ica.ci.lib.mongodb.latestversionrresolver.service;

import fr.synchrotron.soleil.ica.ci.lib.mongodb.latestversionrresolver.service.data.MongoDBDataRepository;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.IOException;

/**
 * @author Gregory Boissinot
 */
public class ArtifactVersionResolverMongoDBRepositoryTest {

    private static MongoDBDataRepository mongoDBDataRepository = new MongoDBDataRepository();
    private static ArtifactVersionResolverService resolverService;

    @BeforeClass
    public static void setupData() throws IOException {
        mongoDBDataRepository.setup();
        resolverService = new ArtifactVersionResolverService(mongoDBDataRepository.getArtifactRepository());
    }

    public static void cleanup() throws IOException {
        mongoDBDataRepository.cleanupDatabase();
    }

    private String getLatest(String org, String name, String status) {
        return resolverService.getLatestVersion(org, name, status);
    }

    @Test
    public void latestBuildStatus() {
        Assert.assertEquals("1.7.BUILD", getLatest("org1", "name1", "build"));
        Assert.assertEquals("1.6.INTEGRATION", getLatest("org2", "name2", "build"));
        Assert.assertEquals("1.5.RELEASE", getLatest("org3", "name3", "build"));
    }

    @Test
    public void latestIntegrationStatus() {
        Assert.assertEquals("1.6.INTEGRATION", getLatest("org1", "name1", "integration"));
        Assert.assertEquals("1.6.INTEGRATION", getLatest("org2", "name2", "integration"));
        Assert.assertEquals("1.5.RELEASE", getLatest("org3", "name3", "integration"));
    }

    @Test
    public void latestReleaseStatus() {
        Assert.assertEquals("1.5.RELEASE", getLatest("org1", "name1", "release"));
        Assert.assertEquals("1.5.RELEASE", getLatest("org2", "name2", "release"));
        Assert.assertEquals("1.5.RELEASE", getLatest("org3", "name3", "release"));
    }

}

@@ -928,16 +928,149 @@
 ing org,
+ String name) %7B%0A        return resolverService.getLatestVersion(org, name);%0A    %7D%0A%0A    private String getLatestWithStatus(String org,
  String 
@@ -1257,32 +1257,42 @@
 UILD%22, getLatest
+WithStatus
 (%22org1%22, %22name1%22
@@ -1352,32 +1352,42 @@
 TION%22, getLatest
+WithStatus
 (%22org2%22, %22name2%22
@@ -1443,32 +1443,42 @@
 EASE%22, getLatest
+WithStatus
 (%22org3%22, %22name3%22
@@ -1599,32 +1599,42 @@
 TION%22, getLatest
+WithStatus
 (%22org1%22, %22name1%22
@@ -1700,32 +1700,42 @@
 TION%22, getLatest
+WithStatus
 (%22org2%22, %22name2%22
@@ -1797,32 +1797,42 @@
 EASE%22, getLatest
+WithStatus
 (%22org3%22, %22name3%22
@@ -1955,24 +1955,34 @@
 %22, getLatest
+WithStatus
 (%22org1%22, %22na
@@ -2048,24 +2048,34 @@
 %22, getLatest
+WithStatus
 (%22org2%22, %22na
@@ -2145,16 +2145,26 @@
 etLatest
+WithStatus
 (%22org3%22,
@@ -2181,22 +2181,239 @@
 %22release%22));
+%0A%0A        Assert.assertEquals(%221.5.RELEASE%22, getLatest(%22org1%22, %22name1%22));%0A        Assert.assertEquals(%221.5.RELEASE%22, getLatest(%22org2%22, %22name2%22));%0A        Assert.assertEquals(%221.5.RELEASE%22, getLatest(%22org3%22, %22name3%22));
 %0A    %7D%0A%0A%7D%0A

Add test for getLatest without status