Skip to content

Commit

Permalink
[SHIBUI-479]
Browse files Browse the repository at this point in the history
Updated unit tests with version changes.
Updated controllers to check for version.
  • Loading branch information
Bill Smith committed May 10, 2018
1 parent 097fa15 commit ac1c685
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ public ResponseEntity<?> update(@RequestBody EntityDescriptorRepresentation edRe
if (existingEd == null) {
return ResponseEntity.notFound().build();
}

// Verify we're the only one attempting to update the EntityDescriptor
if (edRepresentation.getVersion() != existingEd.hashCode()) {
return new ResponseEntity<Void>(HttpStatus.CONFLICT);
}

EntityDescriptor updatedEd =
EntityDescriptor.class.cast(entityDescriptorService.createDescriptorFromRepresentation(edRepresentation));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
Expand Down Expand Up @@ -98,6 +99,12 @@ public ResponseEntity<?> update(@PathVariable String metadataResolverId, @Reques
}

EntityAttributesFilter eaf = filters.get(0);

// Verify we're the only one attempting to update the filter
if (filterRepresentation.getVersion() != eaf.hashCode()) {
return new ResponseEntity<Void>(HttpStatus.CONFLICT);
}

// convert our representation so we can get the attributes more easily...
EntityAttributesFilter updatedFilter = filterService.createFilterFromRepresentation(filterRepresentation);
eaf.setName(updatedFilter.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@ class EntityDescriptorControllerTests extends Specification {
def 'GET /EntityDescriptors with 1 record in repository'() {
given:
def expectedCreationDate = '2017-10-23T11:11:11'
def oneRecordFromRepository =
[new EntityDescriptor(resourceId: 'uuid-1', entityID: 'eid1', serviceProviderName: 'sp1', serviceEnabled: true,
createdDate: LocalDateTime.parse(expectedCreationDate))].stream()
def entityDescriptor = new EntityDescriptor(resourceId: 'uuid-1', entityID: 'eid1', serviceProviderName: 'sp1', serviceEnabled: true,
createdDate: LocalDateTime.parse(expectedCreationDate))
def oneRecordFromRepository = [entityDescriptor].stream()
def version = entityDescriptor.hashCode()
def expectedOneRecordListResponseBody = """
[
{
Expand All @@ -79,7 +80,8 @@ class EntityDescriptorControllerTests extends Specification {
"securityInfo": null,
"assertionConsumerServices": null,
"relyingPartyOverrides": null,
"attributeRelease": null
"attributeRelease": null,
"version": $version
}
]
"""
Expand All @@ -102,12 +104,15 @@ class EntityDescriptorControllerTests extends Specification {
def 'GET /EntityDescriptors with 2 records in repository'() {
given:
def expectedCreationDate = '2017-10-23T11:11:11'
def twoRecordsFromRepository = [new EntityDescriptor(resourceId: 'uuid-1', entityID: 'eid1', serviceProviderName: 'sp1',
def entityDescriptorOne = new EntityDescriptor(resourceId: 'uuid-1', entityID: 'eid1', serviceProviderName: 'sp1',
serviceEnabled: true,
createdDate: LocalDateTime.parse(expectedCreationDate)),
new EntityDescriptor(resourceId: 'uuid-2', entityID: 'eid2', serviceProviderName: 'sp2',
serviceEnabled: false,
createdDate: LocalDateTime.parse(expectedCreationDate))].stream()
createdDate: LocalDateTime.parse(expectedCreationDate))
def versionOne = entityDescriptorOne.hashCode()
def entityDescriptorTwo = new EntityDescriptor(resourceId: 'uuid-2', entityID: 'eid2', serviceProviderName: 'sp2',
serviceEnabled: false,
createdDate: LocalDateTime.parse(expectedCreationDate))
def versionTwo = entityDescriptorTwo.hashCode()
def twoRecordsFromRepository = [entityDescriptorOne, entityDescriptorTwo].stream()
def expectedTwoRecordsListResponseBody = """
[
{
Expand All @@ -125,7 +130,8 @@ class EntityDescriptorControllerTests extends Specification {
"securityInfo": null,
"assertionConsumerServices": null,
"relyingPartyOverrides": null,
"attributeRelease": null
"attributeRelease": null,
"version": $versionOne
},
{
"id": "uuid-2",
Expand All @@ -142,7 +148,8 @@ class EntityDescriptorControllerTests extends Specification {
"securityInfo": null,
"assertionConsumerServices": null,
"relyingPartyOverrides": null,
"attributeRelease": null
"attributeRelease": null,
"version": $versionTwo
}
]
"""
Expand Down Expand Up @@ -170,6 +177,10 @@ class EntityDescriptorControllerTests extends Specification {
def expectedUUID = 'uuid-1'
def expectedResponseHeader = 'Location'
def expectedResponseHeaderValue = "/api/EntityDescriptor/$expectedUUID"
def entityDescriptor = new EntityDescriptor(resourceId: expectedUUID, entityID: expectedEntityId, serviceProviderName: expectedSpName,
serviceEnabled: true,
createdDate: LocalDateTime.parse(expectedCreationDate))
def version = entityDescriptor.hashCode()

def postedJsonBody = """
{
Expand Down Expand Up @@ -208,7 +219,8 @@ class EntityDescriptorControllerTests extends Specification {
"securityInfo": null,
"assertionConsumerServices": null,
"relyingPartyOverrides": null,
"attributeRelease": null
"attributeRelease": null,
"version": $version
}
"""

Expand All @@ -227,9 +239,7 @@ class EntityDescriptorControllerTests extends Specification {
it.entityID == expectedEntityId &&
it.serviceProviderName == expectedSpName &&
it.serviceEnabled == true
}) >> new EntityDescriptor(resourceId: expectedUUID, entityID: expectedEntityId, serviceProviderName: expectedSpName,
serviceEnabled: true,
createdDate: LocalDateTime.parse(expectedCreationDate))
}) >> entityDescriptor

result.andExpect(status().isCreated())
.andExpect(content().json(expectedJsonBody, true))
Expand Down Expand Up @@ -292,6 +302,11 @@ class EntityDescriptorControllerTests extends Specification {
def expectedSpName = 'sp1'
def expectedEntityId = 'eid1'

def entityDescriptor = new EntityDescriptor(resourceId: providedResourceId, entityID: expectedEntityId, serviceProviderName: expectedSpName,
serviceEnabled: true,
createdDate: LocalDateTime.parse(expectedCreationDate))
def version = entityDescriptor.hashCode()

def expectedJsonBody = """
{
"id": "${providedResourceId}",
Expand All @@ -309,7 +324,8 @@ class EntityDescriptorControllerTests extends Specification {
"securityInfo": null,
"assertionConsumerServices": null,
"relyingPartyOverrides": null,
"attributeRelease": null
"attributeRelease": null,
"version": $version
}
"""

Expand All @@ -318,10 +334,8 @@ class EntityDescriptorControllerTests extends Specification {

then:
//EntityDescriptor found
1 * entityDescriptorRepository.findByResourceId(providedResourceId) >>
new EntityDescriptor(resourceId: providedResourceId, entityID: expectedEntityId, serviceProviderName: expectedSpName,
serviceEnabled: true,
createdDate: LocalDateTime.parse(expectedCreationDate))
1 * entityDescriptorRepository.findByResourceId(providedResourceId) >> entityDescriptor


result.andExpect(status().isOk())
.andExpect(content().json(expectedJsonBody, true))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import edu.internet2.tier.shibboleth.admin.ui.service.MetadataResolverService
import edu.internet2.tier.shibboleth.admin.ui.util.RandomGenerator
import edu.internet2.tier.shibboleth.admin.ui.util.TestObjectGenerator
import edu.internet2.tier.shibboleth.admin.util.AttributeUtility
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.autoconfigure.domain.EntityScan
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest
Expand Down Expand Up @@ -160,8 +162,10 @@ class FilterControllerTests extends Specification {
def randomFilter = testObjectGenerator.buildEntityAttributesFilter()
def updatedFilter = testObjectGenerator.buildEntityAttributesFilter()
updatedFilter.resourceId = randomFilter.resourceId
def postedJsonBody = mapper.writeValueAsString(
def postedJsonBodyWithoutVersion = mapper.writeValueAsString(
filterService.createRepresentationFromFilter(updatedFilter))
def postedJsonBodyWithVersion = new JsonSlurper().parseText(postedJsonBodyWithoutVersion)
postedJsonBodyWithVersion << [version: randomFilter.hashCode()]

def originalMetadataResolver = new MetadataResolver()
originalMetadataResolver.setResourceId(randomGenerator.randomId())
Expand All @@ -181,11 +185,11 @@ class FilterControllerTests extends Specification {
def result = mockMvc.perform(
put("/api/MetadataResolver/foo/Filter/$filterUUID")
.contentType(APPLICATION_JSON_UTF8)
.content(postedJsonBody))
.content(JsonOutput.toJson(postedJsonBodyWithVersion)))

then:
result.andExpect(status().isOk())
.andExpect(content().json(postedJsonBody, true))
.andExpect(content().json(postedJsonBodyWithoutVersion, true))
}

EntityAttributesFilter chooseRandomFilterFromList(List<EntityAttributesFilter> filters) {
Expand Down

0 comments on commit ac1c685

Please sign in to comment.