Search in sources :

Example 81 with Group

use of org.commonjava.indy.model.core.Group in project indy by Commonjava.

the class PackageStoreListener method onPackageStorageEvent.

/**
 * this listener observes {@link org.commonjava.maven.galley.event.FileStorageEvent}
 * for a tarball file, which means package.json will be cleared when a npm package
 * is uploaded.
 */
public void onPackageStorageEvent(@Observes FileStorageEvent event) {
    if (!event.getTransfer().getPath().endsWith(PACKAGE_TARBALL_EXTENSION)) {
        return;
    }
    logger.info("Package storage: {}", event.getTransfer());
    final StoreKey storeKey = getKey(event);
    final String pkgPath = normalize(parentPath(event.getTransfer().getParent().getPath()));
    final String pkgMetadataPath = normalize(pkgPath, NPM_METADATA_NAME);
    logger.info("Package metadata: store:{} and path: {}", storeKey.getName(), pkgMetadataPath);
    try {
        if (hosted == storeKey.getType()) {
            HostedRepository hosted = dataManager.query().getHostedRepository(PKG_TYPE_NPM, storeKey.getName());
            try {
                doClear(hosted, pkgMetadataPath);
            } catch (final IOException e) {
                logger.error(String.format("Failed to delete: %s from hosted: %s when npm package changed. Error: %s", pkgMetadataPath, hosted, e.getMessage()), e);
            }
            final Set<Group> groups = dataManager.query().getGroupsAffectedBy(storeKey);
            if (groups != null) {
                for (final Group group : groups) {
                    try {
                        doClear(group, pkgMetadataPath);
                    } catch (final IOException e) {
                        logger.error(String.format("Failed to delete: %s from its group: %s when npm package changed. Error: %s", pkgMetadataPath, group, e.getMessage()), e);
                    }
                }
            }
        }
    } catch (final IndyDataException e) {
        logger.warn("Failed to regenerate package.json for npm packages after deployment to: {}" + "\nCannot retrieve associated groups: {}", pkgMetadataPath, e.getMessage());
    }
}
Also used : IndyDataException(org.commonjava.indy.data.IndyDataException) Group(org.commonjava.indy.model.core.Group) IOException(java.io.IOException) StoreKey(org.commonjava.indy.model.core.StoreKey) HostedRepository(org.commonjava.indy.model.core.HostedRepository)

Example 82 with Group

use of org.commonjava.indy.model.core.Group in project indy by Commonjava.

the class PackageMetadataMergerTest method mergeTwoSimplePackageMetadataFiles.

