<commit_before>/**
 * JBoss, Home of Professional Open Source.
 * Copyright 2014-2020 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.integration_new.endpoint;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.pnc.AbstractTest;
import org.jboss.pnc.client.BuildClient;
import org.jboss.pnc.client.BuildConfigurationClient;
import org.jboss.pnc.client.ClientException;
import org.jboss.pnc.client.GroupBuildClient;
import org.jboss.pnc.client.GroupConfigurationClient;
import org.jboss.pnc.client.RemoteCollection;
import org.jboss.pnc.client.RemoteResourceNotFoundException;
import org.jboss.pnc.dto.Build;
import org.jboss.pnc.dto.BuildConfiguration;
import org.jboss.pnc.dto.BuildConfigurationRevision;
import org.jboss.pnc.dto.BuildConfigurationRevisionRef;
import org.jboss.pnc.dto.GroupBuild;
import org.jboss.pnc.dto.GroupConfiguration;
import org.jboss.pnc.dto.requests.GroupBuildRequest;
import org.jboss.pnc.enums.BuildStatus;
import org.jboss.pnc.enums.RebuildMode;
import org.jboss.pnc.integration.mock.RemoteBuildsCleanerMock;
import org.jboss.pnc.integration.utils.ResponseUtils;
import org.jboss.pnc.integration_new.setup.Deployments;
import org.jboss.pnc.rest.api.parameters.BuildParameters;
import org.jboss.pnc.rest.api.parameters.BuildsFilterParameters;
import org.jboss.pnc.rest.api.parameters.GroupBuildParameters;
import org.jboss.pnc.test.category.ContainerTest;
import org.jboss.shrinkwrap.api.spec.EnterpriseArchive;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.Instant;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.jboss.pnc.integration_new.setup.RestClientConfiguration.asUser;

@RunAsClient
@RunWith(Arquillian.class)
@Category(ContainerTest.class)
public class BuildTest {

    private static final Logger logger = LoggerFactory.getLogger(BuildTest.class);

    private BuildConfigurationClient buildConfigurationClient = new BuildConfigurationClient(asUser());

    private GroupConfigurationClient groupConfigurationClient = new GroupConfigurationClient(asUser());

    private GroupBuildClient groupBuildClient = new GroupBuildClient(asUser());

    private BuildClient buildClient = new BuildClient(asUser());

    @Deployment
    public static EnterpriseArchive deploy() {
        final EnterpriseArchive ear = Deployments.testEarForInContainerTest(BuildTest.class);
        Deployments.addBuildExecutorMock(ear);
        JavaArchive coordinatorJar = ear.getAsType(JavaArchive.class, AbstractTest.COORDINATOR_JAR);
        coordinatorJar.addAsManifestResource("beans-use-mock-remote-clients.xml", "beans.xml");
        coordinatorJar.addClass(RemoteBuildsCleanerMock.class);
        return ear;
    }

    @Test
    public void shouldTriggerBuildAndFinishWithoutProblems() throws ClientException {
        // with
        BuildConfiguration buildConfiguration = buildConfigurationClient.getAll().iterator().next();

        // when
        Build build = buildConfigurationClient.trigger(buildConfiguration.getId(), getPersistentParameters(true));
        assertThat(build).isNotNull().extracting("id").isNotNull().isNotEqualTo("");

        EnumSet<BuildStatus> isIn = EnumSet.of(BuildStatus.SUCCESS);
        ResponseUtils.waitSynchronouslyFor(() -> buildToFinish(build.getId(), isIn, null), 15, TimeUnit.SECONDS);
    }

    @Test
    public void shouldTriggerGroupBuildAndFinishWithoutProblems() throws ClientException {
        // given
        GroupConfiguration buildConfigurationSet = groupConfigurationClient.getAll().iterator().next();

        // when
        GroupBuildParameters groupBuildParameters = new GroupBuildParameters();
        groupBuildParameters.setRebuildMode(RebuildMode.FORCE);

        GroupBuild groupBuild = groupConfigurationClient.trigger(
                buildConfigurationSet.getId(),
                groupBuildParameters,
                GroupBuildRequest.builder().buildConfigurationRevisions(new ArrayList<>()).build());
        assertThat(groupBuild).isNotNull().extracting("id").isNotNull().isNotEqualTo("");
        // then
        EnumSet<BuildStatus> isIn = EnumSet.of(BuildStatus.SUCCESS);
        EnumSet<BuildStatus> isNotIn = EnumSet.of(BuildStatus.REJECTED);
        ResponseUtils.waitSynchronouslyFor(
                () -> groupBuildToFinish(groupBuild.getId(), isIn, isNotIn),
                15,
                TimeUnit.SECONDS);
    }

    @Test
    public void shouldTriggerGroupBuildWithBCInRevisionAndFinishWithoutProblems() throws ClientException {
        // given
        GroupConfiguration groupConfiguration = groupConfigurationClient.getAll().iterator().next();
        assertThat(groupConfiguration.getBuildConfigs()).isNotEmpty();

        List<BuildConfigurationRevisionRef> buildConfigurationRevisions = new ArrayList<>();
        BuildConfigurationRevision buildConfigurationRevision = BuildConfigurationRevision.builder()
                .id(groupConfiguration.getBuildConfigs().keySet().iterator().next())
                .rev(1)
                .name(groupConfiguration.getName())
                .build();
        buildConfigurationRevisions.add(buildConfigurationRevision);

        GroupBuildRequest buildConfigurationSetWithAuditedBCsRest = GroupBuildRequest.builder()
                .buildConfigurationRevisions(buildConfigurationRevisions)
                .build();
        GroupBuildParameters groupBuildParameters = new GroupBuildParameters();
        groupBuildParameters.setRebuildMode(RebuildMode.FORCE);

        // when
        GroupBuild groupBuild = groupConfigurationClient
                .trigger(groupConfiguration.getId(), groupBuildParameters, buildConfigurationSetWithAuditedBCsRest);
        // then
        assertThat(groupBuild).isNotNull().extracting("id").isNotNull().isNotEqualTo("");

        EnumSet<BuildStatus> isIn = EnumSet.of(BuildStatus.SUCCESS);
        EnumSet<BuildStatus> isNotIn = EnumSet.of(BuildStatus.REJECTED);
        ResponseUtils.waitSynchronouslyFor(
                () -> groupBuildToFinish(groupBuild.getId(), isIn, isNotIn),
                15,
                TimeUnit.SECONDS);
    }

    @Test
    public void shouldTriggerBuildWithADependencyAndFinishWithoutProblems() throws ClientException {
        // given - A BC with a dependency on pnc-1.0.0.DR1
        BuildConfiguration buildConfigurationParent = buildConfigurationClient
                .getAll(Optional.empty(), Optional.of("name==dependency-analysis-1.3"))
                .iterator()
                .next();

        // Update dependency<commit_msg>Add NCL-5041 reproducer: reject Build Group with NO_REBUILD_REQUIRED<commit_after>/**
 * JBoss, Home of Professional Open Source.
 * Copyright 2014-2020 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.integration_new.endpoint;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.pnc.AbstractTest;
import org.jboss.pnc.client.BuildClient;
import org.jboss.pnc.client.BuildConfigurationClient;
import org.jboss.pnc.client.ClientException;
import org.jboss.pnc.client.GroupBuildClient;
import org.jboss.pnc.client.GroupConfigurationClient;
import org.jboss.pnc.client.RemoteCollection;
import org.jboss.pnc.client.RemoteResourceNotFoundException;
import org.jboss.pnc.dto.Build;
import org.jboss.pnc.dto.BuildConfiguration;
import org.jboss.pnc.dto.BuildConfigurationRevision;
import org.jboss.pnc.dto.BuildConfigurationRevisionRef;
import org.jboss.pnc.dto.GroupBuild;
import org.jboss.pnc.dto.GroupConfiguration;
import org.jboss.pnc.dto.requests.GroupBuildRequest;
import org.jboss.pnc.enums.BuildStatus;
import org.jboss.pnc.enums.RebuildMode;
import org.jboss.pnc.integration.mock.RemoteBuildsCleanerMock;
import org.jboss.pnc.integration.utils.ResponseUtils;
import org.jboss.pnc.integration_new.setup.Deployments;
import org.jboss.pnc.rest.api.parameters.BuildParameters;
import org.jboss.pnc.rest.api.parameters.BuildsFilterParameters;
import org.jboss.pnc.rest.api.parameters.GroupBuildParameters;
import org.jboss.pnc.test.category.ContainerTest;
import org.jboss.shrinkwrap.api.spec.EnterpriseArchive;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.Instant;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.jboss.pnc.integration_new.setup.RestClientConfiguration.asUser;

@RunAsClient
@RunWith(Arquillian.class)
@Category(ContainerTest.class)
public class BuildTest {

    private static final Logger logger = LoggerFactory.getLogger(BuildTest.class);

    private BuildConfigurationClient buildConfigurationClient = new BuildConfigurationClient(asUser());

    private GroupConfigurationClient groupConfigurationClient = new GroupConfigurationClient(asUser());

    private GroupBuildClient groupBuildClient = new GroupBuildClient(asUser());

    private BuildClient buildClient = new BuildClient(asUser());

    @Deployment
    public static EnterpriseArchive deploy() {
        final EnterpriseArchive ear = Deployments.testEarForInContainerTest(BuildTest.class);
        Deployments.addBuildExecutorMock(ear);
        JavaArchive coordinatorJar = ear.getAsType(JavaArchive.class, AbstractTest.COORDINATOR_JAR);
        coordinatorJar.addAsManifestResource("beans-use-mock-remote-clients.xml", "beans.xml");
        coordinatorJar.addClass(RemoteBuildsCleanerMock.class);
        return ear;
    }

    @Test
    public void shouldTriggerBuildAndFinishWithoutProblems() throws ClientException {
        // with
        BuildConfiguration buildConfiguration = buildConfigurationClient.getAll().iterator().next();

        // when
        Build build = buildConfigurationClient.trigger(buildConfiguration.getId(), getPersistentParameters(true));
        assertThat(build).isNotNull().extracting("id").isNotNull().isNotEqualTo("");

        EnumSet<BuildStatus> isIn = EnumSet.of(BuildStatus.SUCCESS);
        ResponseUtils.waitSynchronouslyFor(() -> buildToFinish(build.getId(), isIn, null), 15, TimeUnit.SECONDS);
    }

    @Test
    public void shouldTriggerGroupBuildAndFinishWithoutProblems() throws ClientException {
        // given
        GroupConfiguration groupConfig = groupConfigurationClient.getAll().iterator().next();

        // when
        GroupBuildParameters groupBuildParameters = new GroupBuildParameters();
        groupBuildParameters.setRebuildMode(RebuildMode.FORCE);

        GroupBuild groupBuild = groupConfigurationClient.trigger(
                groupConfig.getId(),
                groupBuildParameters,
                GroupBuildRequest.builder().buildConfigurationRevisions(new ArrayList<>()).build());
        assertThat(groupBuild).isNotNull().extracting("id").isNotNull().isNotEqualTo("");
        // then
        EnumSet<BuildStatus> isIn = EnumSet.of(BuildStatus.SUCCESS);
        EnumSet<BuildStatus> isNotIn = EnumSet.of(BuildStatus.REJECTED);
        ResponseUtils.waitSynchronouslyFor(
                () -> groupBuildToFinish(groupBuild.getId(), isIn, isNotIn),
                15,
                TimeUnit.SECONDS);
    }

    @Test
    public void shouldRejectGroupBuildWithNoRebuildsRequired() throws ClientException {
        // given
        GroupConfiguration groupConfig = groupConfigurationClient.getAll().iterator().next();

        // and after one build is done
        GroupBuildParameters groupBuildParameters = new GroupBuildParameters();
        groupBuildParameters.setRebuildMode(RebuildMode.FORCE);
        GroupBuild groupBuild1 = groupConfigurationClient.trigger(
                groupConfig.getId(),
                groupBuildParameters,
                GroupBuildRequest.builder().buildConfigurationRevisions(new ArrayList<>()).build());
        assertThat(groupBuild1).isNotNull().extracting("id").isNotNull().isNotEqualTo("");

        ResponseUtils.waitSynchronouslyFor(
                () -> groupBuildToFinish(groupBuild1.getId(), EnumSet.of(BuildStatus.SUCCESS), null),
                15,
                TimeUnit.SECONDS);

        // when next build is triggered
        GroupBuild groupBuild2 = groupConfigurationClient.trigger(
                groupConfig.getId(),
                new GroupBuildParameters(),
                GroupBuildRequest.builder().buildConfigurationRevisions(new ArrayList<>()).build());

        // then
        EnumSet<BuildStatus> isIn = EnumSet.of(BuildStatus.NO_REBUILD_REQUIRED);
        EnumSet<BuildStatus> isNotIn = EnumSet.of(BuildStatus.SUCCESS, BuildStatus.REJECTED);
        ResponseUtils.waitSynchronouslyFor(
                () -> groupBuildToFinish(groupBuild2.getId(), isIn, isNotIn),
                15,
                TimeUnit.SECONDS);
    }

    @Test
    public void shouldTriggerGroupBuildWithBCInRevisionAndFinishWithoutProblems() throws ClientException {
        // given
        GroupConfiguration groupConfiguration = groupConfigurationClient.getAll().iterator().next();
        assertThat(groupConfiguration.getBuildConfigs()).isNotEmpty();

        List<BuildConfigurationRevisionRef> buildConfigurationRevisions = new ArrayList<>();
        BuildConfigurationRevision buildConfigurationRevision = BuildConfigurationRevision.builder()
                .id(groupConfiguration.getBuildConfigs().keySet().iterator().next())
                .rev(1)
                .name(groupConfiguration.getName())
                .build();
        buildConfigurationRevisions.add(buildConfigurationRevision);

        GroupBuildRequest groupConfigWithAuditedBCsRest = GroupBuildRequest.builder()
                .buildConfigurationRevisions(buildConfigurationRevisions)
                .build();
        GroupBuildParameters groupBuildParameters = new GroupBuildParameters();
        groupBuildParameters.setRebuildMode(RebuildMode.FORCE);

        // when
        GroupBuild groupBuild = groupConfigurationClient
                .trigger(groupConfiguration.getId(), groupBuildParameters, groupConfigWithAuditedBCsRest);
        // then
        assertThat(groupBuild).isNotNull().extracting("id").isNotNull().isNotEqualTo("");

        EnumSet<BuildStatus> isIn = EnumSet.of(BuildStatus.SUCCESS);
        EnumSet<BuildStatus> isNotIn = EnumSet.of(BuildStatus.REJECTED);
        ResponseUtils.waitSynchronouslyFor(
                () -> groupBuildToFinish(groupBuild.getId(), isIn, isNotIn),
                15,
                TimeUnit.SECONDS);
    }

    @Test
    public void shouldTriggerBuildWithADependencyAndFinishWithoutProblems() throws ClientException {
        // given - A BC with a dependency on pnc-1.0.0.DR1
        BuildConfiguration buildConfigurationParent = buildConfigurationClient
                .getAll(Optional.empty(), Optional.of("name==dependency-analysis-1.3"))
                .iterator()
                .next();

        // Update dependency
{'fraction_non_alphanumeric': 0.09717942640436122, 'fraction_numerical': 0.003970135103105002, 'mean_word_length': 3.81374786081004, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6081926', 'n_tokens_mistral': 4634, 'n_tokens_neox': 4415, 'n_words': 828}