|
| 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 | +} |
0 commit comments