@Test
public void mergeTwoSimplePackageMetadataFiles() throws Exception {
    String path = "jquery";
    HostedRepository h1 = new HostedRepository(NPM_PKG_KEY, "test-hosted-1");
    HostedRepository h2 = new HostedRepository(NPM_PKG_KEY, "test-hosted-2");
    Transfer t1 = cacheProvider.getTransfer(new ConcreteResource(LocationUtils.toLocation(h1), path));
    initTestData(t1, VERSION_META + "package-1.json");
    Transfer t2 = cacheProvider.getTransfer(new ConcreteResource(LocationUtils.toLocation(h2), path));
    initTestData(t2, VERSION_META + "package-2.json");
    Group g = new Group(NPM_PKG_KEY, "test-group", h1.getKey(), h2.getKey());
    List<Transfer> sources = Arrays.asList(t1, t2);
    byte[] output = new PackageMetadataMerger(Collections.emptyList(), mapper).merge(sources, g, path);
    PackageMetadata merged = mapper.readValue(IOUtils.toString(new ByteArrayInputStream(output)), PackageMetadata.class);
    // normal object fields merging verification
    assertThat(merged.getName(), equalTo("jquery"));
    assertThat(merged.getAuthor().getName(), equalTo("JS Foundation and other contributors"));
    assertThat(merged.getRepository().getType(), equalTo("git"));
    assertThat(merged.getRepository().getUrl(), equalTo("git+https://github.com/jquery/jquery.git"));
    assertThat(merged.getReadmeFilename(), equalTo("README.md"));
    assertThat(merged.getHomepage(), equalTo("https://jquery.com"));
    assertThat(merged.getBugs().getUrl(), equalTo("https://github.com/jquery/jquery/issues"));
    assertThat(merged.getLicense().getType(), equalTo("MIT"));
    // dist-tags object merging verification
    assertThat(merged.getDistTags().getBeta(), equalTo("3.2.1-beta.1"));
    assertThat(merged.getDistTags().getLatest(), equalTo("3.2.1"));
    // versions map merging verification
    Map<String, VersionMetadata> versions = merged.getVersions();
    assertThat(versions, notNullValue());
    assertThat(versions.size(), equalTo(2));
    assertThat(versions.get("1.5.1").getVersion(), equalTo("1.5.1"));
    assertThat(versions.get("1.6.2").getVersion(), equalTo("1.6.2"));
    // maintainers list merging verification
    List<UserInfo> maintainers = merged.getMaintainers();
    assertThat(maintainers, notNullValue());
    assertThat(maintainers.size(), equalTo(3));
    assertThat(maintainers.get(0).getName(), equalTo("dmethvin"));
    assertThat(maintainers.get(1).getName(), equalTo("mgol"));
    assertThat(maintainers.get(2).getName(), equalTo("scott.gonzalez"));
    // time map merging verification
    Map<String, String> times = merged.getTime();
    assertThat(times, notNullValue());
    assertThat(times.size(), equalTo(8));
    assertThat(times.get("modified"), equalTo("2017-05-23T10:57:14.309Z"));
    assertThat(times.get("created"), equalTo("2011-04-19T07:19:56.392Z"));
    // users map merging verification
    Map<String, Boolean> users = merged.getUsers();
    assertThat(users, notNullValue());
    assertThat(users.size(), equalTo(10));
    assertThat(users.get("fgribreau"), equalTo(true));
    // keywords list merging verification
    List<String> keywords = merged.getKeywords();
    assertThat(keywords, notNullValue());
    assertThat(keywords.size(), equalTo(4));
    assertThat(keywords.contains("javascript"), equalTo(true));
}
Also used : Group(org.commonjava.indy.model.core.Group) UserInfo(org.commonjava.indy.pkg.npm.model.UserInfo) ByteArrayInputStream(java.io.ByteArrayInputStream) VersionMetadata(org.commonjava.indy.pkg.npm.model.VersionMetadata) PackageMetadata(org.commonjava.indy.pkg.npm.model.PackageMetadata) Transfer(org.commonjava.maven.galley.model.Transfer) ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource) HostedRepository(org.commonjava.indy.model.core.HostedRepository) Test(org.junit.Test)

Example 83 with Group

use of org.commonjava.indy.model.core.Group in project indy by Commonjava.

the class GroupMetaOverlapWithNestedGroupOfHostRepoMetaTest method run.

