Search in sources :

Example 31 with ValidationViolation

use of com.spotify.protoman.validation.ValidationViolation in project protoman by spotify.

the class ServiceNamingRuleTest method testAllowedName_new.

@Parameters(method = "allowedNames")
@Test
public void testAllowedName_new(final String name) throws Exception {
    final DescriptorSet current = DescriptorSet.empty();
    final DescriptorSet candidate = DescriptorSetUtils.buildDescriptorSet("a.proto", "syntax = 'proto3';\n" + String.format("service %s {\n", name) + "}");
    final ImmutableList<ValidationViolation> violations = schemaValidator.validate(current, candidate);
    assertThat(violations, is(empty()));
}
Also used : ValidationViolation(com.spotify.protoman.validation.ValidationViolation) DescriptorSet(com.spotify.protoman.descriptor.DescriptorSet) Parameters(junitparams.Parameters) Test(org.junit.Test)

Example 32 with ValidationViolation

use of com.spotify.protoman.validation.ValidationViolation in project protoman by spotify.

the class ServiceNamingRuleTest method testAllowedName_existing.

@Parameters(method = "allowedNames")
@Test
public void testAllowedName_existing(final String name) throws Exception {
    final DescriptorSet candidate = DescriptorSetUtils.buildDescriptorSet("a.proto", "syntax = 'proto3';\n" + String.format("service %s {\n", name) + "}");
    final ImmutableList<ValidationViolation> violations = schemaValidator.validate(candidate, candidate);
    assertThat(violations, is(empty()));
}
Also used : ValidationViolation(com.spotify.protoman.validation.ValidationViolation) DescriptorSet(com.spotify.protoman.descriptor.DescriptorSet) Parameters(junitparams.Parameters) Test(org.junit.Test)

Example 33 with ValidationViolation

use of com.spotify.protoman.validation.ValidationViolation in project protoman by spotify.

the class ServiceRemovalRuleTest method testMethodRemoved.

@Test
public void testMethodRemoved() throws Exception {
    final DescriptorSet current = DescriptorSetUtils.buildDescriptorSet("a.proto", "syntax = 'proto3';\n" + "service Derp {\n" + "}");
    final DescriptorSet candidate = DescriptorSetUtils.buildDescriptorSet("a.proto", "syntax = 'proto3';\n");
    final ImmutableList<ValidationViolation> violations = schemaValidator.validate(current, candidate);
    assertThat(violations, contains(validationViolation().type(equalTo(ViolationType.WIRE_INCOMPATIBILITY_VIOLATION)).description(equalTo("service removed"))));
}
Also used : ValidationViolation(com.spotify.protoman.validation.ValidationViolation) DescriptorSet(com.spotify.protoman.descriptor.DescriptorSet) Test(org.junit.Test)

Example 34 with ValidationViolation

use of com.spotify.protoman.validation.ValidationViolation in project protoman by spotify.

the class SchemaRegistry method publishSchemata.

@Override
public PublishResult publishSchemata(final ImmutableList<SchemaFile> schemaFiles) {
    try (final SchemaStorage.Transaction tx = schemaStorage.open()) {
        final BuildDescriptorsResult buildDescriptorsResult = buildDescriptorSets(tx, schemaFiles);
        if (buildDescriptorsResult.currentCompilationError() != null) {
            // Compilation of what's currently in the registry failed. This should not happen!
            throw new RuntimeException("Failed to build descriptor for current schemata");
        }
        if (buildDescriptorsResult.candidateCompilationError() != null) {
            return PublishResult.error(buildDescriptorsResult.candidateCompilationError());
        }
        final DescriptorSet currentDs = buildDescriptorsResult.current();
        final DescriptorSet candidateDs = buildDescriptorsResult.candidate();
        // Validate changes
        final ImmutableList<ValidationViolation> violations = schemaValidator.validate(currentDs, candidateDs);
        // TODO(staffan): Don't treat all violations as fatal
        if (!violations.isEmpty()) {
            return PublishResult.error("Validation failed", violations);
        }
        // Store files, but only for protos that changed
        updatedFiles(schemaFiles.stream(), currentDs, candidateDs).forEach(file -> {
            final ImmutableSet<Path> dependencies = candidateDs.findFileByPath(file.path()).get().dependencies().stream().map(FileDescriptor::filePath).collect(toImmutableSet());
            logger.debug("proto: {}, deps: {}", file.path(), dependencies);
            tx.storeFile(file);
            tx.storeProtoDependencies(file.path(), dependencies);
        });
        // Update package versions (only for packages that have changed)
        final ImmutableMap<String, SchemaVersionPair> publishedPackages = updatePackageVersions(tx, currentDs, candidateDs);
        tx.commit();
        return PublishResult.create(violations, publishedPackages);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : Path(java.nio.file.Path) DescriptorSet(com.spotify.protoman.descriptor.DescriptorSet) DescriptorBuilderException(com.spotify.protoman.descriptor.DescriptorBuilderException) SchemaStorage(com.spotify.protoman.registry.storage.SchemaStorage) ValidationViolation(com.spotify.protoman.validation.ValidationViolation)

Example 35 with ValidationViolation

use of com.spotify.protoman.validation.ValidationViolation in project protoman by spotify.

the class EnumDefaultValueTest method testDisallowedName.

@Parameters(method = "disallowedNames")
@Test
public void testDisallowedName(final String name) throws Exception {
    final DescriptorSet candidate = DescriptorSetUtils.buildDescriptorSet("a.proto", String.format(TEMPLATE, name));
    final ImmutableList<ValidationViolation> violations = schemaValidator.validate(DescriptorSet.empty(), candidate);
    assertThat(violations, contains(validationViolation().description(equalTo("enum value 0 should be used for unknown value, e.g. AN_ENUM_UNSPECIFIED")).type(equalTo(ViolationType.BEST_PRACTICE_VIOLATION)).current(nullValue(GenericDescriptor.class)).candidate(genericDescriptor().sourceCodeInfo(optionalWithValue(sourceCodeInfo().start(filePosition().line(2).column(1)))))));
}
Also used : ValidationViolation(com.spotify.protoman.validation.ValidationViolation) DescriptorSet(com.spotify.protoman.descriptor.DescriptorSet) Parameters(junitparams.Parameters) Test(org.junit.Test)

Aggregations

ValidationViolation (com.spotify.protoman.validation.ValidationViolation)76 DescriptorSet (com.spotify.protoman.descriptor.DescriptorSet)75 Test (org.junit.Test)74 Parameters (junitparams.Parameters)43 DescriptorBuilderException (com.spotify.protoman.descriptor.DescriptorBuilderException)1 GenericDescriptor (com.spotify.protoman.descriptor.GenericDescriptor)1 SchemaStorage (com.spotify.protoman.registry.storage.SchemaStorage)1 Path (java.nio.file.Path)1 Nullable (javax.annotation.Nullable)1