Skip to content

Commit

Permalink
Testing and fixing GetPendingTerminations
Browse files Browse the repository at this point in the history
  • Loading branch information
HusseinYasser committed May 16, 2024
1 parent 53b435a commit 4ff78ef
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,30 @@

import com.workup.contracts.logger.ContractsLogger;
import com.workup.contracts.logger.LoggingLevel;
import com.workup.contracts.models.Contract;
import com.workup.shared.commands.contracts.TerminationRequest;
import com.workup.shared.commands.contracts.requests.GetPendingTerminationsRequest;
import com.workup.shared.commands.contracts.responses.GetPendingTerminationsResponse;
import com.workup.shared.enums.HttpStatusCode;
import com.workup.shared.enums.contracts.TerminationRequestStatus;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

public class GetPendingTerminationsCommand
extends ContractCommand<GetPendingTerminationsRequest, GetPendingTerminationsResponse> {

@Override
public GetPendingTerminationsResponse Run(GetPendingTerminationsRequest request) {

Optional<Contract> contract =
contractRepository.findById(UUID.fromString(request.getContractId()));
if (contract.isEmpty())
return GetPendingTerminationsResponse.builder()
.withStatusCode(HttpStatusCode.BAD_REQUEST)
.withErrorMessage("Invalid Contract Id")
.build();

try {
@SuppressWarnings("unchecked")
List<TerminationRequest> terminationsList =
Expand All @@ -30,14 +41,9 @@ public GetPendingTerminationsResponse Run(GetPendingTerminationsRequest request)
.withRequestId(String.valueOf(req.getRequestId()))
.withContractId(req.getContractId())
.withReason(req.getReason())
.build());

if (terminationsList.isEmpty()) {
return GetPendingTerminationsResponse.builder()
.withStatusCode(HttpStatusCode.NOT_FOUND)
.withErrorMessage("No pending terminations exist")
.build();
}
.withStatus(req.getStatus())
.build())
.toList();

return GetPendingTerminationsResponse.builder()
.withTerminationRequests(terminationsList)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ static void datasourceProperties(DynamicPropertyRegistry registry) {
@Autowired GetMilestoneTests getMilestoneTests;
@Autowired InitiateContractTests initiateContractTests;
@Autowired ViewContractMilestonesTests viewContractMilestonesTests;
@Autowired GetPendingTerminationsTests getPendingTerminationsTests;

@BeforeEach
void clearAll() {
Expand Down Expand Up @@ -199,4 +200,14 @@ void InitiateContractTest() {
void ViewContractMilestonesTest() {
viewContractMilestonesTests.successTest(template);
}

@Test
void GetPendingTerminationsTest1() {
getPendingTerminationsTests.invalidContract(template);
}

@Test
void GetPendingTerminationsTest2() {
getPendingTerminationsTests.successTest(template);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package com.workup.contracts;

import static org.junit.jupiter.api.Assertions.*;

import com.workup.contracts.models.TerminationRequest;
import com.workup.contracts.repositories.TerminationRequestRepository;
import com.workup.shared.commands.contracts.requests.GetPendingTerminationsRequest;
import com.workup.shared.commands.contracts.requests.InitiateContractRequest;
import com.workup.shared.commands.contracts.responses.GetPendingTerminationsResponse;
import com.workup.shared.commands.contracts.responses.InitiateContractResponse;
import com.workup.shared.enums.HttpStatusCode;
import com.workup.shared.enums.ServiceQueueNames;
import com.workup.shared.enums.contracts.TerminationRequestStatus;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.UUID;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class GetPendingTerminationsTests {

@Autowired TerminationRequestRepository terminationRequestRepository;

public void invalidContract(AmqpTemplate template) {
GetPendingTerminationsRequest request =
GetPendingTerminationsRequest.builder()
.withContractId(UUID.randomUUID().toString())
.build();

GetPendingTerminationsResponse response =
(GetPendingTerminationsResponse)
template.convertSendAndReceive(ServiceQueueNames.CONTRACTS, request);
assertNotNull(response);
assertEquals(HttpStatusCode.BAD_REQUEST, response.getStatusCode());
}

public void successTest(AmqpTemplate template) {
// create contract
String clientId = UUID.randomUUID().toString(), freelancerId = UUID.randomUUID().toString();
InitiateContractRequest initiateContractRequest =
InitiateContractRequest.builder()
.withClientId(clientId)
.withFreelancerId(freelancerId)
.withJobId("789")
.withProposalId("bruh")
.withJobTitle("very happy guc worker :)")
.withJobMilestones(new ArrayList<>())
.build();

InitiateContractResponse contractResponse =
(InitiateContractResponse)
template.convertSendAndReceive(ServiceQueueNames.CONTRACTS, initiateContractRequest);

assertNotNull(contractResponse);
assertEquals(HttpStatusCode.CREATED, contractResponse.getStatusCode());

String request1 = UUID.randomUUID().toString(), request2 = UUID.randomUUID().toString();
TerminationRequest clientTerminationRequest =
TerminationRequest.builder()
.withRequestId(UUID.fromString(request1))
.withReason("m4 3agebny el 4o8l da1")
.withContractId(contractResponse.getContractId())
.withRequesterId(clientId)
.withStatus(TerminationRequestStatus.PENDING)
.build();

TerminationRequest freelancerTerminationRequest =
TerminationRequest.builder()
.withRequestId(UUID.fromString(request2))
.withReason("m4 3agebny el 4o8l da2")
.withContractId(contractResponse.getContractId())
.withRequesterId(freelancerId)
.withStatus(TerminationRequestStatus.PENDING)
.build();

HashMap<String, TerminationRequest> expectedTerminationRequests = new HashMap<>();
expectedTerminationRequests.put(freelancerId, freelancerTerminationRequest);
expectedTerminationRequests.put(clientId, clientTerminationRequest);

try {
terminationRequestRepository.save(freelancerTerminationRequest);
terminationRequestRepository.save(clientTerminationRequest);

// Now Get the pending requests :D
GetPendingTerminationsRequest request =
GetPendingTerminationsRequest.builder()
.withContractId(contractResponse.getContractId())
.build();

GetPendingTerminationsResponse response =
(GetPendingTerminationsResponse)
template.convertSendAndReceive(ServiceQueueNames.CONTRACTS, request);

assertNotNull(response);
assertEquals(HttpStatusCode.OK, response.getStatusCode());

response
.getTerminationRequests()
.forEach(
req -> {
TerminationRequest correspondent =
expectedTerminationRequests.get(req.getRequesterId());
assertEquals(correspondent.getStatus(), req.getStatus());
assertEquals(correspondent.getContractId(), req.getContractId());
assertEquals(correspondent.getReason(), req.getReason());
assertEquals(correspondent.getRequestId().toString(), req.getRequestId());
assertEquals(correspondent.getRequesterId(), req.getRequesterId());
});
} catch (Exception e) {
fail("Error has occurred in the GetPending TerminationRequests Tests");
}
}
}

0 comments on commit 4ff78ef

Please sign in to comment.