/*
 * Copyright 2017 Banco Bilbao Vizcaya Argentaria, S.A..
 *
 * 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 com.bbva.arq.devops.ae.mirrorgate.repository;

import static com.bbva.arq.devops.ae.mirrorgate.builders.BuildBuilder.makeBuild;
import static org.assertj.core.api.Assertions.assertThat;

import com.bbva.arq.devops.ae.mirrorgate.model.Build;
import java.util.Arrays;
import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@DataMongoTest
public class BuildRepositoryTests {

    @Autowired
    private BuildRepository repository;

    private static final String REPO_NAME = "mirrorgate";
    private static final String CULPRIT = "Atreyu";

    @Before
    public void before() {
    }

    @Test
    public void getLastByRepoNameAndByTeamMembers() {
        List<String> repos = Arrays.asList(REPO_NAME);
        List<String> teamMembers = Arrays.asList(CULPRIT);

        Build build1 = makeBuild(REPO_NAME, "develop");
        Build build2 = makeBuild(REPO_NAME, "master", teamMembers);
        Build build3 = makeBuild(REPO_NAME, "master");

        repository.save(build1);
        repository.save(build2);
        repository.save(build3);

        List<Build> builds = repository
                .findLastBuildsByReposNameAndByTeamMembers(repos, teamMembers);

        assertThat(builds.get(0).getTimestamp()).isEqualTo(build2.getTimestamp());
        assertThat(builds.get(0).getBranch()).isEqualTo(build2.getBranch());
    }

    @Test
    public void getLastByRepoNameAndByTeamMembersWithoutTeamMembers() {
        List<String> repos = Arrays.asList(REPO_NAME);

        Build build1 = makeBuild(REPO_NAME, "develop");
        Build build2 = makeBuild(REPO_NAME, "master");
        Build build3 = makeBuild(REPO_NAME, "master");

        repository.save(build1);
        repository.save(build2);
        repository.save(build3);

        List<Build> builds = repository
                .findLastBuildsByReposNameAndByTeamMembers(repos, null);

        assertThat(builds.get(0).getTimestamp()).isEqualTo(build3.getTimestamp());
        assertThat(builds.get(0).getBranch()).isEqualTo(build3.getBranch());
        assertThat(builds.get(1).getTimestamp()).isEqualTo(build1.getTimestamp());
        assertThat(builds.get(1).getBranch()).isEqualTo(build1.getBranch());
    }

    @After
    public void after() {
        repository.deleteAll();
    }

}

@@ -1450,16 +1450,18 @@
 ring
+%5B%5D
  CULPRIT
  = %22
@@ -1460,19 +1460,21 @@
 PRIT
+S
  = 
+%7B
 %22Atreyu%22
 ;%0A%0A 
@@ -1469,16 +1469,36 @@
 %22Atreyu%22
+, %22Gmork%22, %22Xayide%22%7D
 ;%0A%0A    @
@@ -1713,16 +1713,33 @@
 (CULPRIT
+S%5B1%5D, CULPRITS%5B2%5D
 );%0A%0A    
@@ -1779,32 +1779,60 @@
 _NAME, %22develop%22
+, Arrays.asList(CULPRITS%5B0%5D)
 );%0A        Build
@@ -1867,28 +1867,127 @@
 E, %22
-master%22, teamMembers
+develop%22, Arrays.asList(CULPRITS%5B2%5D));%0A        Build build3 = makeBuild(REPO_NAME, %22master%22, Arrays.asList(CULPRITS%5B2%5D)
 );%0A 
@@ -1996,33 +1996,33 @@
      Build build
-3
+4
  = makeBuild(REP
@@ -2029,32 +2029,60 @@
 O_NAME, %22master%22
+, Arrays.asList(CULPRITS%5B1%5D)
 );%0A%0A        repo
@@ -2159,32 +2159,65 @@
 ry.save(build3);
+%0A        repository.save(build4);
 %0A%0A        List%3CB
@@ -2248,34 +2248,32 @@
 y%0A              
-  
 .findLastBuildsB
@@ -2317,24 +2317,72 @@
 mMembers);%0A%0A
+        assertThat(builds.size()).isEqualTo(2);%0A
         asse
@@ -2429,25 +2429,25 @@
 qualTo(build
-2
+4
 .getTimestam
@@ -2504,24 +2504,184 @@
 ).isEqualTo(
+build4.getBranch());%0A        assertThat(builds.get(1).getTimestamp()).isEqualTo(build2.getTimestamp());%0A        assertThat(builds.get(1).getBranch()).isEqualTo(
 build2.getBr

improve build test filtered for severals team members