Skip to content

Commit daccef8

Browse files
authored
Add ftest PromoteNpmAndUpdateMetadataTest (#2061)
1 parent ea39ce0 commit daccef8

File tree

4 files changed

+223
-0
lines changed

4 files changed

+223
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/**
2+
* Copyright (C) 2011-2020 Red Hat, Inc. (https://github.com/Commonjava/indy)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.commonjava.indy.pkg.npm.content;
17+
18+
import org.commonjava.indy.model.core.StoreKey;
19+
import org.commonjava.indy.model.core.StoreType;
20+
import org.commonjava.maven.galley.io.SpecialPathManagerImpl;
21+
import org.junit.Test;
22+
23+
import java.util.Optional;
24+
25+
import static org.hamcrest.CoreMatchers.equalTo;
26+
import static org.junit.Assert.assertThat;
27+
import static org.junit.Assert.assertTrue;
28+
29+
public class NPMStoragePathCalculatorTest
30+
{
31+
32+
@Test
33+
public void testNormal()
34+
{
35+
NPMStoragePathCalculator pathCalculator = new NPMStoragePathCalculator( new SpecialPathManagerImpl() );
36+
StoreKey store = new StoreKey("npm", StoreType.hosted, "test");
37+
38+
String tgzPath = "jquery/-/jquery-1.2.3-redhat-1.tgz";
39+
String storagePath = pathCalculator.calculateStoragePath(store, tgzPath );
40+
System.out.println(storagePath);
41+
assertTrue(tgzPath.equals(storagePath));
42+
43+
String rangedTgzPath = "@discoveryjs/json-ext/-/json-ext-0.5.6.tgz";
44+
storagePath = pathCalculator.calculateStoragePath(store, rangedTgzPath );
45+
System.out.println(storagePath);
46+
assertTrue(rangedTgzPath.equals(storagePath));
47+
48+
String packagePath = "jquery/package.json";
49+
storagePath = pathCalculator.calculateStoragePath(store, packagePath );
50+
System.out.println(storagePath);
51+
assertTrue(packagePath.equals(storagePath));
52+
53+
String packagePath2 = "jquery";
54+
storagePath = pathCalculator.calculateStoragePath(store, packagePath2 );
55+
System.out.println(storagePath);
56+
assertTrue(packagePath.equals(storagePath));
57+
58+
String packagePathHttpMeta = "jquery/package.json.http-metadata.json";
59+
storagePath = pathCalculator.calculateStoragePath(store, packagePathHttpMeta );
60+
System.out.println(storagePath);
61+
assertTrue(packagePathHttpMeta.equals(storagePath));
62+
63+
String rangedPackagePath = "@discoveryjs/json-ext/package.json";
64+
storagePath = pathCalculator.calculateStoragePath(store, rangedPackagePath );
65+
System.out.println(storagePath);
66+
assertTrue(rangedPackagePath.equals(storagePath));
67+
68+
String rangedPackagePath2 = "@discoveryjs/json-ext";
69+
storagePath = pathCalculator.calculateStoragePath(store, rangedPackagePath2 );
70+
System.out.println(storagePath);
71+
assertTrue(rangedPackagePath.equals(storagePath));
72+
73+
String rangedPackagePathHttpMeta = "@discoveryjs/json-ext/package.json.http-metadata.json";
74+
storagePath = pathCalculator.calculateStoragePath(store, rangedPackagePathHttpMeta );
75+
System.out.println(storagePath);
76+
assertTrue(rangedPackagePathHttpMeta.equals(storagePath));
77+
}
78+
79+
}
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/**
2+
* Copyright (C) 2011-2020 Red Hat, Inc. (https://github.com/Commonjava/indy)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.commonjava.indy.promote.ftest;
17+
18+
import org.apache.commons.io.IOUtils;
19+
import org.commonjava.indy.client.core.IndyClientModule;
20+
import org.commonjava.indy.client.core.module.IndyContentClientModule;
21+
import org.commonjava.indy.ftest.core.AbstractIndyFunctionalTest;
22+
import org.commonjava.indy.model.core.Group;
23+
import org.commonjava.indy.model.core.HostedRepository;
24+
import org.commonjava.indy.model.core.RemoteRepository;
25+
import org.commonjava.indy.promote.client.IndyPromoteClientModule;
26+
import org.commonjava.indy.promote.model.PathsPromoteRequest;
27+
import org.commonjava.indy.promote.model.PathsPromoteResult;
28+
import org.commonjava.test.http.expect.ExpectationServer;
29+
import org.junit.Before;
30+
import org.junit.Rule;
31+
import org.junit.Test;
32+
33+
import java.io.InputStream;
34+
import java.nio.file.Paths;
35+
import java.util.Collection;
36+
import java.util.Collections;
37+
import java.util.Set;
38+
39+
import static org.commonjava.indy.pkg.npm.model.NPMPackageTypeDescriptor.NPM_PKG_KEY;
40+
import static org.hamcrest.CoreMatchers.*;
41+
import static org.junit.Assert.assertThat;
42+
43+
/**
44+
* GIVEN:
45+
* <ul>
46+
* <li>HostedRepository A with version V1 of x.tgz</li>
47+
* <li>Group G contains HostedRepository A</li>
48+
* <li>RemoteRepository B with version V2 of x.tgz</li>
49+
* </ul>
50+
* <br/>
51+
* WHEN/THEN:
52+
* <ul>
53+
* <li>Get package.json from G, it contains V1</li>
54+
* <li>RemoteRepository B with path x.tgz is promoted to A</li>
55+
* <li>Get package.json from G, it contains both V1 and V2</li>
56+
* </ul>
57+
*/
58+
public class PromoteNpmAndUpdateMetadataTest
59+
extends AbstractIndyFunctionalTest
60+
{
61+
private static final String REMOTE = "remote";
62+
63+
private final String TGZ_V1 = "json-ext-0.5.6.tgz";
64+
private final String PATH_V1 = "/@discoveryjs/json-ext/-/" + TGZ_V1;
65+
66+
private final String TGZ_V2 = "json-ext-0.5.7.tgz";
67+
private final String PATH_V2 = "/@discoveryjs/json-ext/-/" + TGZ_V2;
68+
69+
private final String metadataPath = "/@discoveryjs/json-ext";
70+
71+
protected RemoteRepository source;
72+
73+
protected HostedRepository target;
74+
75+
protected Group group;
76+
77+
protected final IndyPromoteClientModule promotions = new IndyPromoteClientModule();
78+
79+
@Rule
80+
public ExpectationServer server = new ExpectationServer( "repos" );
81+
82+
@Before
83+
public void setup() throws Exception
84+
{
85+
final String RES_ROOT = this.getClass().getSimpleName();
86+
87+
final String changelog = "Setup " + name.getMethodName();
88+
89+
server.expect( server.formatUrl( REMOTE, PATH_V2 ), 200,
90+
readTestResourceAsStream( Paths.get(RES_ROOT, TGZ_V2).toString() ) );
91+
92+
source = new RemoteRepository( NPM_PKG_KEY, "source", server.formatUrl( REMOTE ) );
93+
source = client.stores().create( source, changelog, RemoteRepository.class );
94+
95+
target = new HostedRepository( NPM_PKG_KEY, "target" );
96+
target = client.stores().create( target, changelog, HostedRepository.class );
97+
98+
group = new Group( NPM_PKG_KEY, "test", target.getKey() );
99+
client.stores().create( group, changelog, Group.class );
100+
101+
// Store version V1 of x.tgz to HostedRepository A
102+
IndyContentClientModule c = client.content();
103+
c.store(target.getKey(), PATH_V1, readTestResourceAsStream( Paths.get(RES_ROOT, TGZ_V1).toString() ));
104+
105+
System.out.println(">>>> Prepare done!");
106+
}
107+
108+
@Override
109+
protected Collection<IndyClientModule> getAdditionalClientModules()
110+
{
111+
return Collections.singletonList( promotions );
112+
}
113+
114+
@Test
115+
public void run() throws Exception
116+
{
117+
IndyContentClientModule c = client.content();
118+
119+
// Check the version before promotion
120+
try (InputStream is = c.get(group.getKey(), metadataPath ))
121+
{
122+
String json = IOUtils.toString(is);
123+
assertThat( json.contains(TGZ_V1), equalTo( true ) );
124+
}
125+
126+
// Promote
127+
IndyPromoteClientModule promoteClient = client.module(IndyPromoteClientModule.class);
128+
PathsPromoteResult result = promoteClient.promoteByPath( new PathsPromoteRequest( source.getKey(), target.getKey(), PATH_V2 ) );
129+
130+
// Normal post promotion checks
131+
Set<String> completed = result.getCompletedPaths();
132+
assertThat( completed, notNullValue() );
133+
assertThat( completed.size(), equalTo( 1 ) );
134+
assertThat( result.getError(), nullValue() );
135+
136+
// Check the versions after promotion. Both versions should be there.
137+
try (InputStream is = c.get(group.getKey(), metadataPath ))
138+
{
139+
String json = IOUtils.toString(is);
140+
assertThat( json.contains(TGZ_V1), equalTo( true ) );
141+
assertThat( json.contains(TGZ_V2), equalTo( true ) );
142+
}
143+
}
144+
}
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)