|
| 1 | +/** |
| 2 | + * Copyright (C) 2011-2018 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.commonjava.indy.client.core.IndyClientException; |
| 19 | +import org.commonjava.indy.client.core.IndyClientModule; |
| 20 | +import org.commonjava.indy.ftest.core.AbstractContentManagementTest; |
| 21 | +import org.commonjava.indy.ftest.core.category.EventDependent; |
| 22 | +import org.commonjava.indy.model.core.HostedRepository; |
| 23 | +import org.commonjava.indy.promote.client.IndyPromoteClientModule; |
| 24 | +import org.commonjava.indy.promote.model.PathsPromoteRequest; |
| 25 | +import org.commonjava.indy.promote.model.PathsPromoteResult; |
| 26 | +import org.junit.Before; |
| 27 | +import org.junit.Test; |
| 28 | +import org.junit.experimental.categories.Category; |
| 29 | + |
| 30 | +import java.io.ByteArrayInputStream; |
| 31 | +import java.util.Collection; |
| 32 | +import java.util.Collections; |
| 33 | + |
| 34 | +import static org.commonjava.indy.pkg.PackageTypeConstants.PKG_TYPE_MAVEN; |
| 35 | +import static org.hamcrest.CoreMatchers.equalTo; |
| 36 | +import static org.junit.Assert.assertThat; |
| 37 | + |
| 38 | +/** |
| 39 | + * Check that metadata in a hosted repo is merged with content from a new readonly hosted repository when it is promoted by path. |
| 40 | + * <br/> |
| 41 | + * GIVEN: |
| 42 | + * <ul> |
| 43 | + * <li>HostedRepository A contains path for GA + V1</li> |
| 44 | + * <li>HostedRepository B contains path for GA + V2</li> |
| 45 | + * <li>HostedRepository A is readonly</li> |
| 46 | + * </ul> |
| 47 | + * <br/> |
| 48 | + * WHEN: |
| 49 | + * <ul> |
| 50 | + * <li>HostedRepository B is merged into HostedRepository A</li> |
| 51 | + * </ul> |
| 52 | + * <br/> |
| 53 | + * THEN: |
| 54 | + * <ul> |
| 55 | + * <li>HostedRepository A's metadata path P should reflect values in both V1 and V2</li> |
| 56 | + * </ul> |
| 57 | + */ |
| 58 | +public class HostedMetadataRemergedOnPathPromoteToReadonlyHostedTest |
| 59 | + extends AbstractContentManagementTest |
| 60 | +{ |
| 61 | + private static final String HOSTED_A_NAME= "A"; |
| 62 | + private static final String HOSTED_B_NAME= "B"; |
| 63 | + |
| 64 | + private static final String A_VERSION = "1.0"; |
| 65 | + private static final String B_VERSION = "1.1"; |
| 66 | + |
| 67 | + private static final String PATH = "/org/foo/bar/maven-metadata.xml"; |
| 68 | + |
| 69 | + private static final String POM_PATH_TEMPLATE = "/org/foo/bar/%version%/bar-%version%.pom"; |
| 70 | + |
| 71 | + /* @formatter:off */ |
| 72 | + private static final String REPO_CONTENT_TEMPLATE = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + |
| 73 | + "<metadata>\n" + |
| 74 | + " <groupId>org.foo</groupId>\n" + |
| 75 | + " <artifactId>bar</artifactId>\n" + |
| 76 | + " <versioning>\n" + |
| 77 | + " <release>%version%</release>\n" + |
| 78 | + " <latest>%version%</latest>\n" + |
| 79 | + " <versions>\n" + |
| 80 | + " <version>%version%</version>\n" + |
| 81 | + " </versions>\n" + |
| 82 | + " <lastUpdated>20150722164334</lastUpdated>\n" + |
| 83 | + " </versioning>\n" + |
| 84 | + "</metadata>\n"; |
| 85 | + /* @formatter:on */ |
| 86 | + |
| 87 | + private static final String POM_CONTENT_TEMPLATE = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + |
| 88 | + "<project>\n" + |
| 89 | + " <modelVersion>4.0.0</modelVersion>\n" + |
| 90 | + " <groupId>org.foo</groupId>\n" + |
| 91 | + " <artifactId>bar</artifactId>\n" + |
| 92 | + " <version>%version%</version>\n" + |
| 93 | + " <name>Bar</name>\n" + |
| 94 | + " <dependencies>\n" + |
| 95 | + " <dependency>\n" + |
| 96 | + " <groupId>org.something</groupId>\n" + |
| 97 | + " <artifactId>oh</artifactId>\n" + |
| 98 | + " <version>1.0.1</version>\n" + |
| 99 | + " </dependency>\n" + |
| 100 | + " </dependencies>\n" + |
| 101 | + "</project>\n"; |
| 102 | + /* @formatter:on */ |
| 103 | + |
| 104 | + /* @formatter:off */ |
| 105 | + private static final String AFTER_PROMOTE_CONTENT = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + |
| 106 | + "<metadata>\n" + |
| 107 | + " <groupId>org.foo</groupId>\n" + |
| 108 | + " <artifactId>bar</artifactId>\n" + |
| 109 | + " <versioning>\n" + |
| 110 | + " <release>1.1</release>\n" + |
| 111 | + " <latest>1.1</latest>\n" + |
| 112 | + " <versions>\n" + |
| 113 | + " <version>1.0</version>\n" + |
| 114 | + " <version>1.1</version>\n" + |
| 115 | + " </versions>\n" + |
| 116 | + " <lastUpdated>20150722164334</lastUpdated>\n" + |
| 117 | + " </versioning>\n" + |
| 118 | + "</metadata>\n"; |
| 119 | + /* @formatter:on */ |
| 120 | + |
| 121 | + private HostedRepository a; |
| 122 | + private HostedRepository b; |
| 123 | + private String aPreContent; |
| 124 | + |
| 125 | + private String aPomPath; |
| 126 | + private String aPomContent; |
| 127 | + |
| 128 | + private String bPomPath; |
| 129 | + private String bPomContent; |
| 130 | + |
| 131 | + private final IndyPromoteClientModule promote = new IndyPromoteClientModule(); |
| 132 | + |
| 133 | + @Before |
| 134 | + public void setupRepos() |
| 135 | + throws IndyClientException |
| 136 | + { |
| 137 | + String message = "test setup"; |
| 138 | + |
| 139 | + a = client.stores().create( new HostedRepository( PKG_TYPE_MAVEN, HOSTED_A_NAME ), message, HostedRepository.class ); |
| 140 | + b = client.stores().create( new HostedRepository( PKG_TYPE_MAVEN, HOSTED_B_NAME ), message, HostedRepository.class ); |
| 141 | + |
| 142 | + |
| 143 | + aPomPath = POM_PATH_TEMPLATE.replaceAll( "%version%", A_VERSION ); |
| 144 | + aPomContent = POM_CONTENT_TEMPLATE.replaceAll( "%version%", A_VERSION ); |
| 145 | + |
| 146 | + client.content() |
| 147 | + .store( a.getKey(), aPomPath, new ByteArrayInputStream( |
| 148 | + aPomContent.getBytes() ) ); |
| 149 | + |
| 150 | + aPreContent = REPO_CONTENT_TEMPLATE.replaceAll( "%version%", A_VERSION ); |
| 151 | + |
| 152 | + client.content() |
| 153 | + .store( a.getKey(), PATH, new ByteArrayInputStream( |
| 154 | + aPreContent.getBytes() ) ); |
| 155 | + |
| 156 | + // |
| 157 | + bPomPath = POM_PATH_TEMPLATE.replaceAll( "%version%", B_VERSION ); |
| 158 | + bPomContent = POM_CONTENT_TEMPLATE.replaceAll( "%version%", B_VERSION ); |
| 159 | + |
| 160 | + client.content() |
| 161 | + .store( b.getKey(), bPomPath, new ByteArrayInputStream( |
| 162 | + bPomContent.getBytes() ) ); |
| 163 | + |
| 164 | + a.setReadonly( true ); |
| 165 | + client.stores().update( a, message ); |
| 166 | + |
| 167 | + } |
| 168 | + |
| 169 | + @Test |
| 170 | + @Category( EventDependent.class ) |
| 171 | + public void run() |
| 172 | + throws Exception |
| 173 | + { |
| 174 | + // verify our initial state |
| 175 | + assertMetadataContent( a, PATH, aPreContent ); |
| 176 | + |
| 177 | + PathsPromoteRequest request = new PathsPromoteRequest( b.getKey(), a.getKey(), bPomPath ); |
| 178 | + |
| 179 | + // Pre-existing maven-metadata.xml should NOT cause a failure! |
| 180 | + request.setFailWhenExists( true ); |
| 181 | + |
| 182 | + PathsPromoteResult response = promote.promoteByPath( request ); |
| 183 | + |
| 184 | + assertThat( response.succeeded(), equalTo( true ) ); |
| 185 | + |
| 186 | + waitForEventPropagation(); |
| 187 | + |
| 188 | + // Promotion to repo A should trigger re-merge of maven-metadata.xml, adding the version from repo B to that in A. |
| 189 | + assertMetadataContent( a, PATH, AFTER_PROMOTE_CONTENT ); |
| 190 | + |
| 191 | + } |
| 192 | + |
| 193 | + @Override |
| 194 | + protected Collection<IndyClientModule> getAdditionalClientModules() |
| 195 | + { |
| 196 | + return Collections.singleton( promote ); |
| 197 | + } |
| 198 | + |
| 199 | + @Override |
| 200 | + protected boolean createStandardTestStructures() |
| 201 | + { |
| 202 | + return false; |
| 203 | + } |
| 204 | + |
| 205 | +} |
0 commit comments