@Test
@Category(EventDependent.class)
public void run() throws Exception {
    final String repo1 = "repo1";
    final String repo2 = "repo2";
    final String path = "org/foo/bar/maven-metadata.xml";
    /* @formatter:off */
    final String repo1Content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<metadata>\n" + "  <groupId>org.foo</groupId>\n" + "  <artifactId>bar</artifactId>\n" + "  <versioning>\n" + "    <latest>1.0</latest>\n" + "    <release>1.0</release>\n" + "    <versions>\n" + "      <version>1.0</version>\n" + "    </versions>\n" + "    <lastUpdated>20150722164334</lastUpdated>\n" + "  </versioning>\n" + "</metadata>\n";
    /* @formatter:on */
    /* @formatter:off */
    final String repo2Content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<metadata>\n" + "  <groupId>org.foo</groupId>\n" + "  <artifactId>bar</artifactId>\n" + "  <versioning>\n" + "    <latest>1.1</latest>\n" + "    <release>1.1</release>\n" + "    <versions>\n" + "      <version>1.1</version>\n" + "    </versions>\n" + "    <lastUpdated>20150822164334</lastUpdated>\n" + "  </versioning>\n" + "</metadata>\n";
    /* @formatter:on */
    server.expect(server.formatUrl(repo1, path), 200, repo1Content);
    server.expect(server.formatUrl(repo2, path), 200, repo2Content);
    RemoteRepository remote1 = new RemoteRepository(repo1, server.formatUrl(repo1));
    remote1 = client.stores().create(remote1, "adding remote", RemoteRepository.class);
    RemoteRepository remote2 = new RemoteRepository(repo2, server.formatUrl(repo2));
    remote2 = client.stores().create(remote2, "adding remote", RemoteRepository.class);
    // constructs 3 level group structure: top -> middle -> bottom
    // and the bottom group contains repo1 and repo2
    Group bottomGroup = new Group("bottom", remote1.getKey(), remote2.getKey());
    bottomGroup = client.stores().create(bottomGroup, "adding bottom group", Group.class);
    System.out.printf("\n\nBottom group constituents are:\n  %s\n\n", StringUtils.join(bottomGroup.getConstituents(), "\n  "));
    Group middleGroup = new Group("middle", bottomGroup.getKey());
    middleGroup = client.stores().create(middleGroup, "adding middle group", Group.class);
    System.out.printf("\n\nMiddle group constituents are:\n  %s\n\n", StringUtils.join(middleGroup.getConstituents(), "\n  "));
    Group topGroup = new Group("top", middleGroup.getKey());
    topGroup = client.stores().create(topGroup, "adding top group", Group.class);
    System.out.printf("\n\nTop group constituents are:\n  %s\n\n", StringUtils.join(topGroup.getConstituents(), "\n  "));
    InputStream stream = client.content().get(group, topGroup.getName(), path);
    assertThat(stream, notNullValue());
    String metadata = IOUtils.toString(stream);
    stream.close();
    /* @formatter:off */
    final String groupContent = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<metadata>\n" + "  <groupId>org.foo</groupId>\n" + "  <artifactId>bar</artifactId>\n" + "  <versioning>\n" + "    <latest>1.1</latest>\n" + "    <release>1.1</release>\n" + "    <versions>\n" + "      <version>1.0</version>\n" + "      <version>1.1</version>\n" + "    </versions>\n" + "    <lastUpdated>20150822164334</lastUpdated>\n" + "  </versioning>\n" + "</metadata>\n";
    /* @formatter:on */
    assertThat(metadata, equalTo(groupContent));
    final String hostedRepo = "hostedRepo";
    HostedRepository hostedRepository = new HostedRepository(hostedRepo);
    hostedRepository = client.stores().create(hostedRepository, "adding hosted", HostedRepository.class);
    /* @formatter:off */
    final String hostedMetaContent = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<metadata>\n" + "  <groupId>org.foo</groupId>\n" + "  <artifactId>bar</artifactId>\n" + "  <versioning>\n" + "    <latest>1.2</latest>\n" + "    <release>1.2</release>\n" + "    <versions>\n" + "      <version>1.2</version>\n" + "    </versions>\n" + "    <lastUpdated>20150922164334</lastUpdated>\n" + "  </versioning>\n" + "</metadata>\n";
    /* @formatter:on */
    client.content().store(hostedRepository.getKey(), path, new ByteArrayInputStream(hostedMetaContent.getBytes("UTF-8")));
    final PathInfo p = client.content().getInfo(hosted, hostedRepo, path);
    assertThat("hosted metadata should exist", p.exists(), equalTo(true));
    // added hosted repo to bottom group and update
    bottomGroup.addConstituent(hostedRepository);
    client.stores().update(bottomGroup, "add new hosted to bottom");
    System.out.printf("\n\nUpdated bottom group constituents are:\n  %s\n\n", StringUtils.join(bottomGroup.getConstituents(), "\n  "));
    waitForEventPropagation();
    // the top group should reflect the meta file deprecation and re-indexing
    final String gpLevelMetaFilePath = String.format("%s/var/lib/indy/storage/%s-%s/%s", fixture.getBootOptions().getHomeDir(), group.name(), topGroup.getName(), path);
    assertThat("group metadata should be removed after merging", new File(gpLevelMetaFilePath).exists(), equalTo(false));
    stream = client.content().get(group, topGroup.getName(), path);
    assertThat(stream, notNullValue());
    metadata = IOUtils.toString(stream);
    stream.close();
    /* @formatter:off */
    final String updGroupContent = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<metadata>\n" + "  <groupId>org.foo</groupId>\n" + "  <artifactId>bar</artifactId>\n" + "  <versioning>\n" + "    <latest>1.2</latest>\n" + "    <release>1.2</release>\n" + "    <versions>\n" + "      <version>1.0</version>\n" + "      <version>1.1</version>\n" + "      <version>1.2</version>\n" + "    </versions>\n" + "    <lastUpdated>20150922164334</lastUpdated>\n" + "  </versioning>\n" + "</metadata>\n";
    /* @formatter:on */
    assertThat(metadata, equalTo(updGroupContent));
}
Also used : Group(org.commonjava.indy.model.core.Group) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) RemoteRepository(org.commonjava.indy.model.core.RemoteRepository) PathInfo(org.commonjava.indy.client.core.helper.PathInfo) File(java.io.File) HostedRepository(org.commonjava.indy.model.core.HostedRepository) Category(org.junit.experimental.categories.Category) Test(org.junit.Test) AbstractContentManagementTest(org.commonjava.indy.ftest.core.AbstractContentManagementTest)

