Search in sources :

Example 1 with TypeSafeDiagnosingMatcher

use of org.hamcrest.TypeSafeDiagnosingMatcher in project hippo by NHS-digital-website.

the class ImagePairSection method getMatcher.

public Matcher<? super SectionWidget> getMatcher() {
    return new TypeSafeDiagnosingMatcher<SectionWidget>(ImagePairSectionWidget.class) {

        @Override
        protected boolean matchesSafely(SectionWidget item, Description desc) {
            ImagePairSectionWidget widget = (ImagePairSectionWidget) item;
            List<ImageSectionWidget> imageSectionWidgets = widget.getImageSectionWidgets();
            // pad with nulls for the 2 slots we could have had images in
            while (imageSectionWidgets.size() < 2) {
                imageSectionWidgets.add(null);
            }
            return compare(first.getMatcher(), imageSectionWidgets.get(0), desc) && compare(second == null ? nullValue() : second.getMatcher(), imageSectionWidgets.get(1), desc);
        }

        @Override
        public void describeTo(Description description) {
            description.appendValue(ImagePairSection.this);
        }
    };
}
Also used : ImagePairSectionWidget(uk.nhs.digital.ps.test.acceptance.pages.widgets.ImagePairSectionWidget) Description(org.hamcrest.Description) ImageSectionWidget(uk.nhs.digital.ps.test.acceptance.pages.widgets.ImageSectionWidget) ImagePairSectionWidget(uk.nhs.digital.ps.test.acceptance.pages.widgets.ImagePairSectionWidget) SectionWidget(uk.nhs.digital.ps.test.acceptance.pages.widgets.SectionWidget) TypeSafeDiagnosingMatcher(org.hamcrest.TypeSafeDiagnosingMatcher) ImageSectionWidget(uk.nhs.digital.ps.test.acceptance.pages.widgets.ImageSectionWidget)

Example 2 with TypeSafeDiagnosingMatcher

use of org.hamcrest.TypeSafeDiagnosingMatcher in project microservice_framework by CJSCommonPlatform.

the class JsonSchemaValidationMatcher method isValidJsonEnvelopeForSchema.

/**
 * Validates a JsonEnvelope against the correct schema for the action name provided in the
 * metadata. Expects to find the schema on the class path in package
 * 'json/schema/{action.name}.json' or 'raml/json/schema/{action.name}.json'.
 *
 * @return matcher
 */
public static Matcher<JsonEnvelope> isValidJsonEnvelopeForSchema() {
    return new TypeSafeDiagnosingMatcher<JsonEnvelope>() {

        private ValidationException validationException = null;

        @Override
        protected boolean matchesSafely(final JsonEnvelope jsonEnvelope, final Description description) {
            if (null == validationException) {
                try {
                    final String pathToJsonSchema = format(JSON_SCHEMA_TEMPLATE, jsonEnvelope.metadata().name());
                    getJsonSchemaFor(pathToJsonSchema).validate(new JSONObject(jsonEnvelope.payloadAsJsonObject().toString()));
                } catch (final IllegalArgumentException | IOException e) {
                    try {
                        final String pathToJsonSchema = format(RAML_JSON_SCHEMA_TEMPLATE, jsonEnvelope.metadata().name());
                        getJsonSchemaFor(pathToJsonSchema).validate(new JSONObject(jsonEnvelope.payloadAsJsonObject().toString()));
                    } catch (final IOException ioe) {
                        throw new IllegalArgumentException(ioe);
                    }
                } catch (final ValidationException e) {
                    validationException = e;
                    return false;
                }
                return true;
            } else {
                description.appendText("Schema validation failed with message: ").appendValue(validationException.getMessage());
                return false;
            }
        }

        @Override
        public void describeTo(final Description description) {
            description.appendText("JsonEnvelope validated against schema found on classpath at 'raml/json/schema/' ");
        }
    };
}
Also used : ValidationException(org.everit.json.schema.ValidationException) Description(org.hamcrest.Description) JSONObject(org.json.JSONObject) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) IOException(java.io.IOException) TypeSafeDiagnosingMatcher(org.hamcrest.TypeSafeDiagnosingMatcher)

Example 3 with TypeSafeDiagnosingMatcher

use of org.hamcrest.TypeSafeDiagnosingMatcher in project atlasdb by palantir.

the class CassandraSchemaLockTest method doesNotContainTheColumnFamilyIdMismatchError.

