/**
 * JBoss, Home of Professional Open Source.
 * Copyright 2014 Red Hat, Inc., and individual contributors
 * as indicated by the @author tags.
 *
 * 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.jboss.pnc.datastore.repositories;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.junit.InSequence;
import org.jboss.pnc.datastore.DeploymentFactory;
import org.jboss.pnc.model.BuildRecord;
import org.jboss.pnc.model.BuildStatus;
import org.jboss.pnc.model.User;
import org.jboss.pnc.spi.datastore.repositories.BuildRecordRepository;
import org.jboss.pnc.spi.datastore.repositories.UserRepository;
import org.jboss.pnc.test.category.ContainerTest;
import org.jboss.shrinkwrap.api.Archive;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;

import javax.inject.Inject;
import java.util.Date;
import java.util.List;

import static org.junit.Assert.assertEquals;

/**
 * @author Jakub Bartecek
 */
@RunWith(Arquillian.class)
@Category(ContainerTest.class)
public class BuildRecordRepositoryTest {

    private User user = null;

    @Inject
    private UserRepository userRepository;

    @Inject
    private BuildRecordRepository buildRecordRepository;

    @Deployment
    public static Archive<?> getDeployment() {
        return DeploymentFactory.createDatastoreDeployment();
    }

    @InSequence(1)
    @Test
    public void shouldFindExpiredTemporaryBuilds() {
        // given
        BuildRecord givenBr = initBuildRecordBuilder()
                .endTime(new Date(0))
                .build();
        givenBr = buildRecordRepository.save(givenBr);

        // when
        List<BuildRecord> found = buildRecordRepository.findTemporaryBuildsOlderThan(new Date());

        // then
        assertEquals(1, found.size());
        assertEquals(givenBr.getId(), found.get(0).getId());

    }

    @InSequence(2)
    @Test
    public void shouldFindNoneExpiredTemporaryBuilds() {
        Date now = new Date();
        // given
        BuildRecord givenBr = initBuildRecordBuilder()
                .endTime(now)
                .build();
        buildRecordRepository.save(givenBr);

        // when
        List<BuildRecord> found = buildRecordRepository.findTemporaryBuildsOlderThan(new Date(now.getTime() - 1000));

        // then
        assertEquals(0, found.size());

    }

    private BuildRecord.Builder initBuildRecordBuilder() {
        if(user == null) {
            user = userRepository.save(User.Builder.newBuilder()
                    .id(1)
                    .username("demo-user")
                    .firstName("Demo First Name")
                    .lastName("Demo Last Name")
                    .email("demo-user@pnc.com")
                    .build());
        }

        return BuildRecord.Builder.newBuilder()
                .id(8888)
                .buildConfigurationAuditedId(1)
                .buildConfigurationAuditedRev(1)
                .submitTime(new Date())
                .user(User.Builder.newBuilder().id(1).build())
                .status(BuildStatus.SUCCESS);
    }



}

@@ -1058,16 +1058,62 @@
 l.User;%0A
+import org.jboss.pnc.spi.datastore.Datastore;%0A
 import o
@@ -1846,24 +1846,70 @@
 epository;%0A%0A
+    @Inject%0A    private Datastore datastore;%0A%0A
     @Deploym
@@ -2083,16 +2083,20 @@
 ouldFind
+None
 ExpiredT
@@ -2123,32 +2123,63 @@
        // given%0A
+        Date now = new Date();%0A
         BuildRec
@@ -2247,18 +2247,48 @@
 me(n
-ew Date(0)
+ow)%0A                .temporaryBuild(true
 )%0A  
@@ -2318,26 +2318,16 @@
 %0A       
- givenBr =
  buildRe
@@ -2467,16 +2467,36 @@
 ew Date(
+now.getTime() - 1000
 ));%0A%0A   
@@ -2529,17 +2529,17 @@
 tEquals(
-1
+0
 , found.
@@ -2551,70 +2551,8 @@
 ));%0A
-        assertEquals(givenBr.getId(), found.get(0).getId());%0A%0A
     
@@ -2601,36 +2601,32 @@
  void shouldFind
-None
 ExpiredTemporary
@@ -2640,39 +2640,8 @@
 ) %7B%0A
-        Date now = new Date();%0A
     
@@ -2726,34 +2726,80 @@
       .endTime(n
-ow
+ew Date(0))%0A                .temporaryBuild(true
 )%0A              
@@ -2809,32 +2809,42 @@
 build();%0A       
+ givenBr =
  buildRecordRepo
@@ -2976,32 +2976,16 @@
 ew Date(
-now.getTime() - 
 1000));%0A
@@ -3014,33 +3014,33 @@
    assertEquals(
-0
+1
 , found.size());
@@ -3040,24 +3040,84 @@
 ize());%0A
+        assertEquals(givenBr.getId(), found.get(0).getId());
 %0A    %7D%0A%0A
     priv
@@ -3104,24 +3104,25 @@
 ());%0A    %7D%0A%0A
+%0A
     private 
@@ -3590,12 +3590,40 @@
 .id(
-8888
+datastore.getNextBuildRecordId()
 )%0A  

Fix tests for BuildRecordRepository