MavenMetadataContentValidator extends ComponentSupport { public void validate(final String path, final InputStream mavenMetadata) { try { Metadata metadata = MavenModels.readMetadata(mavenMetadata); if (metadata == null) { throw new InvalidContentException("Metadata at path " + path + " is not a valid maven-metadata.xml"); } if (isNotEmpty(metadata.getGroupId())) { validatePath(path, metadata); } else { log.debug("No groupId found in maven-metadata.xml therefore skipping validation"); } } catch (IOException e) { log.warn("Unable to read maven-metadata.xml at path {}", path, e ); throw new InvalidContentException("Unable to read maven-metadata.xml reason: " + e.getMessage()); } }  void validate(final String path, final InputStream mavenMetadata);  }
@Test(expected = InvalidContentException.class) public void throwInvalidContentWhenMetadataEmpty() { InputStream mavenMetadata = toInputStream(""); underTest.validate(VALID_PATH, mavenMetadata); }
@Test(expected = InvalidContentException.class) public void throwInvalidContentWhenMetadataNotMetadata() { InputStream mavenMetadata = toInputStream("This is not metadata"); underTest.validate(VALID_PATH, mavenMetadata); }
@Test(expected = InvalidContentException.class) public void throwInvalidContentWhenMetadataDoesNotMatchPath() { InputStream mavenMetadata = toInputStream("<metadata>\n" + " <groupId>group</groupId>\n" + " <artifactId>artifact</artifactId>\n" + "</metadata>\n"); underTest.validate(INVALID_PATH, mavenMetadata); }
@Test public void doNotValidateWhenGroupNotFound() { InputStream mavenMetadata = toInputStream("<metadata>\n" + " <artifactId>artifact</artifactId>\n" + "</metadata>\n"); underTest.validate(INVALID_PATH, mavenMetadata); }
@Test public void doNotValidateWhenGroupEmpty() { InputStream mavenMetadata = toInputStream("<metadata>\n" + " <groupId></groupId>\n" + " <artifactId>artifact</artifactId>\n" + "</metadata>\n"); underTest.validate(INVALID_PATH, mavenMetadata); }
@Test(expected = InvalidContentException.class) public void throwInvalidContentWhenArtifactNotFound() { InputStream mavenMetadata = toInputStream("<metadata>\n" + " <groupId>group</groupId>\n" + "</metadata>\n"); underTest.validate(VALID_PATH, mavenMetadata); }
@Test(expected = InvalidContentException.class) public void throwInvalidContentWhenArtifactEmpty() { InputStream mavenMetadata = toInputStream("<metadata>\n" + " <groupId>group</groupId>\n" + " <artifactId></artifactId>\n" + "</metadata>\n"); underTest.validate(VALID_PATH, mavenMetadata); }
@Test public void noExceptionWhenValidContentAndMatchesPath() { InputStream mavenMetadata = toInputStream("<metadata>\n" + " <groupId>group</groupId>\n" + " <artifactId>artifact</artifactId>\n" + "</metadata>\n"); underTest.validate(VALID_PATH, mavenMetadata); }
@Test public void noExceptionWhenValidContentAndMatchesPathForSnapshotMetadata() { InputStream mavenMetadata = toInputStream("<metadata>\n" + " <groupId>group</groupId>\n" + " <artifactId>artifact</artifactId>\n" + " <version>1.0-SNAPSHOT</version>\n" + "</metadata>\n"); underTest.validate(VALID_SNAPSHOT_PATH, mavenMetadata); }
@Test public void noExceptionWhenValidContentAndMatchesPathWithReleasedVersion() { InputStream mavenMetadata = toInputStream("<metadata>\n" + " <groupId>group</groupId>\n" + " <artifactId>artifact</artifactId>\n" + " <version>1.0</version>\n" + "</metadata>\n"); underTest.validate(VALID_PATH, mavenMetadata); }
@Test public void noExceptionWhenGroupOnlyWithCorrectPath() { InputStream mavenMetadata = toInputStream("<metadata>\n" + " <groupId>group</groupId>\n" + "</metadata>\n"); underTest.validate(VALID_PATH_GROUP_ONLY, mavenMetadata); }