private static Matcher<File> doesNotContainTheColumnFamilyIdMismatchError() {
    return new TypeSafeDiagnosingMatcher<File>() {

        @Override
        protected boolean matchesSafely(File file, Description mismatchDescription) {
            Path path = Paths.get(file.getAbsolutePath());
            try (Stream<String> lines = Files.lines(path, StandardCharsets.ISO_8859_1)) {
                List<String> badLines = lines.filter(line -> line.contains("Column family ID mismatch")).collect(Collectors.toList());
                mismatchDescription.appendText("file called " + file.getAbsolutePath() + " which contains lines").appendValueList("\n", "\n", "", badLines);
                return badLines.isEmpty();
            } catch (IOException e) {
                throw Throwables.propagate(e);
            }
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("a file with no column family ID mismatch errors");
        }
    };
}
Also used : Path(java.nio.file.Path) Containers(com.palantir.atlasdb.containers.Containers) CassandraKeyValueServiceConfig(com.palantir.atlasdb.cassandra.CassandraKeyValueServiceConfig) Callable(java.util.concurrent.Callable) ThreeNodeCassandraCluster(com.palantir.atlasdb.containers.ThreeNodeCassandraCluster) Assert.assertThat(org.junit.Assert.assertThat) TypeSafeDiagnosingMatcher(org.hamcrest.TypeSafeDiagnosingMatcher) Matchers.everyItem(org.hamcrest.Matchers.everyItem) ImmutableList(com.google.common.collect.ImmutableList) CassandraKeyValueServiceImpl(com.palantir.atlasdb.keyvalue.cassandra.CassandraKeyValueServiceImpl) TableReference(com.palantir.atlasdb.keyvalue.api.TableReference) CassandraKeyValueService(com.palantir.atlasdb.keyvalue.cassandra.CassandraKeyValueService) ClassRule(org.junit.ClassRule) Path(java.nio.file.Path) ExecutorService(java.util.concurrent.ExecutorService) AtlasDbConstants(com.palantir.atlasdb.AtlasDbConstants) Description(org.hamcrest.Description) CyclicBarrier(java.util.concurrent.CyclicBarrier) Files(java.nio.file.Files) Assert.assertTrue(org.junit.Assert.assertTrue) Throwables(com.google.common.base.Throwables) IOException(java.io.IOException) Test(org.junit.Test) Collectors(java.util.stream.Collectors) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) TimeUnit(java.util.concurrent.TimeUnit) FeatureMatcher(org.hamcrest.FeatureMatcher) List(java.util.List) Matchers.hasItem(org.hamcrest.Matchers.hasItem) Stream(java.util.stream.Stream) Paths(java.nio.file.Paths) Matcher(org.hamcrest.Matcher) Optional(java.util.Optional) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) Description(org.hamcrest.Description) IOException(java.io.IOException) File(java.io.File) TypeSafeDiagnosingMatcher(org.hamcrest.TypeSafeDiagnosingMatcher)

Example 4 with TypeSafeDiagnosingMatcher

use of org.hamcrest.TypeSafeDiagnosingMatcher in project flink by apache.

the class SavepointITCase method hasEntropyInFileStateHandlePaths.

private static Matcher<File> hasEntropyInFileStateHandlePaths() {
    return new TypeSafeDiagnosingMatcher<File>() {

        @Override
        protected boolean matchesSafely(final File savepointDir, final Description mismatchDescription) {
            if (savepointDir == null) {
                mismatchDescription.appendText("savepoint dir must not be null");
                return false;
            }
            final List<Path> filesWithoutEntropy = listRecursively(savepointDir.toPath().resolve(EntropyInjectingTestFileSystem.ENTROPY_INJECTION_KEY));
            final Path savepointDirWithEntropy = savepointDir.toPath().resolve(EntropyInjectingTestFileSystem.ENTROPY);
            final List<Path> filesWithEntropy = listRecursively(savepointDirWithEntropy);
            if (!filesWithoutEntropy.isEmpty()) {
                mismatchDescription.appendText("there are savepoint files with unresolved entropy placeholders");
                return false;
            }
            if (!Files.exists(savepointDirWithEntropy) || filesWithEntropy.isEmpty()) {
                mismatchDescription.appendText("there are no savepoint files with added entropy");
                return false;
            }
            return true;
        }

        @Override
        public void describeTo(final Description description) {
            description.appendText("all savepoint files should have added entropy");
        }
    };
}
Also used : Path(java.nio.file.Path) Description(org.hamcrest.Description) File(java.io.File) TypeSafeDiagnosingMatcher(org.hamcrest.TypeSafeDiagnosingMatcher)

Aggregations

Description (org.hamcrest.Description)4 TypeSafeDiagnosingMatcher (org.hamcrest.TypeSafeDiagnosingMatcher)4 File (java.io.File)2 IOException (java.io.IOException)2 Path (java.nio.file.Path)2 Throwables (com.google.common.base.Throwables)1 ImmutableList (com.google.common.collect.ImmutableList)1 AtlasDbConstants (com.palantir.atlasdb.AtlasDbConstants)1 CassandraKeyValueServiceConfig (com.palantir.atlasdb.cassandra.CassandraKeyValueServiceConfig)1 Containers (com.palantir.atlasdb.containers.Containers)1 ThreeNodeCassandraCluster (com.palantir.atlasdb.containers.ThreeNodeCassandraCluster)1 TableReference (com.palantir.atlasdb.keyvalue.api.TableReference)1 CassandraKeyValueService (com.palantir.atlasdb.keyvalue.cassandra.CassandraKeyValueService)1 CassandraKeyValueServiceImpl (com.palantir.atlasdb.keyvalue.cassandra.CassandraKeyValueServiceImpl)1 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Files (java.nio.file.Files)1 Paths (java.nio.file.Paths)1 List (java.util.List)1 Optional (java.util.Optional)1