Commit c6956a9e authored by femiadeyemi's avatar femiadeyemi
Browse files

improve test coverage

SonarQube set a high bar on test coverage, > 80%.
Base on SonarQube report some few areas of the
controllers need to be covered.

Target: master
Review-at: https://gitlab.hzdr.de/hifis-technical-platform/helmholtz-cerebrum/-/merge_requests/27
parent 73c689ce
package de.helmholtz.marketplace.cerebrum.controller;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.fge.jsonpatch.JsonPatch;
import com.github.fge.jsonpatch.JsonPatchException;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.ArraySchema;
......@@ -14,7 +12,6 @@ import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
......@@ -29,7 +26,6 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
......@@ -209,17 +205,9 @@ public class MarketServiceController
if (Boolean.TRUE.equals(CerebrumEntityUuidGenerator.isValid(uuid))) {
MarketService partiallyUpdatedService = marketServiceRepository.findByUuid(uuid)
.map(service -> {
try {
MarketService marketServicePatched =
CerebrumControllerUtilities.applyPatch(patch, service, MarketService.class);
return marketServiceRepository.save(marketServicePatched);
} catch (JsonPatchException e) {
throw new ResponseStatusException(
HttpStatus.BAD_REQUEST, "json patch body", e);
} catch (JsonProcessingException e) {
throw new ResponseStatusException(
HttpStatus.INTERNAL_SERVER_ERROR, "Internal Server Error", e);
}
MarketService marketServicePatched =
CerebrumControllerUtilities.applyPatch(patch, service, MarketService.class);
return marketServiceRepository.save(marketServicePatched);
})
.orElseThrow(() -> new CerebrumEntityNotFoundException("user", uuid));
return ResponseEntity.ok().body(partiallyUpdatedService);
......
package de.helmholtz.marketplace.cerebrum.controller;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import de.helmholtz.marketplace.cerebrum.entities.MarketUser;
import de.helmholtz.marketplace.cerebrum.errorhandling.CerebrumApiError;
import de.helmholtz.marketplace.cerebrum.errorhandling.exception.CerebrumEntityNotFoundException;
import de.helmholtz.marketplace.cerebrum.errorhandling.exception.CerebrumInvalidUuidException;
import de.helmholtz.marketplace.cerebrum.repository.MarketUserRepository;
import de.helmholtz.marketplace.cerebrum.utils.CerebrumControllerUtilities;
import de.helmholtz.marketplace.cerebrum.utils.CerebrumEntityUuidGenerator;
import com.github.fge.jsonpatch.JsonPatch;
import com.github.fge.jsonpatch.JsonPatchException;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.ArraySchema;
......@@ -25,11 +14,9 @@ import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
......@@ -42,7 +29,6 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
......@@ -50,9 +36,16 @@ import javax.validation.Valid;
import javax.validation.constraints.Min;
import java.net.URI;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;
import de.helmholtz.marketplace.cerebrum.entities.MarketUser;
import de.helmholtz.marketplace.cerebrum.errorhandling.CerebrumApiError;
import de.helmholtz.marketplace.cerebrum.errorhandling.exception.CerebrumEntityNotFoundException;
import de.helmholtz.marketplace.cerebrum.errorhandling.exception.CerebrumInvalidUuidException;
import de.helmholtz.marketplace.cerebrum.repository.MarketUserRepository;
import de.helmholtz.marketplace.cerebrum.utils.CerebrumControllerUtilities;
import de.helmholtz.marketplace.cerebrum.utils.CerebrumEntityUuidGenerator;
@RestController
@Validated
@RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE,
......@@ -98,14 +91,6 @@ public class MarketUserController {
.block();
}
@SuppressWarnings("unused")
public boolean isSomebody(JwtAuthenticationToken token) {
if (Objects.isNull(token)) return false;
String name = marketUserRepository.findBySub((String) token.getTokenAttributes().get("sub")).getFirstName();
if (Objects.isNull(name)) return false;
return name.trim().isEmpty();
}
/* get users */
@Operation(summary = "get array list of all users")
@ApiResponses(value = {
......@@ -261,17 +246,9 @@ public class MarketUserController {
if (Boolean.TRUE.equals(CerebrumEntityUuidGenerator.isValid(uuid))) {
MarketUser partiallyUpdatedUser = marketUserRepository.findByUuid(uuid)
.map(user -> {
try {
MarketUser marketUserPatched =
CerebrumControllerUtilities.applyPatch(patch, user, MarketUser.class);
return marketUserRepository.save(marketUserPatched);
} catch (JsonPatchException e) {
throw new ResponseStatusException(
HttpStatus.BAD_REQUEST, "json patch body", e);
} catch (JsonProcessingException e) {
throw new ResponseStatusException(
HttpStatus.INTERNAL_SERVER_ERROR, "Internal Server Error", e);
}
MarketUser marketUserPatched =
CerebrumControllerUtilities.applyPatch(patch, user, MarketUser.class);
return marketUserRepository.save(marketUserPatched);
})
.orElseThrow(() -> new CerebrumEntityNotFoundException("user", uuid));
return ResponseEntity.ok().body(partiallyUpdatedUser);
......
package de.helmholtz.marketplace.cerebrum.controller;
import de.helmholtz.marketplace.cerebrum.entities.Organization;
import de.helmholtz.marketplace.cerebrum.errorhandling.CerebrumApiError;
import de.helmholtz.marketplace.cerebrum.errorhandling.exception.CerebrumEntityNotFoundException;
import de.helmholtz.marketplace.cerebrum.errorhandling.exception.CerebrumInvalidUuidException;
import de.helmholtz.marketplace.cerebrum.repository.OrganizationRepository;
import de.helmholtz.marketplace.cerebrum.utils.CerebrumControllerUtilities;
import de.helmholtz.marketplace.cerebrum.utils.CerebrumEntityUuidGenerator;
import com.github.fge.jsonpatch.JsonPatch;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.ArraySchema;
......@@ -17,27 +10,22 @@ import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.fge.jsonpatch.JsonPatch;
import com.github.fge.jsonpatch.JsonPatchException;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
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.RestController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
......@@ -47,6 +35,14 @@ import java.net.URI;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import de.helmholtz.marketplace.cerebrum.entities.Organization;
import de.helmholtz.marketplace.cerebrum.errorhandling.CerebrumApiError;
import de.helmholtz.marketplace.cerebrum.errorhandling.exception.CerebrumEntityNotFoundException;
import de.helmholtz.marketplace.cerebrum.errorhandling.exception.CerebrumInvalidUuidException;
import de.helmholtz.marketplace.cerebrum.repository.OrganizationRepository;
import de.helmholtz.marketplace.cerebrum.utils.CerebrumControllerUtilities;
import de.helmholtz.marketplace.cerebrum.utils.CerebrumEntityUuidGenerator;
@RestController
@Validated
@RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE,
......@@ -215,17 +211,9 @@ public class OrganizationController {
if (Boolean.TRUE.equals(CerebrumEntityUuidGenerator.isValid(uuid))) {
Organization partialUpdateOrganisation = organizationRepository.findByUuid(uuid)
.map(organization -> {
try {
Organization organizationPatched =
CerebrumControllerUtilities.applyPatch(patch, organization, Organization.class);
return organizationRepository.save(organizationPatched);
} catch (JsonPatchException e) {
throw new ResponseStatusException(
HttpStatus.BAD_REQUEST, "json patch body", e);
} catch (JsonProcessingException e) {
throw new ResponseStatusException(
HttpStatus.INTERNAL_SERVER_ERROR, "Internal Server Error", e);
}
Organization organizationPatched =
CerebrumControllerUtilities.applyPatch(patch, organization, Organization.class);
return organizationRepository.save(organizationPatched);
})
.orElseThrow(() -> new CerebrumEntityNotFoundException("organization", uuid));
return ResponseEntity.ok().body(partialUpdateOrganisation);
......
......@@ -6,6 +6,8 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.fge.jsonpatch.JsonPatch;
import com.github.fge.jsonpatch.JsonPatchException;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.web.client.HttpServerErrorException;
import java.util.ArrayList;
import java.util.List;
......@@ -16,11 +18,15 @@ public final class CerebrumControllerUtilities
{ }
public static <T> T applyPatch(
JsonPatch patch, T target, Class<T> clazz) throws JsonPatchException, JsonProcessingException
JsonPatch patch, T target, Class<T> clazz)
{
ObjectMapper objectMapper = new ObjectMapper();
JsonNode patched = patch.apply(objectMapper.convertValue(target, JsonNode.class));
return objectMapper.treeToValue(patched, clazz);
try {
ObjectMapper objectMapper = new ObjectMapper();
JsonNode patched = patch.apply(objectMapper.convertValue(target, JsonNode.class));
return objectMapper.treeToValue(patched, clazz);
} catch (JsonPatchException | JsonProcessingException e) {
throw new HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR, e.getLocalizedMessage());
}
}
public static List<Sort.Order> getOrders(List<String> sorts)
......
......@@ -582,6 +582,34 @@ class MarketServiceControllerTest
verify(mockRepository, times(1)).findByUuid("svc-5189a7bc-d630-11ea-87d0-0242ac130003");
}
@Test void
givenValidUuid_and_validMarketService_whenPutRequestToServices_verifyOutput_and_businessLogicCall_thenUpdated()
throws Exception
{
//given
String validService = "{\"name\": \"Treeflex\", \"description\": \"Nullam varius. Nulla facilisi.\", " +
"\"url\": \"https://independent.co.uk/ipsum/dolor/sit/amet/consectetuer.aspx\"}";
given(mockRepository.findByUuid("svc-5189a7bc-d630-11ea-87d0-0242ac130003"))
.willReturn(java.util.Optional.of(singleService));
given(mockRepository.save(any(MarketService.class))).willReturn(singleService);
//when
mvc.perform(put(SVC_API_URI + "/svc-5189a7bc-d630-11ea-87d0-0242ac130003")
.header("Authorization", "Bearer " + TOKEN)
.accept("application/json")
.contentType("application/json").content(validService))
//then
.andExpect(status().isOk())
.andExpect(content().contentType("application/json"))
.andExpect(jsonPath("$['name']").value(singleService.getName()))
.andExpect(jsonPath("$['uuid']").value("svc-5189a7bc-d630-11ea-87d0-0242ac130003"));
verify(mockRepository, times(1)).save(any(MarketService.class));
verify(mockRepository, times(1)).findByUuid("svc-5189a7bc-d630-11ea-87d0-0242ac130003");
}
@Test void
givenInvalidUuid_and_validMMarketService_whenPutRequestToServices_thenBadRequest() throws Exception
{
......
......@@ -587,6 +587,36 @@ class MarketUserControllerTest
verify(mockRepository, times(1)).findByUuid("usr-5189a7bc-d630-11ea-87d0-0242ac130003");
}
@Test void
givenValidUuid_and_validMarketUser_whenPutRequestToUsers_verifyOutput_and_businessLogicCall_thenUpdated()
throws Exception
{
//given
String usr = "{\"firstName\":\"Garreth\"," +
"\"lastName\":\"Lages\"," +
"\"email\":\"glages0@tmall.com\"," +
"\"sub\":\"QJQwuNkp-GElX-aSqc-ddNx-HsvatNkUUSVN\"}";
given(mockRepository.findByUuid("usr-5189a7bc-d630-11ea-87d0-0242ac130003"))
.willReturn(java.util.Optional.of(singleUser));
given(mockRepository.save(any(MarketUser.class))).willReturn(singleUser);
//when
mvc.perform(put(USR_API_URI + "/usr-5189a7bc-d630-11ea-87d0-0242ac130003")
.header("Authorization", "Bearer " + TOKEN)
.accept("application/json")
.contentType("application/json").content(usr))
//then
.andExpect(status().isOk())
.andExpect(content().contentType("application/json"))
.andExpect(jsonPath("$['firstName']").value(singleUser.getFirstName()))
.andExpect(jsonPath("$['uuid']").value("usr-5189a7bc-d630-11ea-87d0-0242ac130003"));
verify(mockRepository, times(1)).save(any(MarketUser.class));
verify(mockRepository, times(1)).findByUuid("usr-5189a7bc-d630-11ea-87d0-0242ac130003");
}
@Test void
givenInvalidUuid_and_validMarketUser_whenPutRequestToUsers_thenBadRequest() throws Exception
{
......
......@@ -691,6 +691,42 @@ class OrganizationControllerTest
verify(mockRepository, times(1)).findByUuid("org-5189a7bc-d630-11ea-87d0-0242ac130003");
}
@Test void
givenValidUuid_and_validOrganisation_whenPutRequestToOrganisations_verifyOutput_and_BusinessLogicCall_thenUpdated()
throws Exception
{
//given
String kit= "{\"name\":\"Karlsruher Institut three fuer Technologie\"," +
"\"img\":\"http://www.kiiiit.edu/img/intern/kit_logo_V2_de.svg\"," +
"\"url\":\"http://www.kiiiit.edu/\"}";
Organization newKit = createNewOrganisationWithUuiD(
"Karlsruher Institut three fuer Technologie",
"KIT",
"http://www.kiiiit.edu/",
"http://www.kiiiit.edu/img/intern/kit_logo_V2_de.svg",
"org-5189a7bc-d630-11ea-87d0-0242ac130003");
given(mockRepository.findByUuid("org-5189a7bc-d630-11ea-87d0-0242ac130003"))
.willReturn(java.util.Optional.of(newKit));
given(mockRepository.save(any(Organization.class))).willReturn(newKit);
//when
mvc.perform(put(ORG_API_URI + "/org-5189a7bc-d630-11ea-87d0-0242ac130003")
.header("Authorization", "Bearer " + TOKEN)
.accept("application/json")
.contentType("application/json").content(kit))
//then
.andExpect(status().isOk())
.andExpect(content().contentType("application/json"))
.andExpect(jsonPath("$['name']").value(newKit.getName()))
.andExpect(jsonPath("$['uuid']").value("org-5189a7bc-d630-11ea-87d0-0242ac130003"));
verify(mockRepository, times(1)).save(any(Organization.class));
verify(mockRepository, times(1)).findByUuid("org-5189a7bc-d630-11ea-87d0-0242ac130003");
}
@Test void
givenInvalidUuid_and_validOrganisation_whenPutRequestToOrganisations_thenBadRequest() throws Exception
{
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment