/**
 * 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.mavenrepositorymanager;

import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.commonjava.indy.client.core.util.UrlUtils;
import org.commonjava.maven.atlas.ident.ref.ProjectVersionRef;
import org.commonjava.maven.atlas.ident.ref.SimpleArtifactRef;
import org.commonjava.maven.atlas.ident.ref.SimpleProjectVersionRef;
import org.jboss.pnc.mavenrepositorymanager.fixture.TestBuildExecution;
import org.jboss.pnc.model.Artifact;
import org.jboss.pnc.spi.repositorymanager.BuildExecution;
import org.jboss.pnc.spi.repositorymanager.RepositoryManagerResult;
import org.jboss.pnc.spi.repositorymanager.model.RepositorySession;
import org.jboss.pnc.test.category.ContainerTest;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import java.util.List;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

@Category(ContainerTest.class)
@Ignore("Uploaded/Built paths should NOT be contained in downloaded/dependency listing!")
public class UploadOneThenDownloadAndVerifyArtifactHasOriginUrlTest
    extends AbstractImportTest
{

    @Test
    public void extractBuildArtifacts_ContainsTwoUploads() throws Exception {
        // create a dummy non-chained build execution and repo session based on it
        BuildExecution execution = new TestBuildExecution();
        RepositorySession rc = driver.createBuildRepository(execution);

        assertThat(rc, notNullValue());

        String baseUrl = rc.getConnectionInfo().getDeployUrl();
        String path = "org/commonjava/indy/indy-core/0.17.0/indy-core-0.17.0.pom";
        String content = "This is a test";

        CloseableHttpClient client = HttpClientBuilder.create().build();

        // upload a couple files related to a single GAV using the repo session deployment url
        // this simulates a build deploying one jar and its associated POM
        final String url = UrlUtils.buildUrl(baseUrl, path);

        assertThat("Failed to upload: " + url, ArtifactUploadUtils.put(client, url, content), equalTo(true));

        // download the two files via the repo session's dependency URL, which will proxy the test http server
        // using the expectations above
        assertThat(download(UrlUtils.buildUrl(baseUrl, path)), equalTo(content));

        ProjectVersionRef pvr = new SimpleProjectVersionRef("org.commonjava.indy", "indy-core", "0.17.0");
        String aref = new SimpleArtifactRef(pvr, "pom", null).toString();

        // extract the "built" artifacts we uploaded above.
        RepositoryManagerResult repositoryManagerResult = rc.extractBuildArtifacts();

        // check that both files are present in extracted result
        List<Artifact> built = repositoryManagerResult.getBuiltArtifacts();
        System.out.println(built);

        assertThat(built, notNullValue());
        assertThat(built.size(), equalTo(1));

        Artifact builtArtifact = built.get(0);
        assertThat(builtArtifact + " doesn't match pom ref: " + aref,
                aref.equals(builtArtifact.getIdentifier()),
                equalTo(true));

        List<Artifact> dependencies = repositoryManagerResult.getDependencies();
        assertThat(dependencies, notNullValue());
        assertThat(dependencies.size(), equalTo(1));

        Artifact dep = dependencies.get(0);
        assertThat(dep.getIdentifier(), equalTo(aref));
        assertTrue(dep.isImported());
        assertThat(dep.getOriginUrl(), notNullValue());

        client.close();
    }

}

@@ -1461,33 +1461,8 @@
 st;%0A
-import org.junit.Ignore;%0A
 impo
@@ -1530,16 +1530,73 @@
 ategory;
+%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;
 %0A%0Aimport
@@ -1834,16 +1834,18 @@
 .class)%0A
+//
 @Ignore(
@@ -2028,16 +2028,142 @@
 Test%0A%7B%0A%0A
+    private static final Logger log = LoggerFactory.getLogger(UploadOneThenDownloadAndVerifyArtifactHasOriginUrlTest.class);%0A%0A
     @Tes
@@ -3466,16 +3466,23 @@
 tring ar
+tifactR
 ef = new
@@ -3560,16 +3560,25 @@
 e %22built
+Artifacts
 %22 artifa
@@ -3780,16 +3780,25 @@
 t%3E built
+Artifacts
  = repos
@@ -3849,32 +3849,64 @@
     
-System.out.println(built
+log.info(%22Built artifacts: %22 + builtArtifacts.toString()
 );%0A%0A
@@ -3929,16 +3929,25 @@
 at(built
+Artifacts
 , notNul
@@ -3981,16 +3981,25 @@
 at(built
+Artifacts
 .size(),
@@ -4051,16 +4051,25 @@
  = built
+Artifacts
 .get(0);
@@ -4135,16 +4135,23 @@
 : %22 + ar
+tifactR
 ef,%0A    
@@ -4164,16 +4164,23 @@
       ar
+tifactR
 ef.equal
@@ -4525,16 +4525,23 @@
 ualTo(ar
+tifactR
 ef));%0A  

Use full names for easier code reading.