Example 84 with Group

use of org.commonjava.indy.model.core.Group in project indy by Commonjava.

the class NPMReadonlyHostedDeleteMetadataTest method test.

@Test
public void test() throws Exception {
    // Prepare
    HostedRepository repo = new HostedRepository(NPM_PKG_KEY, repoName);
    repo = client.stores().create(repo, "adding npm hosted repo", HostedRepository.class);
    StoreKey storeKey = repo.getKey();
    client.content().store(storeKey, tarballPath, readTestResourceAsStream("helper-validator-identifier-7.10.4.tgz"));
    client.content().store(storeKey, packagePath, new ByteArrayInputStream(staleMeta.getBytes()));
    // Set readonly
    repo.setReadonly(true);
    client.stores().update(repo, "set read-only");
    // Create group G
    Group group = new Group(NPM_PKG_KEY, groupName, repo.getKey());
    client.stores().create(group, "adding npm group G", Group.class);
    // get from both A and G
    assertContent(repo, packagePath, staleMeta);
    assertContent(group, packagePath, staleMeta);
    // Extra case: delete non-metadata file is not allowed
    try {
        client.content().delete(storeKey, tarballPath);
    } catch (IndyClientException e) {
        assertThat(e.getStatusCode(), equalTo(ApplicationStatus.METHOD_NOT_ALLOWED.code()));
    }
    // Delete metadata from repo A
    IndyClientHttp http = client.module(IndyRawHttpModule.class).getHttp();
    http.deleteCache(client.content().contentPath(storeKey, packagePath) + "/package.json");
    assertExistence(repo, packagePath, false);
    assertExistence(group, packagePath, false);
    // Get metadata again
    try (InputStream is = client.content().get(storeKey, packagePath)) {
        // String actual = IOUtils.toString( is );
        // System.out.println( ">>>>\n" + actual );
        assertTarballUrl(is, "/api/content/npm/hosted/test/" + tarballPath);
    }
    try (InputStream is = client.content().get(group.getKey(), packagePath)) {
        // String actual = IOUtils.toString( is );
        // System.out.println( ">>>>\n" + actual );
        assertTarballUrl(is, "/api/content/npm/group/G/" + tarballPath);
    }
}
Also used : Group(org.commonjava.indy.model.core.Group) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IndyClientHttp(org.commonjava.indy.client.core.IndyClientHttp) IndyRawHttpModule(org.commonjava.indy.client.core.module.IndyRawHttpModule) IndyClientException(org.commonjava.indy.client.core.IndyClientException) StoreKey(org.commonjava.indy.model.core.StoreKey) HostedRepository(org.commonjava.indy.model.core.HostedRepository) Test(org.junit.Test) AbstractContentManagementTest(org.commonjava.indy.ftest.core.AbstractContentManagementTest)

