From 47582984d8b4b111f6ffbc605b100658b36d61df Mon Sep 17 00:00:00 2001 From: Dmitriy Kopylenko Date: Mon, 8 Oct 2018 10:37:15 -0400 Subject: [PATCH] WIP2 --- ...tadataSourcesUiDefinitionController.groovy | 23 ++++++++++++++----- ...efinitionControllerIntegrationTests.groovy | 9 ++++---- .../metadata-sources-ui-schema_MALFORMED.json | 4 ++-- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/backend/src/main/groovy/edu/internet2/tier/shibboleth/admin/ui/controller/MetadataSourcesUiDefinitionController.groovy b/backend/src/main/groovy/edu/internet2/tier/shibboleth/admin/ui/controller/MetadataSourcesUiDefinitionController.groovy index 6156b97cd..0471e40dd 100644 --- a/backend/src/main/groovy/edu/internet2/tier/shibboleth/admin/ui/controller/MetadataSourcesUiDefinitionController.groovy +++ b/backend/src/main/groovy/edu/internet2/tier/shibboleth/admin/ui/controller/MetadataSourcesUiDefinitionController.groovy @@ -1,6 +1,6 @@ package edu.internet2.tier.shibboleth.admin.ui.controller -import groovy.json.JsonSlurper +import com.fasterxml.jackson.databind.ObjectMapper import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.context.properties.ConfigurationProperties import org.springframework.core.io.ResourceLoader @@ -10,6 +10,8 @@ import org.springframework.web.bind.annotation.RestController import javax.annotation.PostConstruct +import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR + /** * Controller implementing REST resource responsible for exposing structure definition for metadata sources user * interface in terms of JSON schema. @@ -21,19 +23,28 @@ import javax.annotation.PostConstruct class MetadataSourcesUiDefinitionController { //Configured via @ConfigurationProperties with 'shibui.metadata-sources-ui-schema-location' property and default - //value set here if that property is not set - String metadataSourcesUiSchemaLocation //= 'classpath:metadata-sources-ui-schema.json' + //value set here if that property is not explicitly set in application.properties + String metadataSourcesUiSchemaLocation = 'classpath:metadata-sources-ui-schema.json' URL jsonSchemaUrl @Autowired ResourceLoader resourceLoader + @Autowired + ObjectMapper jacksonObjectMapper + @GetMapping ResponseEntity getUiDefinitionJsonSchema() { - //JsonSlurper is not threadsafe, but cheap to init. New instance per-thread is the canonical usage - def json = new JsonSlurper().parse(this.jsonSchemaUrl) - ResponseEntity.ok(json) + try { + def parsedJson = jacksonObjectMapper.readValue(this.jsonSchemaUrl, Map) + return ResponseEntity.ok(parsedJson) + } + catch (Exception e) { + return ResponseEntity.status(INTERNAL_SERVER_ERROR) + .body([jsonParseError : e.getMessage(), + sourceUiSchemaDefinitionFile: this.jsonSchemaUrl]) + } } @PostConstruct diff --git a/backend/src/test/groovy/edu/internet2/tier/shibboleth/admin/ui/controller/MetadataSourcesUiDefinitionControllerIntegrationTests.groovy b/backend/src/test/groovy/edu/internet2/tier/shibboleth/admin/ui/controller/MetadataSourcesUiDefinitionControllerIntegrationTests.groovy index abc416b78..9d804ad57 100644 --- a/backend/src/test/groovy/edu/internet2/tier/shibboleth/admin/ui/controller/MetadataSourcesUiDefinitionControllerIntegrationTests.groovy +++ b/backend/src/test/groovy/edu/internet2/tier/shibboleth/admin/ui/controller/MetadataSourcesUiDefinitionControllerIntegrationTests.groovy @@ -3,7 +3,6 @@ package edu.internet2.tier.shibboleth.admin.ui.controller import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.test.context.SpringBootTest import org.springframework.boot.test.web.client.TestRestTemplate -import org.springframework.core.env.ConfigurableEnvironment import org.springframework.test.context.ActiveProfiles import spock.lang.Specification @@ -37,12 +36,14 @@ class MetadataSourcesUiDefinitionControllerIntegrationTests extends Specificatio configureMalformedJsonInput() def result = this.restTemplate.getForEntity(RESOURCE_URI, Object) - then: "Request results in HTTP 400" - result.statusCodeValue == 200 + then: "Request results in HTTP 500" + result.statusCodeValue == 500 + result.body.jsonParseError + result.body.sourceUiSchemaDefinitionFile } private configureMalformedJsonInput() { - controllerUnderTest.metadataSourcesUiSchemaLocation = 'classpath:metadata-sources-ui-schema_BAD.json' + controllerUnderTest.metadataSourcesUiSchemaLocation = 'classpath:metadata-sources-ui-schema_MALFORMED.json' controllerUnderTest.init() } } \ No newline at end of file diff --git a/backend/src/test/resources/metadata-sources-ui-schema_MALFORMED.json b/backend/src/test/resources/metadata-sources-ui-schema_MALFORMED.json index 6d4d76a24..7ca5220f0 100644 --- a/backend/src/test/resources/metadata-sources-ui-schema_MALFORMED.json +++ b/backend/src/test/resources/metadata-sources-ui-schema_MALFORMED.json @@ -1,5 +1,5 @@ - - "type": "object", +{ + "type": "object" "properties": { "entityId": { "title": "label.entity-id",