Search in sources :

Example 1 with ObjectMapper

use of org.springframework.cloud.cloudfoundry.com.fasterxml.jackson.databind.ObjectMapper in project tomee-buildpack-resource-configuration by cloudfoundry-community.

the class GenericServicePropertiesProviderTest method testCorrectPropertiesFound.

@Test
public void testCorrectPropertiesFound() throws Exception {
    GenericServicePropertiesProvider propertiesProvider = new GenericServicePropertiesProvider(new TestEnvironmentAccessor(), new ObjectMapper());
    configureDelegatingPropertiesProvider(propertiesProvider, "someId", new Properties());
    Properties properties = propertiesProvider.provides();
    Assert.assertEquals(properties.get("name1"), "val1");
    Assert.assertEquals(properties.get("name2"), "val2");
}
Also used : Properties(java.util.Properties) ObjectMapper(org.springframework.cloud.cloudfoundry.com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 2 with ObjectMapper

use of org.springframework.cloud.cloudfoundry.com.fasterxml.jackson.databind.ObjectMapper in project ArTEMiS by ls1intum.

the class TutorParticipationService method validateTutorialExampleSubmission.

/**
 * Validates the tutor example submission. If invalid, throw bad request exception with information which feedback are incorrect.
 */
private void validateTutorialExampleSubmission(ExampleSubmission tutorExampleSubmission) {
    var latestResult = tutorExampleSubmission.getSubmission().getLatestResult();
    if (latestResult == null) {
        throw new BadRequestAlertException("The training does not contain an assessment", ENTITY_NAME, "invalid_assessment");
    }
    var tutorFeedback = latestResult.getFeedbacks();
    var instructorFeedback = exampleSubmissionRepository.getFeedbackForExampleSubmission(tutorExampleSubmission.getId());
    boolean equalFeedbackCount = instructorFeedback.size() == tutorFeedback.size();
    var unreferencedInstructorFeedbackCount = instructorFeedback.stream().filter(feedback -> feedback.getType() == MANUAL_UNREFERENCED).toList().size();
    var unreferencedTutorFeedback = tutorFeedback.stream().filter(feedback -> feedback.getType() == MANUAL_UNREFERENCED).toList();
    // If invalid, get all incorrect feedback and send an array of the corresponding `FeedbackCorrectionError`s to the client.
    var wrongFeedback = tutorFeedback.stream().flatMap(feedback -> {
        // If current tutor feedback is unreferenced and there are already more than enough unreferenced feedback provided, mark this feedback as unnecessary.
        var unreferencedTutorFeedbackCount = unreferencedTutorFeedback.indexOf(feedback) + 1;
        var validationError = unreferencedTutorFeedbackCount > unreferencedInstructorFeedbackCount ? Optional.of(UNNECESSARY_FEEDBACK) : checkTutorFeedbackForErrors(feedback, instructorFeedback);
        if (validationError.isEmpty()) {
            return Stream.empty();
        }
        var objectWriter = new ObjectMapper().writer().withDefaultPrettyPrinter();
        try {
            // Build JSON string for the corresponding `FeedbackCorrectionError` object.
            // TODO: I think we should let Spring automatically convert it to Json
            var feedbackCorrectionErrorJSON = objectWriter.writeValueAsString(new FeedbackCorrectionError(feedback.getReference(), validationError.get()));
            return Stream.of(feedbackCorrectionErrorJSON);
        } catch (JsonProcessingException e) {
            log.warn("JsonProcessingException in validateTutorialExampleSubmission: {}", e.getMessage());
            return Stream.empty();
        }
    }).collect(Collectors.joining(","));
    if (wrongFeedback.isBlank() && equalFeedbackCount) {
        return;
    }
    // Pack this information into bad request exception.
    throw new BadRequestAlertException("{\"errors\": [" + wrongFeedback + "]}", ENTITY_NAME, "invalid_assessment", true);
}
Also used : BadRequestAlertException(de.tum.in.www1.artemis.web.rest.errors.BadRequestAlertException) java.util(java.util) JsonProcessingException(org.springframework.cloud.cloudfoundry.com.fasterxml.jackson.core.JsonProcessingException) Logger(org.slf4j.Logger) BadRequestAlertException(de.tum.in.www1.artemis.web.rest.errors.BadRequestAlertException) FeedbackCorrectionErrorType(de.tum.in.www1.artemis.service.TutorParticipationService.FeedbackCorrectionErrorType) ObjectMapper(org.springframework.cloud.cloudfoundry.com.fasterxml.jackson.databind.ObjectMapper) LoggerFactory(org.slf4j.LoggerFactory) Collectors(java.util.stream.Collectors) EntityNotFoundException(de.tum.in.www1.artemis.web.rest.errors.EntityNotFoundException) Stream(java.util.stream.Stream) de.tum.in.www1.artemis.domain(de.tum.in.www1.artemis.domain) Service(org.springframework.stereotype.Service) TutorParticipationStatus(de.tum.in.www1.artemis.domain.enumeration.TutorParticipationStatus) ExampleSubmissionRepository(de.tum.in.www1.artemis.repository.ExampleSubmissionRepository) TutorParticipation(de.tum.in.www1.artemis.domain.participation.TutorParticipation) TutorParticipationRepository(de.tum.in.www1.artemis.repository.TutorParticipationRepository) FeedbackType(de.tum.in.www1.artemis.domain.enumeration.FeedbackType) JsonProcessingException(org.springframework.cloud.cloudfoundry.com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(org.springframework.cloud.cloudfoundry.com.fasterxml.jackson.databind.ObjectMapper)

Example 3 with ObjectMapper

use of org.springframework.cloud.cloudfoundry.com.fasterxml.jackson.databind.ObjectMapper in project Artemis by ls1intum.

the class TutorParticipationService method validateTutorialExampleSubmission.

/**
 * Validates the tutor example submission. If invalid, throw bad request exception with information which feedback are incorrect.
 */
private void validateTutorialExampleSubmission(ExampleSubmission tutorExampleSubmission) {
    var latestResult = tutorExampleSubmission.getSubmission().getLatestResult();
    if (latestResult == null) {
        throw new BadRequestAlertException("The training does not contain an assessment", ENTITY_NAME, "invalid_assessment");
    }
    var tutorFeedback = latestResult.getFeedbacks();
    var instructorFeedback = exampleSubmissionRepository.getFeedbackForExampleSubmission(tutorExampleSubmission.getId());
    boolean equalFeedbackCount = instructorFeedback.size() == tutorFeedback.size();
    var unreferencedInstructorFeedbackCount = instructorFeedback.stream().filter(feedback -> feedback.getType() == MANUAL_UNREFERENCED).toList().size();
    var unreferencedTutorFeedback = tutorFeedback.stream().filter(feedback -> feedback.getType() == MANUAL_UNREFERENCED).toList();
    // If invalid, get all incorrect feedback and send an array of the corresponding `FeedbackCorrectionError`s to the client.
    var wrongFeedback = tutorFeedback.stream().flatMap(feedback -> {
        // If current tutor feedback is unreferenced and there are already more than enough unreferenced feedback provided, mark this feedback as unnecessary.
        var unreferencedTutorFeedbackCount = unreferencedTutorFeedback.indexOf(feedback) + 1;
        var validationError = unreferencedTutorFeedbackCount > unreferencedInstructorFeedbackCount ? Optional.of(UNNECESSARY_FEEDBACK) : checkTutorFeedbackForErrors(feedback, instructorFeedback);
        if (validationError.isEmpty()) {
            return Stream.empty();
        }
        var objectWriter = new ObjectMapper().writer().withDefaultPrettyPrinter();
        try {
            // Build JSON string for the corresponding `FeedbackCorrectionError` object.
            // TODO: I think we should let Spring automatically convert it to Json
            var feedbackCorrectionErrorJSON = objectWriter.writeValueAsString(new FeedbackCorrectionError(feedback.getReference(), validationError.get()));
            return Stream.of(feedbackCorrectionErrorJSON);
        } catch (JsonProcessingException e) {
            log.warn("JsonProcessingException in validateTutorialExampleSubmission: {}", e.getMessage());
            return Stream.empty();
        }
    }).collect(Collectors.joining(","));
    if (wrongFeedback.isBlank() && equalFeedbackCount) {
        return;
    }
    // Pack this information into bad request exception.
    throw new BadRequestAlertException("{\"errors\": [" + wrongFeedback + "]}", ENTITY_NAME, "invalid_assessment", true);
}
Also used : BadRequestAlertException(de.tum.in.www1.artemis.web.rest.errors.BadRequestAlertException) java.util(java.util) JsonProcessingException(org.springframework.cloud.cloudfoundry.com.fasterxml.jackson.core.JsonProcessingException) Logger(org.slf4j.Logger) BadRequestAlertException(de.tum.in.www1.artemis.web.rest.errors.BadRequestAlertException) FeedbackCorrectionErrorType(de.tum.in.www1.artemis.service.TutorParticipationService.FeedbackCorrectionErrorType) ObjectMapper(org.springframework.cloud.cloudfoundry.com.fasterxml.jackson.databind.ObjectMapper) LoggerFactory(org.slf4j.LoggerFactory) Collectors(java.util.stream.Collectors) EntityNotFoundException(de.tum.in.www1.artemis.web.rest.errors.EntityNotFoundException) Stream(java.util.stream.Stream) de.tum.in.www1.artemis.domain(de.tum.in.www1.artemis.domain) Service(org.springframework.stereotype.Service) TutorParticipationStatus(de.tum.in.www1.artemis.domain.enumeration.TutorParticipationStatus) ExampleSubmissionRepository(de.tum.in.www1.artemis.repository.ExampleSubmissionRepository) TutorParticipation(de.tum.in.www1.artemis.domain.participation.TutorParticipation) TutorParticipationRepository(de.tum.in.www1.artemis.repository.TutorParticipationRepository) FeedbackType(de.tum.in.www1.artemis.domain.enumeration.FeedbackType) JsonProcessingException(org.springframework.cloud.cloudfoundry.com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(org.springframework.cloud.cloudfoundry.com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

ObjectMapper (org.springframework.cloud.cloudfoundry.com.fasterxml.jackson.databind.ObjectMapper)3 de.tum.in.www1.artemis.domain (de.tum.in.www1.artemis.domain)2 FeedbackType (de.tum.in.www1.artemis.domain.enumeration.FeedbackType)2 TutorParticipationStatus (de.tum.in.www1.artemis.domain.enumeration.TutorParticipationStatus)2 TutorParticipation (de.tum.in.www1.artemis.domain.participation.TutorParticipation)2 ExampleSubmissionRepository (de.tum.in.www1.artemis.repository.ExampleSubmissionRepository)2 TutorParticipationRepository (de.tum.in.www1.artemis.repository.TutorParticipationRepository)2 FeedbackCorrectionErrorType (de.tum.in.www1.artemis.service.TutorParticipationService.FeedbackCorrectionErrorType)2 BadRequestAlertException (de.tum.in.www1.artemis.web.rest.errors.BadRequestAlertException)2 EntityNotFoundException (de.tum.in.www1.artemis.web.rest.errors.EntityNotFoundException)2 java.util (java.util)2 Collectors (java.util.stream.Collectors)2 Stream (java.util.stream.Stream)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 JsonProcessingException (org.springframework.cloud.cloudfoundry.com.fasterxml.jackson.core.JsonProcessingException)2 Service (org.springframework.stereotype.Service)2 Properties (java.util.Properties)1 Test (org.junit.Test)1