/**
 * 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.core.test.buildCoordinator;

import org.jboss.arquillian.junit.Arquillian;
import org.jboss.pnc.mock.datastore.DatastoreMock;
import org.jboss.pnc.mock.model.builders.TestProjectConfigurationBuilder;
import org.jboss.pnc.model.BuildConfigSetRecord;
import org.jboss.pnc.model.BuildRecord;
import org.jboss.pnc.model.BuildStatus;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by <a href="mailto:matejonnet@gmail.com">Matej Lazar</a> on 2015-01-06.
 */
@RunWith(Arquillian.class)
public class ProjectWithDependenciesBuildTest extends ProjectBuilder {

    Logger log = LoggerFactory.getLogger(ProjectWithDependenciesBuildTest.class);

    @Inject
    BuildCoordinatorFactory buildCoordinatorFactory;

    @Test
    public void buildProjectTestCase() throws Exception {
        //given
        DatastoreMock datastoreMock = new DatastoreMock();
        TestProjectConfigurationBuilder configurationBuilder = new TestProjectConfigurationBuilder(datastoreMock);

        //when
        buildProjects(configurationBuilder.buildConfigurationSet(1), buildCoordinatorFactory.createBuildCoordinator(datastoreMock));

        //expect
        List<BuildRecord> buildRecords = datastoreMock.getBuildRecords();
        log.trace("Found build records: {}", buildRecords.stream()
                .map(br -> "Br.id: " + br.getId() + ", " + br.getBuildConfigurationAudited().getId().toString())
                .collect(Collectors.joining("; ")));
        Assert.assertEquals("Wrong datastore results count.", 5, buildRecords.size());

        BuildRecord buildRecord = buildRecords.get(0);
        String buildLog = buildRecord.getBuildLog();
        Assert.assertTrue("Invalid build log.", buildLog.contains("Finished: SUCCESS"));

        assertBuildArtifactsPresent(buildRecord.getBuiltArtifacts());
        assertDependencyArtifactsPresent(buildRecord.getDependencies());

        BuildConfigSetRecord buildConfigSetRecord = datastoreMock.getBuildConfigSetRecords().get(0);
        Assert.assertNotNull(buildConfigSetRecord.getEndTime());
        Assert.assertTrue(buildConfigSetRecord.getEndTime().getTime() > buildConfigSetRecord.getStartTime().getTime());
        Assert.assertEquals(BuildStatus.SUCCESS, buildConfigSetRecord.getStatus());
    }

}

@@ -794,16 +794,108 @@
 illian;%0A
+import org.jboss.pnc.core.test.buildCoordinator.event.TestCDIBuildSetStatusChangedReceiver;%0A
 import o
@@ -1140,16 +1140,76 @@
 Status;%0A
+import org.jboss.pnc.spi.events.BuildSetStatusChangedEvent;%0A
 import o
@@ -1367,16 +1367,42 @@
 Inject;%0A
+import java.util.HashSet;%0A
 import j
@@ -1412,24 +1412,46 @@
 .util.List;%0A
+import java.util.Set;%0A
 import java.
@@ -1823,86 +1823,659 @@
    @
-Test%0A    public void buildProjectTestCase() throws Exception %7B%0A        //given
+Inject%0A    TestCDIBuildSetStatusChangedReceiver testCDIBuildSetStatusChangedReceiver;%0A%0A    private static final int BUILD_SET_ID = 14352;%0A    Set%3CBuildSetStatusChangedEvent%3E eventsReceived = new HashSet%3C%3E();%0A%0A    private void collectEvent(BuildSetStatusChangedEvent buildSetStatusChangedEvent) %7B%0A        if (buildSetStatusChangedEvent.getBuildSetConfigurationId() == BUILD_SET_ID) %7B%0A            eventsReceived.add(buildSetStatusChangedEvent);%0A        %7D%0A    %7D%0A%0A    @Test%0A    public void buildProjectTestCase() throws Exception %7B%0A        //given%0A        testCDIBuildSetStatusChangedReceiver.addBuildSetStatusChangedEventListener((e) -%3E collectEvent(e));
 %0A   
@@ -2727,16 +2727,20 @@
 ionSet(1
+4352
 ), build
@@ -3918,22 +3918,249 @@
 etStatus());
+%0A%0A        String events = eventsReceived.stream().map(e -%3E e.toString()).collect(Collectors.joining(%22; %22));%0A        Assert.assertEquals(%22Invalid number of received events. Received events: %22 + events, 2, eventsReceived.size());
 %0A    %7D%0A%0A%7D%0A

Test set status update events.