This repository has been archived by the owner on Aug 11, 2020. It is now read-only.
/
Nxcm4861ArtifactStoreHelper_storeItemWithChecksumsTest.java
144 lines (130 loc) · 6.66 KB
/
Nxcm4861ArtifactStoreHelper_storeItemWithChecksumsTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* Sonatype Nexus (TM) Open Source Version
* Copyright (c) 2007-2012 Sonatype, Inc.
* All rights reserved. Includes the third-party code listed at http://links.sonatype.com/products/nexus/oss/attributions.
*
* This program and the accompanying materials are made available under the terms of the Eclipse Public License Version 1.0,
* which accompanies this distribution and is available at http://www.eclipse.org/legal/epl-v10.html.
*
* Sonatype Nexus (TM) Professional Version is available from Sonatype, Inc. "Sonatype" and "Sonatype Nexus" are trademarks
* of Sonatype, Inc. Apache Maven is a trademark of the Apache Software Foundation. M2eclipse is a trademark of the
* Eclipse Foundation. All other trademarks are the property of their respective owners.
*/
package org.sonatype.nexus.proxy.maven;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.is;
import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Matchers.anyObject;
import static org.mockito.Mockito.verify;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonatype.configuration.ConfigurationException;
import org.sonatype.nexus.configuration.model.CLocalStorage;
import org.sonatype.nexus.configuration.model.CRepository;
import org.sonatype.nexus.configuration.model.DefaultCRepository;
import org.sonatype.nexus.proxy.AbstractProxyTestEnvironment;
import org.sonatype.nexus.proxy.EnvironmentBuilder;
import org.sonatype.nexus.proxy.ResourceStoreRequest;
import org.sonatype.nexus.proxy.maven.maven2.M2Repository;
import org.sonatype.nexus.proxy.maven.maven2.M2RepositoryConfiguration;
import org.sonatype.nexus.proxy.repository.Repository;
import org.sonatype.nexus.proxy.storage.local.LocalRepositoryStorage;
import org.sonatype.sisu.litmus.testsupport.mock.MockitoRule;
/**
* UT for NXCM-4861: {@link ArtifactStoreHelper#storeItemWithChecksums(boolean, org.sonatype.nexus.proxy.item.AbstractStorageItem)}
* was in some cases causing proxying to kick in (after store item was retrieved from remote -- only in case of procurement reposes
* but still wrong).
*
* Despite extending {@link AbstractProxyTestEnvironment} it actually uses
* a hosted repo only, and using spy determines actually happened invocations.
*/
public class Nxcm4861ArtifactStoreHelper_storeItemWithChecksumsTest
extends AbstractProxyTestEnvironment
{
private Logger log = LoggerFactory.getLogger( Nxcm4861ArtifactStoreHelper_storeItemWithChecksumsTest.class );
private static final String REPO_ID = "inhouse";
@Override
protected EnvironmentBuilder getEnvironmentBuilder()
throws Exception
{
// we need one hosted repo only, so build it
return new EnvironmentBuilder()
{
@Override
public void startService()
{
}
@Override
public void stopService()
{
}
@Override
public void buildEnvironment( AbstractProxyTestEnvironment env )
throws ConfigurationException,
IOException,
ComponentLookupException
{
final PlexusContainer container = env.getPlexusContainer();
// ading one hosted only
final M2Repository repo = (M2Repository) container.lookup( Repository.class, "maven2" );
CRepository repoConf = new DefaultCRepository();
repoConf.setProviderRole( Repository.class.getName() );
repoConf.setProviderHint( "maven2" );
repoConf.setId( REPO_ID );
repoConf.setLocalStorage( new CLocalStorage() );
repoConf.getLocalStorage().setProvider( "file" );
repoConf.getLocalStorage().setUrl(
env.getApplicationConfiguration().getWorkingDirectory(
"proxy/store/inhouse" ).toURI().toURL().toString() );
Xpp3Dom exRepo = new Xpp3Dom( "externalConfiguration" );
repoConf.setExternalConfiguration( exRepo );
M2RepositoryConfiguration exRepoConf = new M2RepositoryConfiguration( exRepo );
exRepoConf.setRepositoryPolicy( RepositoryPolicy.RELEASE );
repo.configure( repoConf );
env.getApplicationConfiguration().getConfigurationModel().addRepository( repoConf );
env.getRepositoryRegistry().addRepository( repo );
}
};
}
@Test
public void retrieveAfterStoreShouldBeLocalOnly()
throws Exception
{
final MavenRepository realMavenRepository =
getRepositoryRegistry().getRepositoryWithFacet( REPO_ID, MavenRepository.class );
final MavenRepository mavenRepository = Mockito.spy( realMavenRepository );
// invoke storeWithChecksums
final String PATH = "/group/artifact/1.0/artifact-1.0.jar";
final ResourceStoreRequest request = new ResourceStoreRequest( PATH );
mavenRepository.storeItemWithChecksums( request, new ByteArrayInputStream( "some fluke content".getBytes() ),
null );
// verify side effects (storage changes)
assertThat( "Artifact should be stored",
mavenRepository.getLocalStorage().containsItem( mavenRepository, new ResourceStoreRequest( PATH ) ),
is( true ) );
assertThat( "Artifact checksum should be stored",
mavenRepository.getLocalStorage().containsItem( mavenRepository,
new ResourceStoreRequest( PATH + ".sha1" ) ),
is( true ) );
// verify params
final ArgumentCaptor<ResourceStoreRequest> ac = ArgumentCaptor.forClass( ResourceStoreRequest.class );
verify( mavenRepository ).retrieveItem( anyBoolean(), ac.capture() );
// we need to have exactly one retrieve invocation
assertThat( "We must have one retrieve happened!", ac.getAllValues().size(), equalTo( 1 ) );
// and that one must be localOnly
assertThat( "Request " + ac.getValue() + " should be localOnly!", ac.getValue().isRequestLocalOnly(),
is( true ) );
}
}