package org.owasp.dependencycheck.analyzer;

import mockit.Expectations;
import mockit.Mock;
import mockit.MockUp;
import mockit.Mocked;
import org.junit.BeforeClass;
import org.junit.Test;
import org.owasp.dependencycheck.data.central.CentralSearch;
import org.owasp.dependencycheck.data.nexus.MavenArtifact;
import org.owasp.dependencycheck.dependency.Dependency;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Collections;
import java.util.List;

import static org.junit.Assert.assertEquals;

/**
 * Tests for the CentralAnalyzer.
 */
public class CentralAnalyzerTest {

    private static final String SHA1_SUM = "my-sha1-sum";

    @BeforeClass
    public static void beforeClass() {
        doNotSleepBetweenRetries();
    }

    @Test
    @SuppressWarnings("PMD.NonStaticInitializer")
    public void testFetchMavenArtifactsWithoutException(@Mocked final CentralSearch centralSearch,
                                                        @Mocked final Dependency dependency)
            throws IOException {

        CentralAnalyzer instance = new CentralAnalyzer();
        instance.searcher = centralSearch;

        final List<MavenArtifact> expectedMavenArtifacts = Collections.emptyList();
        new Expectations() {{
            dependency.getSha1sum();
            returns(SHA1_SUM);

            centralSearch.searchSha1(SHA1_SUM);
            returns(expectedMavenArtifacts);
        }};

        final List<MavenArtifact> actualMavenArtifacts = instance.fetchMavenArtifacts(dependency);

        assertEquals(expectedMavenArtifacts, actualMavenArtifacts);
    }

    @Test
    @SuppressWarnings("PMD.NonStaticInitializer")
    public void testFetchMavenArtifactsWithSporadicIOException(@Mocked final CentralSearch centralSearch,
                                                               @Mocked final Dependency dependency)
            throws IOException {

        CentralAnalyzer instance = new CentralAnalyzer();
        instance.searcher = centralSearch;

        final List<MavenArtifact> expectedMavenArtifacts = Collections.emptyList();
        new Expectations() {{
            dependency.getSha1sum();
            returns(SHA1_SUM);

            centralSearch.searchSha1(SHA1_SUM);
            result = new IOException("Could not connect to MavenCentral (500): Internal Server Error");
            result = new IOException("Could not connect to MavenCentral (500): Internal Server Error");
            result = expectedMavenArtifacts;
        }};

        final List<MavenArtifact> actualMavenArtifacts = instance.fetchMavenArtifacts(dependency);

        assertEquals(expectedMavenArtifacts, actualMavenArtifacts);
    }

    @Test(expected = FileNotFoundException.class)
    @SuppressWarnings("PMD.NonStaticInitializer")
    public void testFetchMavenArtifactsRethrowsFileNotFoundException(@Mocked final CentralSearch centralSearch,
                                                                     @Mocked final Dependency dependency)
            throws IOException {

        CentralAnalyzer instance = new CentralAnalyzer();
        instance.searcher = centralSearch;

        new Expectations() {{
            dependency.getSha1sum();
            returns(SHA1_SUM);

            centralSearch.searchSha1(SHA1_SUM);
            result = new FileNotFoundException("Artifact not found in Central");
        }};

        instance.fetchMavenArtifacts(dependency);
    }

    @Test(expected = IOException.class)
    @SuppressWarnings("PMD.NonStaticInitializer")
    public void testFetchMavenArtifactsAlwaysThrowsIOException(@Mocked final CentralSearch centralSearch,
                                                               @Mocked final Dependency dependency)
            throws IOException {

        CentralAnalyzer instance = new CentralAnalyzer();
        instance.searcher = centralSearch;

        new Expectations() {{
            dependency.getSha1sum();
            returns(SHA1_SUM);

            centralSearch.searchSha1(SHA1_SUM);
            result = new IOException("no internet connection");
        }};

        instance.fetchMavenArtifacts(dependency);
    }

    /**
     * We do not want to waste time in unit tests.
     */
    private static void doNotSleepBetweenRetries() {
        new MockUp<Thread>() {
            @Mock
            void sleep(long millis) {
                // do not sleep
            }
        };
    }
}

@@ -1,8 +1,679 @@
+/*%0A * This file is part of dependency-check-core.%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A *     http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A *%0A * Copyright (c) 2017 Jeremy Long. All Rights Reserved.%0A */%0A
 package 

Add license information