Skip to content

Commit

Permalink
SHIBUI-2059
Browse files Browse the repository at this point in the history
added logic for delete bundles
  • Loading branch information
chasegawa committed Sep 11, 2021
1 parent de71b30 commit 9e36ffb
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package edu.internet2.tier.shibboleth.admin.ui.controller;

import edu.internet2.tier.shibboleth.admin.ui.domain.AttributeBundle;
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.security.exception.GroupDeleteException;
import edu.internet2.tier.shibboleth.admin.ui.security.exception.GroupExistsConflictException;
import edu.internet2.tier.shibboleth.admin.ui.security.model.Group;
import edu.internet2.tier.shibboleth.admin.ui.service.AttributeBundleService;
Expand All @@ -11,7 +13,9 @@
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.annotation.Secured;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -37,7 +41,13 @@ public ResponseEntity<?> create(@RequestBody AttributeBundle bundle) throws Obje
return ResponseEntity.status(HttpStatus.CREATED).body(result);
}

//DELETE
@Secured("ROLE_ADMIN")
@DeleteMapping("/{resourceId}")
@Transactional
public ResponseEntity<?> delete(@PathVariable String resourceId) throws EntityNotFoundException {
attributeBundleService.deleteDefinition(resourceId);
return ResponseEntity.noContent().build();
}

//PUT
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package edu.internet2.tier.shibboleth.admin.ui.service;

import edu.internet2.tier.shibboleth.admin.ui.domain.AttributeBundle;
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.AttributeBundleRepository;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -23,4 +24,11 @@ public AttributeBundle create(AttributeBundle bundle) throws ObjectIdExistsExcep
public List<AttributeBundle> findAll() {
return attributeBundleRepository.findAll();
}

public void deleteDefinition(String resourceId) throws EntityNotFoundException {
if (attributeBundleRepository.findByResourceId(resourceId).isEmpty()) {
throw new EntityNotFoundException(String.format("Unable to find attribute bundle with resource id: [%s] for deletion", resourceId));
}
attributeBundleRepository.deleteById(resourceId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
/**
* This simplifies translation to the front end. We use the ENUM on the backend, but the <code>BundleableAttributeType</code>
* is tagged to serialize using this helper.
* Note: The deserialize is done naturally by setting <code>spring.jackson.mapper.accept-case-insensitive-enums=true</code> in
* the application.properties and by the setup of the ENUM itself
* Note: The deserialize is done by the setup of the ENUM itself
*/
public class BundleableAttributeTypeValueSerializer extends StdSerializer<BundleableAttributeType> {
public BundleableAttributeTypeValueSerializer() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.MapperFeature
import com.fasterxml.jackson.databind.ObjectMapper
import edu.internet2.tier.shibboleth.admin.ui.configuration.ShibUIConfiguration
import edu.internet2.tier.shibboleth.admin.ui.domain.AttributeBundle
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.AttributeBundleRepository
import edu.internet2.tier.shibboleth.admin.ui.service.AttributeBundleService
Expand All @@ -22,6 +23,7 @@ import spock.lang.Specification
import static org.hamcrest.CoreMatchers.containsString
import static org.hamcrest.Matchers.containsInAnyOrder
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.result.MockMvcResultMatchers.content
Expand Down Expand Up @@ -127,6 +129,40 @@ class AttributeBundleControllerTests extends Specification {
.andExpect(jsonPath("\$.attributes", containsInAnyOrder("eduPersonPrincipalName", "surname", "givenName")))
}

def "test delete" () {
expect:
attributeBundleRepository.findAll().isEmpty()

when:
def json = """
{
"name": "bundleName",
"resourceId": "randomIDVal",
"attributes": ["eduPersonPrincipalName", "surname", "givenName"]
}
"""
AttributeBundle bundle = objectMapper.readValue(json, AttributeBundle.class)
attributeBundleRepository.save(bundle)

then:
attributeBundleRepository.findAll().size() == 1

// Delete something doesn't exist
try {
mockMvc.perform(delete("/api/custom/entity/bundles/randomIDValdoesntexist"))
false
} catch (NestedServletException expected) {
expected instanceof EntityNotFoundException
}

when: "Delete what does exist"
def result = mockMvc.perform(delete("/api/custom/entity/bundles/randomIDVal"))

then:
result.andExpect(status().isNoContent())
attributeBundleRepository.findAll().isEmpty()
}

// can go away with merge to develop and this extends the base test class
@TestConfiguration
private static class ABCTConfig {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import spock.lang.Specification
@ContextConfiguration(classes = [ShibUIConfiguration])
class AttributeBundleRepositoryTests extends Specification {
@Autowired
AttributeBundleRepository abRepo
AttributeBundleRepository attributeBundleRepository

ObjectMapper objectMapper = new ObjectMapper().with {
it.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS)
Expand All @@ -37,7 +37,7 @@ class AttributeBundleRepositoryTests extends Specification {
AttributeBundle bundle = objectMapper.readValue(json, AttributeBundle.class)

when:
def result = abRepo.save(bundle)
def result = attributeBundleRepository.save(bundle)

then:
result == bundle
Expand Down

0 comments on commit 9e36ffb

Please sign in to comment.