Example 85 with Group

use of org.commonjava.indy.model.core.Group in project indy by Commonjava.

the class NPMRemoteMetadataContentDecorator_ScopedPathTest method test.

@Test
public void test() throws Exception {
    final String packagePath = "@types/jquery";
    final String tarball = "jquery-1.10.8-alpha.tgz";
    final String baseUrl = server.formatUrl(STORE, SUBPATH);
    final String tarballUrl = String.format("%s/%s/-/%s", baseUrl, packagePath, tarball);
    final String packageContent = IOUtils.toString(Thread.currentThread().getContextClassLoader().getResourceAsStream("subpath-scoped-package-1.10.8-alpha.json")).replace("@@REGISTRY@@", baseUrl);
    server.expect(server.formatUrl(STORE, SUBPATH, packagePath), 200, new ByteArrayInputStream(packageContent.getBytes()));
    final RemoteRepository remoteRepository = new RemoteRepository(NPM_PKG_KEY, STORE, baseUrl);
    client.stores().create(remoteRepository, "adding npm remote repo", RemoteRepository.class);
    final Group group = new Group(NPM_PKG_KEY, GROUP, remoteRepository.getKey());
    client.stores().create(group, "adding group", Group.class);
    // retrieve from remote repo
    StoreKey storeKey = remoteRepository.getKey();
    InputStream stream = client.content().get(storeKey, packagePath);
    assertThat(stream, notNullValue());
    String contextUrl = UrlUtils.buildUrl(fixture.getUrl(), "content", NPM_PKG_KEY, storeKey.getType().name(), STORE);
    String decoratedContent = packageContent.replaceAll(tarballUrl, String.format("%s/%s/-/%s", contextUrl, packagePath, tarball));
    assertThat(IOUtils.toString(stream), equalTo(decoratedContent));
    stream.close();
    // retrieve from group G
    StoreKey groupKey = group.getKey();
    stream = client.content().get(groupKey, packagePath);
    assertThat(stream, notNullValue());
    contextUrl = UrlUtils.buildUrl(fixture.getUrl(), "content", NPM_PKG_KEY, groupKey.getType().name(), GROUP);
    String maskedUrl = String.format("%s/%s/-/%s", contextUrl, packagePath, tarball);
    // group metadata is not a simple copy of the remote repo so we only check if the decorated tarball url exists
    assertThat(IOUtils.toString(stream), containsString(maskedUrl));
    stream.close();
}
Also used : Group(org.commonjava.indy.model.core.Group) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) RemoteRepository(org.commonjava.indy.model.core.RemoteRepository) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) StoreKey(org.commonjava.indy.model.core.StoreKey) Test(org.junit.Test) AbstractContentManagementTest(org.commonjava.indy.ftest.core.AbstractContentManagementTest)

Aggregations

Group (org.commonjava.indy.model.core.Group)211 Test (org.junit.Test)111 HostedRepository (org.commonjava.indy.model.core.HostedRepository)89 RemoteRepository (org.commonjava.indy.model.core.RemoteRepository)82 StoreKey (org.commonjava.indy.model.core.StoreKey)76 ByteArrayInputStream (java.io.ByteArrayInputStream)67 InputStream (java.io.InputStream)62 IndyDataException (org.commonjava.indy.data.IndyDataException)44 AbstractContentManagementTest (org.commonjava.indy.ftest.core.AbstractContentManagementTest)42 ArtifactStore (org.commonjava.indy.model.core.ArtifactStore)42 Transfer (org.commonjava.maven.galley.model.Transfer)31 EventMetadata (org.commonjava.maven.galley.event.EventMetadata)30 Logger (org.slf4j.Logger)30 Before (org.junit.Before)29 StoreDataManager (org.commonjava.indy.data.StoreDataManager)27 ConcreteResource (org.commonjava.maven.galley.model.ConcreteResource)27 HashSet (java.util.HashSet)26 ArrayList (java.util.ArrayList)25 List (java.util.List)22 IndyWorkflowException (org.commonjava.indy.IndyWorkflowException)22