001package org.eclipse.aether.internal.impl.checksum;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.util.Collections;
023import java.util.HashMap;
024import java.util.Map;
025
026import org.eclipse.aether.DefaultRepositorySystemSession;
027import org.eclipse.aether.RepositorySystemSession;
028import org.eclipse.aether.artifact.Artifact;
029import org.eclipse.aether.artifact.DefaultArtifact;
030import org.eclipse.aether.impl.RepositorySystemLifecycle;
031import org.eclipse.aether.internal.impl.DefaultRepositorySystemLifecycle;
032import org.eclipse.aether.internal.test.util.TestUtils;
033import org.eclipse.aether.spi.checksums.TrustedChecksumsSource;
034import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactory;
035import org.junit.Before;
036import org.junit.Test;
037
038import static org.hamcrest.MatcherAssert.assertThat;
039import static org.hamcrest.Matchers.aMapWithSize;
040import static org.hamcrest.Matchers.anEmptyMap;
041import static org.hamcrest.Matchers.hasEntry;
042import static org.hamcrest.Matchers.is;
043import static org.hamcrest.Matchers.notNullValue;
044import static org.hamcrest.Matchers.nullValue;
045import static org.junit.Assume.assumeThat;
046
047public abstract class FileTrustedChecksumsSourceTestSupport
048{
049    private static final Artifact ARTIFACT_WITHOUT_CHECKSUM = new DefaultArtifact( "test:test:1.0" );
050
051    private static final Artifact ARTIFACT_WITH_CHECKSUM = new DefaultArtifact( "test:test:2.0" );
052
053    private static final String ARTIFACT_TRUSTED_CHECKSUM = "trustedChecksum";
054
055    private DefaultRepositorySystemSession session;
056
057    private ChecksumAlgorithmFactory checksumAlgorithmFactory;
058
059    private RepositorySystemLifecycle repositorySystemLifecycle;
060
061    private FileTrustedChecksumsSourceSupport subject;
062
063    private boolean checksumWritten;
064
065    @Before
066    public void before() throws Exception
067    {
068        session = TestUtils.newSession();
069        // populate local repository
070        checksumAlgorithmFactory = new Sha1ChecksumAlgorithmFactory();
071        repositorySystemLifecycle = new DefaultRepositorySystemLifecycle();
072        subject = prepareSubject( repositorySystemLifecycle );
073        checksumWritten = false;
074
075        DefaultRepositorySystemSession prepareSession = new DefaultRepositorySystemSession( session );
076        enableSource( prepareSession );
077        TrustedChecksumsSource.Writer writer = subject.getTrustedArtifactChecksumsWriter( prepareSession );
078        if ( writer != null )
079        {
080            HashMap<String, String> checksums = new HashMap<>();
081            checksums.put( checksumAlgorithmFactory.getName(), ARTIFACT_TRUSTED_CHECKSUM );
082            writer.addTrustedArtifactChecksums( ARTIFACT_WITH_CHECKSUM, prepareSession.getLocalRepository(),
083                    Collections.singletonList( checksumAlgorithmFactory ), checksums );
084            checksumWritten = true;
085        }
086    }
087
088    protected abstract FileTrustedChecksumsSourceSupport prepareSubject( RepositorySystemLifecycle lifecycle );
089
090    protected abstract void enableSource( DefaultRepositorySystemSession session );
091
092    @Test
093    public void notEnabled()
094    {
095        assertThat(
096                subject.getTrustedArtifactChecksums(
097                        session,
098                        ARTIFACT_WITH_CHECKSUM,
099                        session.getLocalRepository(),
100                        Collections.singletonList( checksumAlgorithmFactory )
101                ),
102                nullValue()
103        );
104    }
105
106    @Test
107    public void noProvidedArtifactChecksum()
108    {
109        enableSource( session );
110        Map<String, String> providedChecksums = subject.getTrustedArtifactChecksums(
111                session,
112                ARTIFACT_WITHOUT_CHECKSUM,
113                session.getLocalRepository(),
114                Collections.singletonList( checksumAlgorithmFactory )
115        );
116        assertThat( providedChecksums, notNullValue() );
117        assertThat( providedChecksums, anEmptyMap() );
118    }
119
120    @Test
121    public void haveProvidedArtifactChecksum()
122    {
123        assumeThat( checksumWritten, is( true ) );
124        enableSource( session );
125        Map<String, String> providedChecksums = subject.getTrustedArtifactChecksums(
126                session,
127                ARTIFACT_WITH_CHECKSUM,
128                session.getLocalRepository(),
129                Collections.singletonList( checksumAlgorithmFactory )
130        );
131        assertThat( providedChecksums, notNullValue() );
132        assertThat( providedChecksums, aMapWithSize( 1 ) );
133        assertThat( providedChecksums, hasEntry( checksumAlgorithmFactory.getName(), ARTIFACT_TRUSTED_CHECKSUM ) );
134    }
135}