From ac1c685842d4a8f90ed81149de0df18697babaf3 Mon Sep 17 00:00:00 2001 From: Bill Smith Date: Thu, 10 May 2018 10:28:10 -0700 Subject: [PATCH] [SHIBUI-479] Updated unit tests with version changes. Updated controllers to check for version. --- .../EntityDescriptorController.java | 6 +++ .../admin/ui/controller/FilterController.java | 7 +++ .../EntityDescriptorControllerTests.groovy | 54 ++++++++++++------- .../controller/FilterControllerTests.groovy | 10 ++-- 4 files changed, 54 insertions(+), 23 deletions(-) diff --git a/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/controller/EntityDescriptorController.java b/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/controller/EntityDescriptorController.java index 3e9780607..afde9d244 100644 --- a/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/controller/EntityDescriptorController.java +++ b/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/controller/EntityDescriptorController.java @@ -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(HttpStatus.CONFLICT); + } + EntityDescriptor updatedEd = EntityDescriptor.class.cast(entityDescriptorService.createDescriptorFromRepresentation(edRepresentation)); diff --git a/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/controller/FilterController.java b/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/controller/FilterController.java index a818de149..093a5b9ff 100644 --- a/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/controller/FilterController.java +++ b/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/controller/FilterController.java @@ -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.*; @@ -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(HttpStatus.CONFLICT); + } + // convert our representation so we can get the attributes more easily... EntityAttributesFilter updatedFilter = filterService.createFilterFromRepresentation(filterRepresentation); eaf.setName(updatedFilter.getName()); diff --git a/backend/src/test/groovy/edu/internet2/tier/shibboleth/admin/ui/controller/EntityDescriptorControllerTests.groovy b/backend/src/test/groovy/edu/internet2/tier/shibboleth/admin/ui/controller/EntityDescriptorControllerTests.groovy index 205e91e9b..7d6c7481d 100644 --- a/backend/src/test/groovy/edu/internet2/tier/shibboleth/admin/ui/controller/EntityDescriptorControllerTests.groovy +++ b/backend/src/test/groovy/edu/internet2/tier/shibboleth/admin/ui/controller/EntityDescriptorControllerTests.groovy @@ -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 = """ [ { @@ -79,7 +80,8 @@ class EntityDescriptorControllerTests extends Specification { "securityInfo": null, "assertionConsumerServices": null, "relyingPartyOverrides": null, - "attributeRelease": null + "attributeRelease": null, + "version": $version } ] """ @@ -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 = """ [ { @@ -125,7 +130,8 @@ class EntityDescriptorControllerTests extends Specification { "securityInfo": null, "assertionConsumerServices": null, "relyingPartyOverrides": null, - "attributeRelease": null + "attributeRelease": null, + "version": $versionOne }, { "id": "uuid-2", @@ -142,7 +148,8 @@ class EntityDescriptorControllerTests extends Specification { "securityInfo": null, "assertionConsumerServices": null, "relyingPartyOverrides": null, - "attributeRelease": null + "attributeRelease": null, + "version": $versionTwo } ] """ @@ -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 = """ { @@ -208,7 +219,8 @@ class EntityDescriptorControllerTests extends Specification { "securityInfo": null, "assertionConsumerServices": null, "relyingPartyOverrides": null, - "attributeRelease": null + "attributeRelease": null, + "version": $version } """ @@ -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)) @@ -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}", @@ -309,7 +324,8 @@ class EntityDescriptorControllerTests extends Specification { "securityInfo": null, "assertionConsumerServices": null, "relyingPartyOverrides": null, - "attributeRelease": null + "attributeRelease": null, + "version": $version } """ @@ -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)) diff --git a/backend/src/test/groovy/edu/internet2/tier/shibboleth/admin/ui/controller/FilterControllerTests.groovy b/backend/src/test/groovy/edu/internet2/tier/shibboleth/admin/ui/controller/FilterControllerTests.groovy index f69883052..06be14ccb 100644 --- a/backend/src/test/groovy/edu/internet2/tier/shibboleth/admin/ui/controller/FilterControllerTests.groovy +++ b/backend/src/test/groovy/edu/internet2/tier/shibboleth/admin/ui/controller/FilterControllerTests.groovy @@ -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 @@ -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()) @@ -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 filters) {