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.jupiter.api.BeforeEach;
34  import org.junit.jupiter.api.Test;
35  
36  import static org.junit.jupiter.api.Assertions.*;
37  import static org.mockito.ArgumentMatchers.eq;
38  import static org.mockito.Mockito.mock;
39  import static org.mockito.Mockito.when;
40  
41  public class TrustedToProvidedChecksumsSourceAdapterTest {
42      private final Artifact artifactWithChecksum = new DefaultArtifact("g:a:v1");
43  
44      private final Artifact artifactWithoutChecksum = new DefaultArtifact("g:a:v2");
45  
46      private final RemoteRepository repository =
47              new RemoteRepository.Builder("repo", "default", "https://example.com").build();
48  
49      private final List<ChecksumAlgorithmFactory> checksums =
50              Collections.singletonList(new Sha1ChecksumAlgorithmFactory());
51  
52      private final RepositorySystemSession session = mock(RepositorySystemSession.class);
53  
54      private final TrustedChecksumsSource trustedChecksumsSource = mock(TrustedChecksumsSource.class);
55  
56      private TrustedToProvidedChecksumsSourceAdapter adapter;
57  
58      @BeforeEach
59      void before() {
60          HashMap<String, String> result = new HashMap<>();
61          result.put(Sha1ChecksumAlgorithmFactory.NAME, "foo");
62          when(trustedChecksumsSource.getTrustedArtifactChecksums(
63                          eq(session), eq(artifactWithChecksum), eq(repository), eq(checksums)))
64                  .thenReturn(result);
65          adapter = new TrustedToProvidedChecksumsSourceAdapter(
66                  Collections.singletonMap("trusted", trustedChecksumsSource));
67      }
68  
69      @Test
70      void testSimplePositive() {
71          ArtifactDownload transfer = new ArtifactDownload();
72          transfer.setArtifact(artifactWithChecksum);
73          Map<String, String> chk = adapter.getProvidedArtifactChecksums(session, transfer, repository, checksums);
74          assertNotNull(chk);
75          assertEquals(chk.get(Sha1ChecksumAlgorithmFactory.NAME), "foo");
76      }
77  
78      @Test
79      void testSimpleNegative() {
80          ArtifactDownload transfer = new ArtifactDownload();
81          transfer.setArtifact(artifactWithoutChecksum);
82          Map<String, String> chk = adapter.getProvidedArtifactChecksums(session, transfer, repository, checksums);
83          assertNull(chk);
84      }
85  
86      @Test
87      void testMrmPositive() {
88          RemoteRepository mrm = new RemoteRepository.Builder("mrm", "default", "https://example.com").build();
89          ArtifactDownload transfer = new ArtifactDownload();
90          transfer.setArtifact(artifactWithChecksum);
91          transfer.setRepositories(Collections.singletonList(repository));
92          Map<String, String> chk = adapter.getProvidedArtifactChecksums(session, transfer, mrm, checksums);
93          assertNotNull(chk);
94          assertEquals(chk.get(Sha1ChecksumAlgorithmFactory.NAME), "foo");
95      }
96  
97      @Test
98      void testMrmNegative() {
99          RemoteRepository mrm = new RemoteRepository.Builder("mrm", "default", "https://example.com").build();
100         ArtifactDownload transfer = new ArtifactDownload();
101         transfer.setArtifact(artifactWithoutChecksum);
102         transfer.setRepositories(Collections.singletonList(repository));
103         Map<String, String> chk = adapter.getProvidedArtifactChecksums(session, transfer, mrm, checksums);
104         assertNull(chk);
105     }
106 }