Skip to content

Commit

Permalink
SHIBUI-2270
Browse files Browse the repository at this point in the history
expanding backend API and services supporting the API


Former-commit-id: 375522a
  • Loading branch information
chasegawa committed Aug 19, 2022
1 parent abafe11 commit 4fa6854
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -11,6 +12,8 @@
public interface ShibConfigurationService {
void addAllConfigurationProperties(Collection<ShibConfigurationProperty> newProperties);

ShibPropertySet create(ShibPropertySet set) throws ObjectIdExistsException;

void delete(int resourceId) throws EntityNotFoundException;

List<ShibConfigurationProperty> getAllConfigurationProperties();
Expand All @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@
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;
import edu.internet2.tier.shibboleth.admin.ui.repository.ShibPropertySettingRepository;
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 {
Expand All @@ -34,6 +33,18 @@ public void addAllConfigurationProperties(Collection<ShibConfigurationProperty>
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);
Expand Down Expand Up @@ -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<ShibPropertySetting> propertiesToUpdate = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class ShibConfigurationServiceTests extends AbstractBaseDataJpaTest {

it
}
service.save(set)
service.create(set)
ShibPropertySet dbSet = propertySetRepo.findByName("somerandom")

then:
Expand All @@ -127,15 +127,15 @@ 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:
updatedSet.properties.size() == 3
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:
Expand Down

0 comments on commit 4fa6854

Please sign in to comment.