### Question:
LoadManifestValidationService { public void validateManifest(LoadManifest loadManifest) { try { log.debug("Start validating load manifest: {}", loadManifest); final JsonNode manifestDefaultSchema = getDefaultManifestSchema(); JsonNode manifest = objectMapper.valueToTree(loadManifest); Set<ValidationMessage> errors = jsonValidationService .validate(manifestDefaultSchema, manifest); log.debug("Validation result: {}", errors); if (!errors.isEmpty()) { throw new IngestException(format( "Failed to validate json from manifest %s, validation result is %s", loadManifest, errors)); } } catch (IOException e) { throw new IngestException(format("Fail parse load manifest - %s", loadManifest)); } }  void validateManifest(LoadManifest loadManifest);  }### Answer:
@Test void shouldSuccessfullyValidateLoadManifest() { service.validateManifest(getLoadManifest()); InOrder inOrder = Mockito.inOrder(jsonValidationService, schemeFetcher); inOrder.verify(jsonValidationService).validate(any(JsonNode.class), any(JsonNode.class)); inOrder.verifyNoMoreInteractions(); }
@Test void shouldThrowIngestExceptionWhenValidationHasTheErrors() { Throwable thrown = catchThrowable(() -> service.validateManifest(new LoadManifest())); then(thrown) .isInstanceOf(IngestException.class) .hasMessageStartingWith("Failed to validate json from manifest") .hasMessageContaining("validation result is"); InOrder inOrder = Mockito.inOrder(jsonValidationService, schemeFetcher); inOrder.verify(jsonValidationService).validate(any(JsonNode.class), any(JsonNode.class)); inOrder.verifyNoMoreInteractions(); }
@Test void shouldThrowServerErrorExceptionWhen() { willReturn(null).given(manifestSchemaReceiver).getLoadManifestSchema(); Throwable thrown = catchThrowable(() -> service.validateManifest(getLoadManifest())); then(thrown) .isInstanceOf(OsduServerErrorException.class) .hasMessage("Can not find resource for load manifest schema"); InOrder inOrder = Mockito.inOrder(jsonValidationService, schemeFetcher); inOrder.verifyNoMoreInteractions(); }
@Test void shouldThrowIngestExceptionWhenFailToParseManifestSchema() throws IOException { willThrow(IOException.class).given(objectMapper).readTree(any(InputStream.class)); Throwable thrown = catchThrowable(() -> service.validateManifest(getLoadManifest())); then(thrown) .isInstanceOf(IngestException.class) .hasMessageStartingWith("Fail parse load manifest"); InOrder inOrder = Mockito.inOrder(jsonValidationService, schemeFetcher); inOrder.verifyNoMoreInteractions(); }
@Test void shouldThrowIllegalArgumentExceptionWhenUnableToReadLoadManifestAsTree() { willThrow(IllegalArgumentException.class).given(objectMapper) .valueToTree(any(LoadManifest.class)); Throwable thrown = catchThrowable(() -> service.validateManifest(getLoadManifest())); then(thrown).isInstanceOf(IllegalArgumentException.class); InOrder inOrder = Mockito.inOrder(jsonValidationService, schemeFetcher); inOrder.verifyNoMoreInteractions(); }
@Test void shouldThrowIngestExceptionWhenFailToCreateJsonSchema() throws IOException { JsonNode uriNode = Mockito.mock(JsonNode.class); JsonNode schemaNode = Mockito.mock(JsonNode.class); willReturn(schemaNode).given(objectMapper).readTree(any(InputStream.class)); given(schemaNode.get("$schema")).willReturn(uriNode); Throwable thrown = catchThrowable(() -> service.validateManifest(getLoadManifest())); then(thrown) .isInstanceOf(IngestException.class) .hasMessageStartingWith("Error creating json validation schema from json object:"); InOrder inOrder = Mockito.inOrder(jsonValidationService, schemeFetcher); inOrder.verify(jsonValidationService).validate(any(JsonNode.class), any(JsonNode.class)); inOrder.verifyNoMoreInteractions(); }