Skip to content

Commit

Permalink
INtegrate with 905 changes
Browse files Browse the repository at this point in the history
  • Loading branch information
dima767 committed Oct 16, 2018
1 parent dcf2d09 commit 97a7419
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package edu.internet2.tier.shibboleth.admin.ui.jsonschema

import com.fasterxml.jackson.databind.ObjectMapper
import edu.internet2.tier.shibboleth.admin.ui.domain.frontend.EntityDescriptorRepresentation
import mjson.Json
import org.springframework.beans.factory.annotation.Autowired
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package edu.internet2.tier.shibboleth.admin.ui.configuration;

import com.fasterxml.jackson.databind.ObjectMapper;
import edu.internet2.tier.shibboleth.admin.ui.jsonschema.MetadataSourcesJsonSchemaResourceLocation;
import edu.internet2.tier.shibboleth.admin.ui.jsonschema.RelyingPartyOverridesJsonSchemaValidatingControllerAdvice;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ResourceLoader;
Expand All @@ -13,7 +13,7 @@
public class JsonSchemaValidationComponentsConfiguration {

@Bean
public MetadataSourcesJsonSchemaResourceLocation metadataSourcesJsonSchemaResourceLocation(ResourceLoader resourceLoader) {
return new MetadataSourcesJsonSchemaResourceLocation(resourceLoader);
public MetadataSourcesJsonSchemaResourceLocation metadataSourcesJsonSchemaResourceLocation(ResourceLoader resourceLoader, ObjectMapper jacksonMapper) {
return new MetadataSourcesJsonSchemaResourceLocation(resourceLoader, jacksonMapper);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package edu.internet2.tier.shibboleth.admin.ui.jsonschema;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
Expand All @@ -12,6 +14,7 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Map;

/**
* Encapsulates metadata sources JSON schema location.
Expand All @@ -27,10 +30,15 @@ public class MetadataSourcesJsonSchemaResourceLocation {

private URL jsonSchemaUrl;

ResourceLoader resourceLoader;
private ResourceLoader resourceLoader;

public MetadataSourcesJsonSchemaResourceLocation(ResourceLoader resourceLoader) {
private ObjectMapper jacksonMapper;



public MetadataSourcesJsonSchemaResourceLocation(ResourceLoader resourceLoader, ObjectMapper jacksonMapper) {
this.resourceLoader = resourceLoader;
this.jacksonMapper = jacksonMapper;
}

public void setMetadataSourcesUiSchemaLocation(String metadataSourcesUiSchemaLocation) {
Expand All @@ -54,9 +62,15 @@ public URI getUri() {
public void init() {
try {
this.jsonSchemaUrl = this.resourceLoader.getResource(this.metadataSourcesUiSchemaLocation).getURL();
//Detect malformed JSON schema early, during application start up and fail fast with useful exception message
this.jacksonMapper.readValue(this.jsonSchemaUrl, Map.class);
}
catch (IOException ex) {
throw new BeanCreationException(ex.getMessage(), ex);
catch (Exception ex) {
StringBuilder msg =
new StringBuilder(String.format("An error is detected during JSON parsing => [%s]", ex.getMessage()));
msg.append(String.format("Offending resource => [%s]", this.metadataSourcesUiSchemaLocation));

throw new BeanInitializationException(msg.toString(), ex);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package edu.internet2.tier.shibboleth.admin.ui.controller

import edu.internet2.tier.shibboleth.admin.ui.jsonschema.MetadataSourcesJsonSchemaResourceLocation
import org.springframework.beans.factory.BeanInitializationException
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.web.client.TestRestTemplate
Expand All @@ -17,13 +19,12 @@ class MetadataSourcesUiDefinitionControllerIntegrationTests extends Specificatio
private TestRestTemplate restTemplate

@Autowired
MetadataSourcesUiDefinitionController controllerUnderTest
MetadataSourcesJsonSchemaResourceLocation schemaLocation

static RESOURCE_URI = '/api/ui/MetadataSources'

def "GET Metadata Sources UI definition schema"() {
when: 'GET request is made for metadata source UI definition schema'

def result = this.restTemplate.getForEntity(RESOURCE_URI, Object)

then: "Request completed successfully"
Expand All @@ -33,7 +34,7 @@ class MetadataSourcesUiDefinitionControllerIntegrationTests extends Specificatio

def "GET Malformed Metadata Sources UI definition schema"() {
when: 'GET request is made for malformed metadata source UI definition schema'
configureMalformedJsonInput()
configureMalformedJsonInput(simulateApplicationStartup { false })
def result = this.restTemplate.getForEntity(RESOURCE_URI, Object)

then: "Request results in HTTP 500"
Expand All @@ -42,8 +43,32 @@ class MetadataSourcesUiDefinitionControllerIntegrationTests extends Specificatio
result.body.sourceUiSchemaDefinitionFile
}

private configureMalformedJsonInput() {
controllerUnderTest.metadataSourcesUiSchemaLocation = 'classpath:metadata-sources-ui-schema_MALFORMED.json'
controllerUnderTest.init()
def "Malformed Metadata Sources UI definition schema is detected during application start up"() {
when: 'Application is starting up and malformed JSON schema is detected'
configureMalformedJsonInput(simulateApplicationStartup { true })

then:
def ex = thrown(BeanInitializationException)
ex.message.contains('An error is detected during JSON parsing =>')
ex.message.contains('Offending resource =>')

}

private configureMalformedJsonInput(boolean simulateApplicationStartup) {
schemaLocation.metadataSourcesUiSchemaLocation = 'classpath:metadata-sources-ui-schema_MALFORMED.json'
try {
schemaLocation.init()
}
catch (Exception e) {
if (simulateApplicationStartup) {
throw e
}
}

}

//Just for the nicer, readable, DSL-like
private static boolean simulateApplicationStartup(Closure booleanFlagSupplier) {
booleanFlagSupplier()
}
}

0 comments on commit 97a7419

Please sign in to comment.