View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.internal.impl.checksum;
20  
21  import java.util.Collections;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  
26  import org.eclipse.aether.RepositorySystemSession;
27  import org.eclipse.aether.artifact.Artifact;
28  import org.eclipse.aether.artifact.DefaultArtifact;
29  import org.eclipse.aether.repository.RemoteRepository;
30  import org.eclipse.aether.spi.checksums.TrustedChecksumsSource;
31  import org.eclipse.aether.spi.connector.ArtifactDownload;
32  import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactory;
33  import org.junit.Before;
34  import org.junit.Test;
35  
36  import static org.hamcrest.MatcherAssert.assertThat;
37  import static org.hamcrest.Matchers.hasEntry;
38  import static org.hamcrest.Matchers.notNullValue;
39  import static org.hamcrest.Matchers.nullValue;
40  import static org.mockito.ArgumentMatchers.eq;
41  import static org.mockito.Mockito.mock;
42  import static org.mockito.Mockito.when;
43  
44  public class TrustedToProvidedChecksumsSourceAdapterTest {
45      private final Artifact artifactWithChecksum = new DefaultArtifact("g:a:v1");
46  
47      private final Artifact artifactWithoutChecksum = new DefaultArtifact("g:a:v2");
48  
49      private final RemoteRepository repository =
50              new RemoteRepository.Builder("repo", "default", "https://example.com").build();
51  
52      private final List<ChecksumAlgorithmFactory> checksums =
53              Collections.singletonList(new Sha1ChecksumAlgorithmFactory());
54  
55      private final RepositorySystemSession session = mock(RepositorySystemSession.class);
56  
57      private final TrustedChecksumsSource trustedChecksumsSource = mock(TrustedChecksumsSource.class);
58  
59      private TrustedToProvidedChecksumsSourceAdapter adapter;
60  
61      @Before
62      public void before() {
63          HashMap<String, String> result = new HashMap<>();
64          result.put(Sha1ChecksumAlgorithmFactory.NAME, "foo");
65          when(trustedChecksumsSource.getTrustedArtifactChecksums(
66                          eq(session), eq(artifactWithChecksum), eq(repository), eq(checksums)))
67                  .thenReturn(result);
68          adapter = new TrustedToProvidedChecksumsSourceAdapter(
69                  Collections.singletonMap("trusted", trustedChecksumsSource));
70      }
71  
72      @Test
73      public void testSimplePositive() {
74          ArtifactDownload transfer = new ArtifactDownload();
75          transfer.setArtifact(artifactWithChecksum);
76          Map<String, String> chk = adapter.getProvidedArtifactChecksums(session, transfer, repository, checksums);
77          assertThat(chk, notNullValue());
78          assertThat(chk, hasEntry(Sha1ChecksumAlgorithmFactory.NAME, "foo"));
79      }
80  
81      @Test
82      public void testSimpleNegative() {
83          ArtifactDownload transfer = new ArtifactDownload();
84          transfer.setArtifact(artifactWithoutChecksum);
85          Map<String, String> chk = adapter.getProvidedArtifactChecksums(session, transfer, repository, checksums);
86          assertThat(chk, nullValue());
87      }
88  
89      @Test
90      public void testMrmPositive() {
91          RemoteRepository mrm = new RemoteRepository.Builder("mrm", "default", "https://example.com").build();
92          ArtifactDownload transfer = new ArtifactDownload();
93          transfer.setArtifact(artifactWithChecksum);
94          transfer.setRepositories(Collections.singletonList(repository));
95          Map<String, String> chk = adapter.getProvidedArtifactChecksums(session, transfer, mrm, checksums);
96          assertThat(chk, notNullValue());
97          assertThat(chk, hasEntry(Sha1ChecksumAlgorithmFactory.NAME, "foo"));
98      }
99  
100     @Test
101     public void testMrmNegative() {
102         RemoteRepository mrm = new RemoteRepository.Builder("mrm", "default", "https://example.com").build();
103         ArtifactDownload transfer = new ArtifactDownload();
104         transfer.setArtifact(artifactWithoutChecksum);
105         transfer.setRepositories(Collections.singletonList(repository));
106         Map<String, String> chk = adapter.getProvidedArtifactChecksums(session, transfer, mrm, checksums);
107         assertThat(chk, nullValue());
108     }
109 }