Skip to content

Commit

Permalink
final cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
stefdev49 committed Jul 24, 2023
1 parent 8d639cc commit a774b03
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 51 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Sonatype Nexus (TM) Open Source Version
* Copyright (c) 2018-present 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.repository.composer.internal;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.junit.Before;
import org.junit.Test;
import org.sonatype.nexus.repository.Repository;
import org.sonatype.nexus.repository.http.HttpStatus;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.sonatype.nexus.testsuite.testsupport.FormatClientSupport.status;

public class ComposerGroupIT
extends ComposerITSupport
{
private static final String COMPOSER_TEST_GROUP = "composer-test-group";

private static final String COMPOSER_TEST_HOSTED = "composer-test-hosted";

private ComposerClient groupClient;

private ComposerClient hostedClient;

@Before
public void setup() throws Exception {
startServer();


hostedClient= composerClient(repos.createComposerHosted(COMPOSER_TEST_HOSTED));
Repository groupRepo = repos.createComposerGroup(COMPOSER_TEST_GROUP, COMPOSER_TEST_HOSTED);
groupClient = composerClient(groupRepo);
}

@Test
public void nonExistingPackageProduces404() throws Exception {
assertThat(status(groupClient.get(BAD_PATH)), is(HttpStatus.NOT_FOUND));
}

@Test
public void putAndGetOk() throws Exception {
// given : check package not exists
assertThat(status(groupClient.get(VALID_ZIPBALL_URL)), is(HttpStatus.NOT_FOUND));

// when : upload package on hosted
assertThat(hostedClient.put(NAME_PACKAGES + "/upload/" + VALID_ZIPBALL_BASE_URL, testData.resolveFile(ZIPBALL_FILE_NAME)), is(200));

// then : download on group
assertThat(status(groupClient.get(VALID_ZIPBALL_URL)), is(HttpStatus.OK));
}

@Test
public void checkRestAPI() throws Exception {
assertThat(status(groupClient.get("/service/rest/v1/repositories")), is(HttpStatus.OK));
}

@Test
public void badPutGroupConfigurationByAPI() throws Exception {
assertThat(groupClient.put("/service/rest/v1/repositories/composer/group/" + COMPOSER_TEST_GROUP, "bad request"), is(HttpStatus.BAD_REQUEST));
}

@Test
public void getAndUpdateGroupConfigurationByAPI() throws Exception {
JsonObject expected = new JsonParser().parse("{\"name\":\"composer-test-group\",\"format\":\"composer\",\"online\":true,\"storage\":{\"blobStoreName\":\"default\",\"strictContentTypeValidation\":true},\"group\":{\"memberNames\":[\"composer-test-hosted\"],\"writableMember\":\"None\"},\"type\":\"group\"}").getAsJsonObject();
getAndUpdateConfig(expected, COMPOSER_TEST_GROUP, "group", groupClient);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,19 @@

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.junit.Before;
import org.junit.Test;
import org.sonatype.nexus.repository.Repository;
import org.sonatype.nexus.repository.http.HttpStatus;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.sonatype.nexus.testsuite.testsupport.FormatClientSupport.bytes;
import static org.sonatype.nexus.testsuite.testsupport.FormatClientSupport.status;
import static org.testcontainers.shaded.org.hamcrest.text.MatchesPattern.matchesPattern;

public class ComposerHostedIT
extends ComposerITSupport
{

private static final String BAD_PATH = "/this/path/is/not/valid";

public static final String COMPOSER_TEST_HOSTED = "composer-test-hosted";
private static final String COMPOSER_TEST_HOSTED = "composer-test-hosted";

private ComposerClient hostedClient;

Expand Down Expand Up @@ -68,27 +62,12 @@ public void checkRestAPI() throws Exception {

@Test
public void badPutHostedConfigurationByAPI() throws Exception {
assertThat(hostedClient.put("/service/rest/v1/repositories/composer/proxy/" + COMPOSER_TEST_HOSTED, "bad request"), is(HttpStatus.BAD_REQUEST));
assertThat(hostedClient.put("/service/rest/v1/repositories/composer/hosted/" + COMPOSER_TEST_HOSTED, "bad request"), is(HttpStatus.BAD_REQUEST));
}

@Test
public void getHostedConfigurationByAPI() throws Exception {
CloseableHttpResponse response = hostedClient.get("/service/rest/v1/repositories/composer/hosted/" + COMPOSER_TEST_HOSTED);
assertThat(status(response), is(HttpStatus.OK));
String config = new String(bytes(response));
// convert to json and check some values
JsonObject jsonConfig = (JsonObject) new JsonParser().parse(config);
public void getAndUpdateHostedConfigurationByAPI() throws Exception {
JsonObject expected = new JsonParser().parse("{\"name\":\"composer-test-hosted\",\"format\":\"composer\",\"online\":true,\"storage\":{\"blobStoreName\":\"default\",\"strictContentTypeValidation\":true,\"writePolicy\":\"ALLOW\"},\"cleanup\":null,\"component\":{\"proprietaryComponents\":false},\"type\":\"hosted\"}").getAsJsonObject();
// check url field matches http://localhost:[0-9]*/repository/composer-test-hosted
assertThat(matchesPattern("http://localhost:[0-9]*/repository/" + COMPOSER_TEST_HOSTED).matches(jsonConfig.get("url").getAsString()), is(true));
// compare ignoring url field
jsonConfig.remove("url");
assertThat(jsonConfig, is(expected));

// set online to false, then update repository
jsonConfig.remove("online");
jsonConfig.addProperty("online", false);
int code = hostedClient.put("/service/rest/v1/repositories/composer/hosted/" + COMPOSER_TEST_HOSTED, jsonConfig.toString());
assertThat(code, is(HttpStatus.NO_CONTENT));
getAndUpdateConfig(expected, COMPOSER_TEST_HOSTED, "hosted", hostedClient);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@
*/
package org.sonatype.nexus.repository.composer.internal;

import java.io.IOException;
import java.net.URL;

import javax.annotation.Nonnull;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.junit.After;
import org.ops4j.pax.exam.Configuration;
import org.ops4j.pax.exam.Option;
Expand All @@ -24,6 +28,7 @@
import org.sonatype.nexus.pax.exam.NexusPaxExamSupport;
import org.sonatype.nexus.repository.Repository;
import org.sonatype.nexus.repository.composer.internal.fixtures.RepositoryRuleComposer;
import org.sonatype.nexus.repository.http.HttpStatus;
import org.sonatype.nexus.repository.storage.Asset;
import org.sonatype.nexus.repository.storage.MetadataNodeEntityAdapter;
import org.sonatype.nexus.repository.storage.StorageFacet;
Expand All @@ -34,6 +39,11 @@
import org.junit.Rule;

import static com.google.common.base.Preconditions.checkNotNull;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.sonatype.nexus.testsuite.testsupport.FormatClientSupport.bytes;
import static org.sonatype.nexus.testsuite.testsupport.FormatClientSupport.status;
import static org.testcontainers.shaded.org.hamcrest.text.MatchesPattern.matchesPattern;

abstract public class ComposerITSupport
extends RepositoryITSupport
Expand Down Expand Up @@ -77,6 +87,7 @@ abstract public class ComposerITSupport
protected static final String MIME_TYPE_JSON = "application/json";

protected static final String MIME_TYPE_ZIP = "application/zip";
protected static final String BAD_PATH = "/this/path/is/not/valid";

@Rule
public RepositoryRuleComposer repos = new RepositoryRuleComposer(() -> repositoryManager);
Expand Down Expand Up @@ -141,4 +152,26 @@ protected void startServer() throws Exception {
public void tearDown() throws Exception {
server.stop();
}

protected void getAndUpdateConfig(JsonObject expected, String repoName, String repoType, ComposerClient repoClient) throws IOException {
// when
CloseableHttpResponse response = repoClient.get("/service/rest/v1/repositories/composer/"+ repoType + "/" + repoName);
String config = new String(bytes(response));
JsonObject jsonConfig = (JsonObject) new JsonParser().parse(config);

// then
assertThat(status(response), is(HttpStatus.OK));
// check url field matches http://localhost:[0-9]*/repository/composer-test-<name>
assertThat(matchesPattern("http://localhost:[0-9]*/repository/" + repoName).matches(jsonConfig.get("url").getAsString()), is(true));
// compare ignoring url field
jsonConfig.remove("url");
assertThat(jsonConfig, is(expected));

// set online to false, remove optional connection properties, then update repository
jsonConfig.remove("online");
jsonConfig.remove("connection");
jsonConfig.addProperty("online", false);
int code = repoClient.put("/service/rest/v1/repositories/composer/"+ repoType + "/" + repoName, jsonConfig.toString());
assertThat(code, is(HttpStatus.NO_CONTENT));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.sonatype.nexus.testsuite.testsupport.FormatClientSupport.bytes;
import static org.sonatype.nexus.testsuite.testsupport.FormatClientSupport.status;
import static org.testcontainers.shaded.org.hamcrest.text.MatchesPattern.matchesPattern;

Expand All @@ -35,11 +34,7 @@ public class ComposerProxyIT
{
private static final String FORMAT_NAME = "composer";

private static final String FILE_PACKAGES = NAME_PACKAGES + EXTENSION_JSON;

private static final String BAD_PATH = "/this/path/is/not/valid";

public static final String COMPOSER_TEST_PROXY = "composer-test-proxy";
private static final String COMPOSER_TEST_PROXY = "composer-test-proxy";

private ComposerClient proxyClient;

Expand Down Expand Up @@ -121,29 +116,12 @@ public void badPutProxyConfigurationByAPI() throws Exception {
}

@Test
public void getAndPutProxyConfigurationByAPI() throws Exception {
public void getAndUpdateProxyConfigurationByAPI() throws Exception {
// given
int port = server.getPort();
JsonObject expected = new JsonParser().parse("{\"name\":\"composer-test-proxy\",\"format\":\"composer\",\"online\":true,\"storage\":{\"blobStoreName\":\"default\",\"strictContentTypeValidation\":true},\"cleanup\":null,\"proxy\":{\"remoteUrl\":\"http://localhost:" + port + "\",\"contentMaxAge\":1440,\"metadataMaxAge\":1440},\"negativeCache\":{\"enabled\":true,\"timeToLive\":1440},\"httpClient\":{\"blocked\":false,\"autoBlock\":false,\"connection\":{\"retries\":null,\"userAgentSuffix\":null,\"timeout\":null,\"enableCircularRedirects\":false,\"enableCookies\":false,\"useTrustStore\":false},\"authentication\":null},\"routingRuleName\":null,\"type\":\"proxy\"}").getAsJsonObject();

// when
CloseableHttpResponse response = proxyClient.get("/service/rest/v1/repositories/composer/proxy/" + COMPOSER_TEST_PROXY);
String config = new String(bytes(response));
JsonObject jsonConfig = (JsonObject) new JsonParser().parse(config);

// then
assertThat(status(response), is(HttpStatus.OK));
// check url field matches http://localhost:[0-9]*/repository/composer-test-proxy
assertThat(matchesPattern("http://localhost:[0-9]*/repository/" + COMPOSER_TEST_PROXY).matches(jsonConfig.get("url").getAsString()), is(true));
// compare ignoring url field
jsonConfig.remove("url");
assertThat(jsonConfig, is(expected));

// set online to false, remove optional connection properties, then update repository
jsonConfig.remove("online");
jsonConfig.remove("connection");
jsonConfig.addProperty("online", false);
int code = proxyClient.put("/service/rest/v1/repositories/composer/proxy/" + COMPOSER_TEST_PROXY, jsonConfig.toString());
assertThat(code, is(HttpStatus.NO_CONTENT));
getAndUpdateConfig(expected, COMPOSER_TEST_PROXY, "proxy", proxyClient);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,11 @@ trait ComposerRepoRecipes
createRepository(createHosted(name, 'composer-hosted'))
}

@Nonnull
Repository createComposerGroup(final String name, final String... members)
{
createRepository(createGroup(name, 'composer-group', members))
}

abstract Repository createRepository(final Configuration configuration)
}

0 comments on commit a774b03

Please sign in to comment.