Skip to content

Commit

Permalink
Claim changes
Browse files Browse the repository at this point in the history
  • Loading branch information
arcshiftsolutions committed Dec 5, 2023
1 parent 4d2a959 commit 5545a6a
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,17 @@ private void validatePayload(SecureExchangeCreate secureExchange) {
}
}

private void validatePayload(SecureExchangeClaimRequest claimRequest) {
for(String possibleUUID: claimRequest.getSecureExchangeIDs()){
try {
UUID.fromString(possibleUUID);
}catch (Exception e){
ApiError error = ApiError.builder().timestamp(LocalDateTime.now()).message("Payload contains invalid secure exchange ID.").status(BAD_REQUEST).build();
throw new InvalidPayloadException(error);
}
}
}

@Override
@Transactional
public ResponseEntity<Void> deleteById(final UUID id) {
Expand All @@ -128,9 +139,11 @@ public ResponseEntity<Void> deleteById(final UUID id) {
}

@Override
public ResponseEntity<List<SecureExchange>> claimAllSecureExchanges(List<UUID> secureExchangeIDs, String reviewer) {
getService().claimAllSecureExchanges(secureExchangeIDs, reviewer);
return ResponseEntity.noContent().build();
public ResponseEntity<List<SecureExchange>> claimAllSecureExchanges(SecureExchangeClaimRequest secureExchangeClaimRequest) {
validatePayload(secureExchangeClaimRequest);
setAuditColumns(secureExchangeClaimRequest);
var savedExchanges = getService().claimAllSecureExchanges(secureExchangeClaimRequest);
return ResponseEntity.ok(savedExchanges.stream().map(mapper::toStructure).collect(Collectors.toList()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package ca.bc.gov.educ.api.edx.endpoint.v1;

import ca.bc.gov.educ.api.edx.constants.v1.URL;
import ca.bc.gov.educ.api.edx.struct.v1.SecureExchange;
import ca.bc.gov.educ.api.edx.struct.v1.SecureExchangeContactTypeCode;
import ca.bc.gov.educ.api.edx.struct.v1.SecureExchangeCreate;
import ca.bc.gov.educ.api.edx.struct.v1.SecureExchangeStatusCode;
import ca.bc.gov.educ.api.edx.struct.v1.*;
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.info.Info;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
Expand Down Expand Up @@ -108,17 +105,11 @@ public interface SecureExchangeEndpoint {
@ApiResponses(value = {@ApiResponse(responseCode = "204", description = "NO CONTENT"), @ApiResponse(responseCode = "404", description = "NOT FOUND."), @ApiResponse(responseCode = "500", description = "INTERNAL SERVER ERROR.")})
ResponseEntity<Void> deleteById(@PathVariable UUID id);

/**
* Claim all secure exchanges provided
*
* @param secureExchangeIDs the secure exchange IDs
* @return the response entity
*/
@PostMapping(URL.CLAIM_ALL)
@PreAuthorize("hasAuthority('SCOPE_WRITE_SECURE_EXCHANGE')")
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK"), @ApiResponse(responseCode = "500", description = "INTERNAL SERVER ERROR.")})
@Tag(name = "Endpoint to claim all secure exchanges provided by ID.", description = "Endpoint to claim all secure exchanges provided by ID.")
ResponseEntity<List<SecureExchange>> claimAllSecureExchanges(@RequestParam List<UUID> secureExchangeIDs, @RequestParam String reviewer);
ResponseEntity<List<SecureExchange>> claimAllSecureExchanges(@Validated @RequestBody SecureExchangeClaimRequest claimRequest);

/**
* Find all completable future.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
import ca.bc.gov.educ.api.edx.model.v1.SecureExchangeContactTypeCodeEntity;
import ca.bc.gov.educ.api.edx.model.v1.SecureExchangeEntity;
import ca.bc.gov.educ.api.edx.model.v1.SecureExchangeStatusCodeEntity;
import ca.bc.gov.educ.api.edx.props.ApplicationProperties;
import ca.bc.gov.educ.api.edx.repository.*;
import ca.bc.gov.educ.api.edx.struct.v1.SecureExchange;
import ca.bc.gov.educ.api.edx.struct.v1.SecureExchangeClaimRequest;
import ca.bc.gov.educ.api.edx.utils.TransformUtil;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -25,9 +27,8 @@
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
Expand Down Expand Up @@ -110,23 +111,25 @@ public List<SecureExchangeEntity> findSecureExchange(final String contactIdentif
}

@Transactional
public void claimAllSecureExchanges(final List<UUID> secureExchangeIds, String reviewer) {
for(final UUID secureExchangeId : secureExchangeIds) {
final Optional<SecureExchangeEntity> curSecureExchange = this.getSecureExchangeRequestRepository().findById(secureExchangeId);
public List<SecureExchangeEntity> claimAllSecureExchanges(final SecureExchangeClaimRequest secureExchangeClaimRequest) {
List<SecureExchangeEntity> savedEntities = new ArrayList<>();
for(final String secureExchangeId : secureExchangeClaimRequest.getSecureExchangeIDs()) {
final Optional<SecureExchangeEntity> curSecureExchange = this.getSecureExchangeRequestRepository().findById(UUID.fromString(secureExchangeId));
if (curSecureExchange.isPresent()) {
final SecureExchangeEntity secureExchange = curSecureExchange.get();
if(StringUtils.isNotEmpty(reviewer)) {
secureExchange.setReviewer(reviewer);
secureExchange.setUpdateUser(reviewer);
if(StringUtils.isNotEmpty(secureExchangeClaimRequest.getReviewer())) {
secureExchange.setReviewer(secureExchangeClaimRequest.getReviewer());
secureExchange.setUpdateUser(secureExchangeClaimRequest.getReviewer());
}else{
secureExchange.setReviewer(null);
secureExchange.setUpdateUser(ApplicationProperties.CLIENT_ID);
secureExchange.setUpdateUser(secureExchangeClaimRequest.getUpdateUser());
}
secureExchange.setUpdateDate(LocalDateTime.now());

this.secureExchangeRequestRepository.save(secureExchange);
savedEntities.add(this.secureExchangeRequestRepository.save(secureExchange));
}
}
return savedEntities;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package ca.bc.gov.educ.api.edx.struct.v1;

import ca.bc.gov.educ.api.edx.struct.BaseRequest;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.Serializable;
import java.util.List;

@EqualsAndHashCode(callSuper = true)
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class SecureExchangeClaimRequest extends BaseRequest implements Serializable {
private static final long serialVersionUID = 583620260139143932L;

private List<String> secureExchangeIDs;

private String reviewer;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import ca.bc.gov.educ.api.edx.BaseEdxAPITest;
import ca.bc.gov.educ.api.edx.struct.v1.SecureExchange;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;

public abstract class BaseEdxControllerTest extends BaseEdxAPITest {

Expand Down Expand Up @@ -59,13 +61,21 @@ protected String dummySecureExchangeJsonWithMinAndDocument(final String ministry

protected SecureExchange getSecureExchangeEntityFromJsonString() {
try {
System.out.println(this.dummySecureExchangeJson());
return new ObjectMapper().readValue(this.dummySecureExchangeJson(), SecureExchange.class);
} catch (final Exception e) {
throw new RuntimeException(e);
}
}

public static String asJsonString(final Object obj) {
try {
ObjectMapper om = new ObjectMapper();
om.registerModule(new JavaTimeModule()).configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
return om.writeValueAsString(obj);
} catch (final Exception e) {
throw new RuntimeException(e);
}
}


protected SecureExchange getSecureExchangeEntityFromJsonStringNoCreateUpdateDate() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import ca.bc.gov.educ.api.edx.repository.*;
import ca.bc.gov.educ.api.edx.struct.v1.SearchCriteria;
import ca.bc.gov.educ.api.edx.struct.v1.SecureExchange;
import ca.bc.gov.educ.api.edx.struct.v1.SecureExchangeClaimRequest;
import ca.bc.gov.educ.api.edx.struct.v1.ValueType;
import ca.bc.gov.educ.api.edx.support.DocumentBuilder;
import ca.bc.gov.educ.api.edx.support.DocumentTypeCodeBuilder;
Expand Down Expand Up @@ -189,26 +190,57 @@ void testCreateSecureExchange_LowercaseEmailVerifiedFlag_ShouldReturnStatusBadRe
}

@Test
void testClaimSecureExchange_ShouldReturnStatusNoContent() throws Exception {
void testClaimSecureExchange_ShouldReturnStatusOk() throws Exception {
final SecureExchangeEntity entity = this.repository.save(mapper.toModel(this.getSecureExchangeEntityFromJsonString()));
List<String> exchangeList = new ArrayList<>();
exchangeList.add(entity.getSecureExchangeID().toString());
SecureExchangeClaimRequest request = new SecureExchangeClaimRequest();
request.setSecureExchangeIDs(exchangeList);
request.setReviewer("TESTMIN");

this.mockMvc.perform(post(URL.BASE_URL_SECURE_EXCHANGE + URL.CLAIM_ALL).param(
"secureExchangeIDs", new String[]{String.valueOf(entity.getSecureExchangeID())}).param("reviewer", "TESTMIN")
.with(jwt().jwt((jwt) -> jwt.claim("scope", "WRITE_SECURE_EXCHANGE")))
.contentType(APPLICATION_JSON)
.accept(APPLICATION_JSON)).andDo(print()).andExpect(status().isNoContent());
this.mockMvc.perform(post(URL.BASE_URL_SECURE_EXCHANGE + URL.CLAIM_ALL)
.with(jwt().jwt((jwt) -> jwt.claim("scope", "WRITE_SECURE_EXCHANGE")))
.contentType(APPLICATION_JSON)
.accept(APPLICATION_JSON)
.content(asJsonString(request)))
.andDo(print()).andExpect(status().isOk()).andExpect(jsonPath("$", hasSize(1)));;
}

@Test
void testClaimSecureExchanges_ShouldReturnStatusNoContent() throws Exception {
void testClaimSecureExchanges_ShouldReturnStatusOk() throws Exception {
final SecureExchangeEntity entity = this.repository.save(mapper.toModel(this.getSecureExchangeEntityFromJsonString()));
final SecureExchangeEntity entity2 = this.repository.save(mapper.toModel(this.getSecureExchangeEntityFromJsonString()));
List<String> exchangeList = new ArrayList<>();
exchangeList.add(entity.getSecureExchangeID().toString());
exchangeList.add(entity2.getSecureExchangeID().toString());
SecureExchangeClaimRequest request = new SecureExchangeClaimRequest();
request.setSecureExchangeIDs(exchangeList);
request.setReviewer("TESTMIN");

this.mockMvc.perform(post(URL.BASE_URL_SECURE_EXCHANGE + URL.CLAIM_ALL)
.with(jwt().jwt((jwt) -> jwt.claim("scope", "WRITE_SECURE_EXCHANGE")))
.contentType(APPLICATION_JSON)
.accept(APPLICATION_JSON)
.content(asJsonString(request)))
.andDo(print()).andExpect(status().isOk()).andExpect(jsonPath("$", hasSize(2)));;
}

this.mockMvc.perform(post(URL.BASE_URL_SECURE_EXCHANGE + URL.CLAIM_ALL).param(
"secureExchangeIDs", new String[]{String.valueOf(entity.getSecureExchangeID()), String.valueOf(entity2.getSecureExchangeID())}).param("reviewer", "TESTMIN")
.with(jwt().jwt((jwt) -> jwt.claim("scope", "WRITE_SECURE_EXCHANGE")))
.contentType(APPLICATION_JSON)
.accept(APPLICATION_JSON)).andDo(print()).andExpect(status().isNoContent());
@Test
void testClaimSecureExchanges_ShouldReturnStatusBadRequest() throws Exception {
final SecureExchangeEntity entity = this.repository.save(mapper.toModel(this.getSecureExchangeEntityFromJsonString()));
List<String> exchangeList = new ArrayList<>();
exchangeList.add(entity.getSecureExchangeID().toString());
exchangeList.add("ABCD");
SecureExchangeClaimRequest request = new SecureExchangeClaimRequest();
request.setSecureExchangeIDs(exchangeList);
request.setReviewer("TESTMIN");

this.mockMvc.perform(post(URL.BASE_URL_SECURE_EXCHANGE + URL.CLAIM_ALL)
.with(jwt().jwt((jwt) -> jwt.claim("scope", "WRITE_SECURE_EXCHANGE")))
.contentType(APPLICATION_JSON)
.accept(APPLICATION_JSON)
.content(asJsonString(request)))
.andDo(print()).andExpect(status().isBadRequest());;
}

@Test
Expand Down

0 comments on commit 5545a6a

Please sign in to comment.