diff --git a/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/controller/ShibPropertiesController.java b/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/controller/ShibPropertiesController.java index 1721228d5..8b3952954 100644 --- a/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/controller/ShibPropertiesController.java +++ b/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/controller/ShibPropertiesController.java @@ -17,6 +17,7 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -64,16 +65,15 @@ public ResponseEntity deletePropertySet(@PathVariable Integer resourceId) thr @Secured("ROLE_ADMIN") @Transactional public ResponseEntity createPropertySet(@RequestBody ShibPropertySet newSet) throws ObjectIdExistsException { - // If already defined, we won't/can't create a new one, nor will this call update on the definition - try { - ShibPropertySet set = service.getSet(newSet.getResourceId()); - throw new ObjectIdExistsException(Integer.toString(newSet.getResourceId())); - } - catch (EntityNotFoundException e) { - // we hope not to find this - do nothing - } - - ShibPropertySet result = service.save(newSet); + ShibPropertySet result = service.create(newSet); return ResponseEntity.status(HttpStatus.CREATED).body(result); } + + @PutMapping("/property/set/{resourceId}") + @Secured("ROLE_ADMIN") + @Transactional + public ResponseEntity updatePropertySet(@RequestBody ShibPropertySet setToUpdate, @PathVariable int resourceId) throws EntityNotFoundException { + ShibPropertySet result = service.update(setToUpdate); + return ResponseEntity.status(HttpStatus.OK).body(result); + } } \ No newline at end of file diff --git a/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/controller/ShibPropertiesControllerExceptionHandler.java b/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/controller/ShibPropertiesControllerExceptionHandler.java index 35adfcef0..bc16bb739 100644 --- a/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/controller/ShibPropertiesControllerExceptionHandler.java +++ b/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/controller/ShibPropertiesControllerExceptionHandler.java @@ -12,27 +12,11 @@ @ControllerAdvice(assignableTypes = {ShibPropertiesController.class}) public class ShibPropertiesControllerExceptionHandler extends ResponseEntityExceptionHandler { - -// @ExceptionHandler({ ConcurrentModificationException.class }) -// public ResponseEntity handleConcurrentModificationException(ConcurrentModificationException e, WebRequest request) { -// return ResponseEntity.status(HttpStatus.CONFLICT).body(new ErrorResponse(HttpStatus.CONFLICT, e.getMessage())); -// } - @ExceptionHandler({ EntityNotFoundException.class }) public ResponseEntity handleEntityNotFoundException(EntityNotFoundException e, WebRequest request) { return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse(HttpStatus.NOT_FOUND, e.getMessage())); } -// @ExceptionHandler({ ForbiddenException.class }) -// public ResponseEntity handleForbiddenAccess(ForbiddenException e, WebRequest request) { -// return ResponseEntity.status(HttpStatus.FORBIDDEN).body(new ErrorResponse(HttpStatus.FORBIDDEN, e.getMessage())); -// } - -// @ExceptionHandler({ InvalidPatternMatchException.class }) -// public ResponseEntity handleInvalidUrlMatchException(InvalidPatternMatchException e, WebRequest request) { -// return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ErrorResponse(HttpStatus.BAD_REQUEST, e.getMessage())); -// } - @ExceptionHandler({ ObjectIdExistsException.class }) public ResponseEntity handleObjectIdExistsException(ObjectIdExistsException e, WebRequest request) { HttpHeaders headers = new HttpHeaders(); diff --git a/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/service/ShibConfigurationService.java b/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/service/ShibConfigurationService.java index d0c220962..64c029d96 100644 --- a/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/service/ShibConfigurationService.java +++ b/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/service/ShibConfigurationService.java @@ -3,6 +3,7 @@ import edu.internet2.tier.shibboleth.admin.ui.domain.shib.properties.ShibConfigurationProperty; import edu.internet2.tier.shibboleth.admin.ui.domain.shib.properties.ShibPropertySet; import edu.internet2.tier.shibboleth.admin.ui.exception.EntityNotFoundException; +import edu.internet2.tier.shibboleth.admin.ui.exception.ObjectIdExistsException; import edu.internet2.tier.shibboleth.admin.ui.repository.ProjectionIdAndName; import java.util.Collection; @@ -11,6 +12,8 @@ public interface ShibConfigurationService { void addAllConfigurationProperties(Collection newProperties); + ShibPropertySet create(ShibPropertySet set) throws ObjectIdExistsException; + void delete(int resourceId) throws EntityNotFoundException; List getAllConfigurationProperties(); @@ -21,9 +24,7 @@ public interface ShibConfigurationService { ShibPropertySet getSet(int resourceId) throws EntityNotFoundException; - ShibPropertySet getSet(String name); - - ShibPropertySet save(ShibPropertySet set); - ShibConfigurationProperty save(ShibConfigurationProperty prop); + + ShibPropertySet update(ShibPropertySet setToUpdate) throws EntityNotFoundException; } \ No newline at end of file diff --git a/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/service/ShibConfigurationServiceImpl.java b/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/service/ShibConfigurationServiceImpl.java index b394caa1f..74d9e3637 100644 --- a/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/service/ShibConfigurationServiceImpl.java +++ b/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/service/ShibConfigurationServiceImpl.java @@ -4,6 +4,7 @@ import edu.internet2.tier.shibboleth.admin.ui.domain.shib.properties.ShibPropertySet; import edu.internet2.tier.shibboleth.admin.ui.domain.shib.properties.ShibPropertySetting; import edu.internet2.tier.shibboleth.admin.ui.exception.EntityNotFoundException; +import edu.internet2.tier.shibboleth.admin.ui.exception.ObjectIdExistsException; import edu.internet2.tier.shibboleth.admin.ui.repository.ProjectionIdAndName; import edu.internet2.tier.shibboleth.admin.ui.repository.ShibConfigurationRepository; import edu.internet2.tier.shibboleth.admin.ui.repository.ShibPropertySetRepository; @@ -11,12 +12,10 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import javax.transaction.Transactional; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.List; -import java.util.ResourceBundle; @Service public class ShibConfigurationServiceImpl implements ShibConfigurationService { @@ -34,6 +33,18 @@ public void addAllConfigurationProperties(Collection shibConfigurationRepository.saveAll(newProperties); } + @Override + public ShibPropertySet create(ShibPropertySet set) throws ObjectIdExistsException { + try { + getSet(set.getResourceId()); + throw new ObjectIdExistsException(Integer.toString(set.getResourceId())); + } + catch (EntityNotFoundException e) { + // we don't want to find the object + } + return save(set); + } + @Override public void delete(int resourceId) throws EntityNotFoundException { ShibPropertySet set = shibPropertySetRepository.findByResourceId(resourceId); @@ -68,19 +79,18 @@ public ShibPropertySet getSet(int resourceId) throws EntityNotFoundException { return result; } - @Override - public ShibPropertySet getSet(String name) { - return shibPropertySetRepository.findByName(name); - } - @Override public ShibConfigurationProperty save(ShibConfigurationProperty prop) { return shibConfigurationRepository.save(prop); } @Override - @Transactional - public ShibPropertySet save(ShibPropertySet incomingPropSet) { + public ShibPropertySet update(ShibPropertySet setToUpdate) throws EntityNotFoundException { + getSet(setToUpdate.getResourceId()); // check that it exists, if not it'll throw an exception + return save(setToUpdate); + } + + private ShibPropertySet save(ShibPropertySet incomingPropSet) { ShibPropertySet result = new ShibPropertySet(); List propertiesToUpdate = new ArrayList<>(); diff --git a/backend/src/test/groovy/edu/internet2/tier/shibboleth/admin/ui/controller/ShibPropertiesControllerTests.groovy b/backend/src/test/groovy/edu/internet2/tier/shibboleth/admin/ui/controller/ShibPropertiesControllerTests.groovy index ae925f074..e5c418f9d 100644 --- a/backend/src/test/groovy/edu/internet2/tier/shibboleth/admin/ui/controller/ShibPropertiesControllerTests.groovy +++ b/backend/src/test/groovy/edu/internet2/tier/shibboleth/admin/ui/controller/ShibPropertiesControllerTests.groovy @@ -23,6 +23,7 @@ import static org.springframework.http.MediaType.APPLICATION_JSON import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath @@ -176,4 +177,34 @@ class ShibPropertiesControllerTests extends AbstractBaseDataJpaTest { def createdSet = propertySetRepo.findByName("somerandom") createdSet.getProperties().size() == 2 } + + @WithMockAdmin + def "PUT /api/shib/property/set update set that doesn't exist"() { + when: + ShibPropertySet set = propertySetRepo.findByResourceId(defaultSetResourceId) + set.resourceId = 1234 + def jsonBody = mapper.writeValueAsString(set) + + then: + try { + mockMvc.perform(put('/api/shib/property/set/1234').contentType(APPLICATION_JSON).content(jsonBody)) + } + catch (Exception e) { + e instanceof EntityNotFoundException + } + } + + @WithMockAdmin + def "PUT /api/shib/property/set update set"() { + when: + ShibPropertySet set = propertySetRepo.findByResourceId(defaultSetResourceId) + set.name = "newName" + def jsonBody = mapper.writeValueAsString(set) + def url = "/api/shib/property/set/{resourceId}" + def result = mockMvc.perform(put(url, defaultSetResourceId).contentType(APPLICATION_JSON).content(jsonBody)) + + then: + result.andExpect(status().isOk()).andExpect(jsonPath("\$.name").value("newName")) + propertySetRepo.findByResourceId(defaultSetResourceId).name.equals("newName") + } } \ No newline at end of file diff --git a/backend/src/test/groovy/edu/internet2/tier/shibboleth/admin/ui/service/ShibConfigurationServiceTests.groovy b/backend/src/test/groovy/edu/internet2/tier/shibboleth/admin/ui/service/ShibConfigurationServiceTests.groovy index f98f692a5..36f548215 100644 --- a/backend/src/test/groovy/edu/internet2/tier/shibboleth/admin/ui/service/ShibConfigurationServiceTests.groovy +++ b/backend/src/test/groovy/edu/internet2/tier/shibboleth/admin/ui/service/ShibConfigurationServiceTests.groovy @@ -106,7 +106,7 @@ class ShibConfigurationServiceTests extends AbstractBaseDataJpaTest { it } - service.save(set) + service.create(set) ShibPropertySet dbSet = propertySetRepo.findByName("somerandom") then: @@ -127,7 +127,7 @@ class ShibConfigurationServiceTests extends AbstractBaseDataJpaTest { defaultSet.properties.add(prop) // create a copy of the set so they can't possibly be real db entities def copySet = objectMapper.readValue(objectMapper.writeValueAsString(defaultSet), ShibPropertySet.class) - service.save(copySet) + service.update(copySet) def updatedSet = propertySetRepo.findByResourceId(defaultSetResourceId) then: @@ -135,7 +135,7 @@ class ShibConfigurationServiceTests extends AbstractBaseDataJpaTest { when: updatedSet.properties.remove(0) - service.save(objectMapper.readValue(objectMapper.writeValueAsString(updatedSet), ShibPropertySet.class)) + service.update(objectMapper.readValue(objectMapper.writeValueAsString(updatedSet), ShibPropertySet.class)) def updatedSet2 = propertySetRepo.findByResourceId(defaultSetResourceId) then: