Skip to content

Commit

Permalink
Showing 2 changed files with 34 additions and 3 deletions.
@@ -4,6 +4,8 @@
import edu.internet2.tier.shibboleth.admin.ui.security.repository.UserRepository;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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.RequestMapping;
@@ -29,16 +31,30 @@ public UsersController(UserRepository userRepository) {
this.userRepository = userRepository;
}

@Transactional(readOnly = true)
@GetMapping
public List<User> getAll() {
return userRepository.findAll();
}

@Transactional(readOnly = true)
@GetMapping("/{username}")
public ResponseEntity<?> getOne(@PathVariable String username) {
return ResponseEntity
.ok(userRepository.findByUsername(username)
.orElseThrow(() -> new HttpClientErrorException(NOT_FOUND, String.format("User with username [%s] not found", username))));
return ResponseEntity.ok(findUserOrThrowHttp404(username));
}

@Transactional
@DeleteMapping("/{username}")
public ResponseEntity<?> deleteOne(@PathVariable String username) {
User user = findUserOrThrowHttp404(username);
userRepository.delete(user);
return ResponseEntity.noContent().build();
}


private User findUserOrThrowHttp404(String username) {
return userRepository.findByUsername(username)
.orElseThrow(() -> new HttpClientErrorException(NOT_FOUND, String.format("User with username [%s] not found", username)));
}

}
@@ -47,4 +47,19 @@ class UsersControllerIntegrationTests extends Specification {
result.body.errorCode == '404'
result.body.errorMessage == 'User with username [bogus] not found'
}
def 'DELETE ONE existing user'() {
when: 'GET request is made for one existing user'
def result = this.restTemplate.getForEntity("$RESOURCE_URI/admin", Map)
then: 'Request completed with HTTP 200'
result.statusCodeValue == 200
when: 'DELETE request is made'
this.restTemplate.delete("$RESOURCE_URI/admin")
result = this.restTemplate.getForEntity("$RESOURCE_URI/admin", Map)
then: 'The deleted user is gone'
result.statusCodeValue == 404
}
}

0 comments on commit 0c26b6f

Please sign in to comment.