Skip to content

Commit

Permalink
Refactor tests based on code review
Browse files Browse the repository at this point in the history
  • Loading branch information
dima767 committed Oct 22, 2018
1 parent 1303209 commit b810489
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 554 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,10 @@ class MetadataSourcesUiDefinitionController {
ResponseEntity<?> getUiDefinitionJsonSchema() {
try {
def parsedJson = jacksonObjectMapper.readValue(this.jsonSchemaLocation.url, Map)
def widget = parsedJson["properties"]["attributeRelease"]["widget"]
def data = []
customAttributesConfiguration.getAttributes().each {
def attribute = [:]
attribute["key"] = it["name"]
attribute["label"] = it["displayName"]
data << attribute
parsedJson['properties']['attributeRelease']['widget']['data'] =
customAttributesConfiguration.getAttributes().collect {
[key: it['name'], label: it['displayName']]
}
widget["data"] = data
return ResponseEntity.ok(parsedJson)
}
catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import edu.internet2.tier.shibboleth.admin.ui.jsonschema.MetadataSourcesJsonSchemaResourceLocation;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ResourceLoader;
Expand All @@ -10,10 +11,20 @@
* @author Dmitriy Kopylenko
*/
@Configuration
@ConfigurationProperties("shibui")
public class JsonSchemaValidationComponentsConfiguration {

//Configured via @ConfigurationProperties (using setter method) with 'shibui.metadata-sources-ui-schema-location' property and default
//value set here if that property is not explicitly set in application.properties
private String metadataSourcesUiSchemaLocation ="classpath:metadata-sources-ui-schema.json";

//This setter is used by Boot's @ConfiguratonProperties binding machinery
public void setMetadataSourcesUiSchemaLocation(String metadataSourcesUiSchemaLocation) {
this.metadataSourcesUiSchemaLocation = metadataSourcesUiSchemaLocation;
}

@Bean
public MetadataSourcesJsonSchemaResourceLocation metadataSourcesJsonSchemaResourceLocation(ResourceLoader resourceLoader, ObjectMapper jacksonMapper) {
return new MetadataSourcesJsonSchemaResourceLocation(resourceLoader, jacksonMapper);
return new MetadataSourcesJsonSchemaResourceLocation(metadataSourcesUiSchemaLocation, resourceLoader, jacksonMapper);
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
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;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
Expand All @@ -21,28 +15,33 @@
*
* @author Dmitriy Kopylenko
*/
@ConfigurationProperties("shibui")
public class MetadataSourcesJsonSchemaResourceLocation {

//Configured via @ConfigurationProperties with 'shibui.metadata-sources-ui-schema-location' property and default
//value set here if that property is not explicitly set in application.properties
private String metadataSourcesUiSchemaLocation = "classpath:metadata-sources-ui-schema.json";
private final String metadataSourcesUiSchemaLocation;

private URL jsonSchemaUrl;

private ResourceLoader resourceLoader;
private final ResourceLoader resourceLoader;

private ObjectMapper jacksonMapper;
private final ObjectMapper jacksonMapper;

private boolean detectMalformedJsonDuringInit = true;


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

public void setMetadataSourcesUiSchemaLocation(String metadataSourcesUiSchemaLocation) {
//This constructor is used in tests
public MetadataSourcesJsonSchemaResourceLocation(String metadataSourcesUiSchemaLocation,
ResourceLoader resourceLoader,
ObjectMapper jacksonMapper,
boolean detectMalformedJsonDuringInit) {
this.metadataSourcesUiSchemaLocation = metadataSourcesUiSchemaLocation;
this.resourceLoader = resourceLoader;
this.jacksonMapper = jacksonMapper;
this.detectMalformedJsonDuringInit = detectMalformedJsonDuringInit;
}

public URL getUrl() {
Expand All @@ -62,8 +61,10 @@ 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);
if(this.detectMalformedJsonDuringInit) {
//Detect malformed JSON schema early, during application start up and fail fast with useful exception message
this.jacksonMapper.readValue(this.jsonSchemaUrl, Map.class);
}
}
catch (Exception ex) {
StringBuilder msg =
Expand Down
4 changes: 0 additions & 4 deletions backend/src/main/resources/i18n/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,6 @@ brand.footer.links-label-4=Mailing List
brand.footer.links-desc-4=Shibboleth.net open-source community mailing list
brand.footer.copyright=Copyright \u00A9 Internet2

brand.unicon=Unicon
brand.unicon-logo=Unicon Logo
brand.i2=Internet 2
brand.i2-logo=Internet 2 Logo
brand.in-partnership-with=In partnership with
brand.and=and

Expand Down
4 changes: 0 additions & 4 deletions backend/src/main/resources/i18n/messages_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,6 @@ brand.footer.links-label-4=Mailing List
brand.footer.links-desc-4=Shibboleth.net open-source community mailing list
brand.footer.copyright=Copyright \u00A9 Internet2

brand.unicon=Unicon
brand.unicon-logo=Unicon Logo
brand.i2=Internet 2
brand.i2-logo=Internet 2 Logo
brand.in-partnership-with=In partnership with
brand.and=and

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package edu.internet2.tier.shibboleth.admin.ui.controller

import com.fasterxml.jackson.databind.ObjectMapper
import edu.internet2.tier.shibboleth.admin.ui.jsonschema.MetadataSourcesJsonSchemaResourceLocation
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.context.TestConfiguration
import org.springframework.boot.test.web.client.TestRestTemplate
import org.springframework.context.annotation.Bean
import org.springframework.core.io.ResourceLoader
import org.springframework.test.context.ActiveProfiles
import spock.lang.Specification

/**
* @author Dmitriy Kopylenko
*/
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("no-auth")
class BadJSONMetadataSourcesUiDefinitionControllerIntegrationTests extends Specification {

@Autowired
private TestRestTemplate restTemplate

@Autowired
MetadataSourcesJsonSchemaResourceLocation schemaLocation

static RESOURCE_URI = '/api/ui/MetadataSources'

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

then: "Request results in HTTP 500"
result.statusCodeValue == 500
result.body.jsonParseError
result.body.sourceUiSchemaDefinitionFile
}

@TestConfiguration
static class Config {
@Bean
MetadataSourcesJsonSchemaResourceLocation metadataSourcesJsonSchemaResourceLocation(ResourceLoader resourceLoader,
ObjectMapper jacksonMapper) {

new MetadataSourcesJsonSchemaResourceLocation('classpath:metadata-sources-ui-schema_MALFORMED.json',
resourceLoader, jacksonMapper, false)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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.test.context.ActiveProfiles
import spock.lang.Specification

/**
* @author Dmitriy Kopylenko
*/
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("no-auth")
class GoodJSONMetadataSourcesUiDefinitionControllerIntegrationTests extends Specification {

@Autowired
private TestRestTemplate restTemplate

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"
result.statusCodeValue == 200
result.body.properties.entityId.title == 'label.entity-id'
}
}

This file was deleted.

Loading

0 comments on commit b810489

Please sign in to comment.