/** * Returns a new {@link MavenId} whose values are based on the given {@link * com.google.cloud.tools.libraries.json.CloudLibraryClientMavenCoordinates}. * * @param mavenCoordinates the {@link CloudLibraryClientMavenCoordinates} to convert to a {@link * MavenId} */ static MavenId toMavenId(CloudLibraryClientMavenCoordinates mavenCoordinates) { return new MavenId( mavenCoordinates.getGroupId(), mavenCoordinates.getArtifactId(), mavenCoordinates.getVersion()); } }
/** * Joins the given list of {@link MavenId MavenIds} into a human-readable string. * * <p>Does not include the version as the version of the dependency may be managed by a BOM. * * @param mavenIds the list of {@link MavenId MavenIds} to join */ private static String joinMavenIds(List<MavenId> mavenIds) { return mavenIds .stream() .map( mavenId -> { StringBuilder builder = new StringBuilder(); MavenId.append(builder, mavenId.getGroupId()); MavenId.append(builder, mavenId.getArtifactId()); return builder.toString(); }) .map(string -> "- " + string) .collect(Collectors.joining("<br>")); } }
/** * Returns {@code true} if the given {@link MavenId} is in the given list of {@link * MavenDomDependency dependencies}. * * <p>Note that equality is tested via matching group IDs and artifact IDs. Equality of versions * is not required. This prevents adding duplicate dependencies for the same artifact, but with a * different version. * * @param mavenId the {@link MavenId} to check for existence in the given list of dependencies * @param dependencies the list of {@link MavenDomDependency} objects that currently exist in the * DOM model */ static boolean isMavenIdInDependencyList(MavenId mavenId, List<MavenDomDependency> dependencies) { return dependencies .stream() .anyMatch( dependency -> mavenId.equals( dependency.getGroupId().getStringValue(), dependency.getArtifactId().getStringValue())); }
/** * Writes the google-cloud-java dependency to the users pom.xml. * * <p>If the dependency uses a BOM, the dependency will not be written with a version so that the * BOM can manage the version. */ private static void writeNewMavenDependency( MavenDomProjectModel model, MavenId mavenId, boolean hasBom) { MavenDomDependency dependency = MavenDomUtil.createDomDependency(model, /* editor= */ null); dependency.getGroupId().setStringValue(mavenId.getGroupId()); dependency.getArtifactId().setStringValue(mavenId.getArtifactId()); // Only write the version to the dependency if there is no BOM if (!hasBom) { dependency.getVersion().setStringValue(mavenId.getVersion()); } UsageTrackerService.getInstance() .trackEvent(GctTracking.CLIENT_LIBRARY_ADD_LIBRARY_MAVEN) .addMetadata(GctTracking.METADATA_BUILD_SYSTEM_KEY, GctTracking.METADATA_BUILD_SYSTEM_MAVEN) .addMetadata(GctTracking.METADATA_LABEL_KEY, mavenId.getDisplayString()) .ping(); }
/** Creates a new {@link MavenModuleBuilder} that can be used to create Maven modules in tests. */ private MavenModuleBuilder initMavenModuleBuilder(Project project) { MavenModuleBuilder moduleBuilder = new MavenModuleBuilder(); MavenId mavenId = new MavenId("org.foo", "module", "1.0"); String root = project.getBasePath(); moduleBuilder.setName("module"); moduleBuilder.setModuleFilePath(root + "/module.iml"); moduleBuilder.setContentEntryPath(root); moduleBuilder.setProjectId(mavenId); return moduleBuilder; }
@Test public void getCloudLibraries_withOnlyNonCloudDependencies_isEmpty() { when(librariesService.getCloudLibraries()) .thenReturn(ImmutableList.of(LIBRARY.toCloudLibrary())); MavenTestUtils.getInstance() .runWithMavenModule( testFixture.getProject(), module -> { MavenId nonCloudDependency = new MavenId("my-group", "my-artifact", "1.0"); writeDependenciesToPom(module, ImmutableList.of(nonCloudDependency)); assertThat(state.getCloudLibraries(module)).isEmpty(); }); }
@Test public void getCloudLibraries_withCloudAndNonCloudDependencies_returnsValidCloudLibrary() { when(librariesService.getCloudLibraries()) .thenReturn(ImmutableList.of(LIBRARY.toCloudLibrary())); MavenTestUtils.getInstance() .runWithMavenModule( testFixture.getProject(), module -> { TestCloudLibraryClientMavenCoordinates mavenCoordinates = LIBRARY.clients().get(0).mavenCoordinates(); String groupId = mavenCoordinates.groupId(); String artifactId = mavenCoordinates.artifactId(); MavenId cloudDependency = new MavenId(groupId, artifactId, "1.0"); MavenId nonCloudDependency = new MavenId("my-group", "my-artifact", "1.0"); writeDependenciesToPom(module, ImmutableList.of(cloudDependency, nonCloudDependency)); state.syncManagedProjectLibraries(); Set<CloudLibrary> libraries = state.getCloudLibraries(module); assertThat_cloudLibrariesContainsExactlyOne(libraries); }); }
@Test public void getCloudLibraries_withOnlyCloudDependencies_returnsValidCloudLibrary() { when(librariesService.getCloudLibraries()) .thenReturn(ImmutableList.of(LIBRARY.toCloudLibrary())); MavenTestUtils.getInstance() .runWithMavenModule( testFixture.getProject(), module -> { TestCloudLibraryClientMavenCoordinates mavenCoordinates = LIBRARY.clients().get(0).mavenCoordinates(); String groupId = mavenCoordinates.groupId(); String artifactId = mavenCoordinates.artifactId(); MavenId managedDependency = new MavenId(groupId, artifactId, "1.0"); writeDependenciesToPom(module, ImmutableList.of(managedDependency)); state.syncManagedProjectLibraries(); Set<CloudLibrary> libraries = state.getCloudLibraries(module); assertThat_cloudLibrariesContainsExactlyOne(libraries); }); }