Skip to content

Commit

Permalink
added the remaining testing of all commands
Browse files Browse the repository at this point in the history
  • Loading branch information
HusseinYasser committed May 16, 2024
1 parent fe39ab4 commit 060cc2e
Show file tree
Hide file tree
Showing 7 changed files with 267 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,24 +1,8 @@
package com.workup.contracts;

import com.workup.contracts.commands.*;
import com.workup.shared.commands.contracts.requests.ContractTerminationRequest;
import com.workup.shared.commands.contracts.requests.EvaluateMilestoneRequest;
import com.workup.shared.commands.contracts.requests.GetContractRequest;
import com.workup.shared.commands.contracts.requests.GetPendingTerminationsRequest;
import com.workup.shared.commands.contracts.requests.HandleTerminationRequest;
import com.workup.shared.commands.contracts.requests.InitiateContractRequest;
import com.workup.shared.commands.contracts.requests.MarkPaymentCompletedRequest;
import com.workup.shared.commands.contracts.requests.ProgressMilestoneRequest;
import com.workup.shared.commands.contracts.requests.ViewContractMilestonesRequest;
import com.workup.shared.commands.contracts.responses.ContractTerminationResponse;
import com.workup.shared.commands.contracts.responses.EvaluateMilestoneResponse;
import com.workup.shared.commands.contracts.responses.GetContractResponse;
import com.workup.shared.commands.contracts.responses.GetPendingTerminationsResponse;
import com.workup.shared.commands.contracts.responses.HandleTerminationResponse;
import com.workup.shared.commands.contracts.responses.InitiateContractResponse;
import com.workup.shared.commands.contracts.responses.MarkPaymentCompletedResponse;
import com.workup.shared.commands.contracts.responses.ProgressMilestoneResponse;
import com.workup.shared.commands.contracts.responses.ViewContractMilestonesResponse;
import com.workup.shared.commands.contracts.requests.*;
import com.workup.shared.commands.contracts.responses.*;
import com.workup.shared.enums.ServiceQueueNames;
import java.util.concurrent.CompletableFuture;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
Expand Down Expand Up @@ -106,6 +90,13 @@ public CompletableFuture<GetPendingTerminationsResponse> receive(GetPendingTermi
return CompletableFuture.completedFuture(
((GetPendingTerminationsCommand) commandMap.getCommand("GetPendingTerminations")).Run(in));
}

@RabbitHandler
@Async
public CompletableFuture<GetMilestoneResponse> receive(GetMilestoneRequest in) throws Exception {
return CompletableFuture.completedFuture(
((GetMilestoneCommand) commandMap.getCommand("GetMilestone")).Run(in));
}
// NEW_COMMAND_BOILERPLATE

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public void registerCommands() {
commands.put("EvaluateMilestone", EvaluateMilestoneCommand.class);
commands.put("ProgressMilestone", ProgressMilestoneCommand.class);
commands.put("GetPendingTerminations", GetPendingTerminationsCommand.class);
commands.put("GetMilestone", GetMilestoneCommand.class);
// NEW_COMMAND_BOILERPLATE
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ static void datasourceProperties(DynamicPropertyRegistry registry) {
@Autowired MarkMilestoneAsPaidTests markMilestoneAsPaidTests;
@Autowired EvaluateMilestoneTests evaluateMilestoneTests;
@Autowired GetContractTests getContractTests;
@Autowired GetMilestoneTests getMilestoneTests;
@Autowired InitiateContractTests initiateContractTests;
@Autowired ViewContractMilestonesTests viewContractMilestonesTests;

@BeforeEach
void clearAll() {
Expand Down Expand Up @@ -196,4 +199,32 @@ void GetContractTest2() {
void ContractsLoggerTest1() {
ContractsLogger.print("Contracts Logger Test 1 Message", LoggingLevel.INFO);
}

@Test
void GetMilestoneTest1() {
getMilestoneTests.milestoneNotFoundTest(template);
}

@Test
void GetMilestoneTest2() {
try {
getMilestoneTests.successTest(template);
} catch (Exception e) {
ContractsLogger.print("Error occurred while applying GetMilestoneTest2", LoggingLevel.ERROR);
}
}

@Test
void InitiateContractTest() {
try {
initiateContractTests.successTest(template);
} catch (Exception e) {
ContractsLogger.print("Error Occurred in Initating COntract Test", LoggingLevel.ERROR);
}
}

@Test
void ViewContractMilestonesTest() {
viewContractMilestonesTests.successTest(template);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

import com.workup.contracts.models.Contract;
import com.workup.contracts.repositories.ContractRepository;
import com.workup.shared.commands.contracts.Milestone;
import com.workup.shared.commands.contracts.requests.GetContractRequest;
Expand All @@ -14,6 +15,7 @@
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -68,5 +70,17 @@ public void successTest(AmqpTemplate template) throws ParseException {
(GetContractResponse) template.convertSendAndReceive(ServiceQueueNames.CONTRACTS, request);
assertNotNull(response);
assertEquals(HttpStatusCode.OK, response.getStatusCode());

Optional<Contract> contractOptional =
contractRepository.findById(UUID.fromString(response.getContractId()));
if (contractOptional.isEmpty()) fail();

Contract contract = contractOptional.get();

assertEquals("789", contract.getJobId());
assertEquals("bruh", contract.getProposalId());
assertEquals("very happy guc worker :)", contract.getJobTitle());
assertEquals(clientId, contract.getClientId());
assertEquals(freelancerId, contract.getFreelancerId());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.workup.contracts;

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

import com.workup.contracts.models.ContractMilestone;
import com.workup.contracts.repositories.ContractMilestoneRepository;
import com.workup.shared.commands.contracts.requests.GetMilestoneRequest;
import com.workup.shared.commands.contracts.responses.GetMilestoneResponse;
import com.workup.shared.enums.HttpStatusCode;
import com.workup.shared.enums.ServiceQueueNames;
import java.text.ParseException;
import java.text.SimpleDateFormat;
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 GetMilestoneTests {

@Autowired ContractMilestoneRepository contractMilestoneRepository;

public void milestoneNotFoundTest(AmqpTemplate template) {
String milestoneId = UUID.randomUUID().toString();
GetMilestoneRequest request =
GetMilestoneRequest.builder().withMilestoneId(milestoneId).build();

GetMilestoneResponse response =
(GetMilestoneResponse) template.convertSendAndReceive(ServiceQueueNames.CONTRACTS, request);
System.out.println(response);
assertNotNull(response);
assertEquals(HttpStatusCode.NOT_FOUND, response.getStatusCode());
}

public void successTest(AmqpTemplate template) throws ParseException {

String milestoneId = UUID.randomUUID().toString();
ContractMilestone milestone =
ContractMilestone.builder()
.withMilestoneId(UUID.fromString(milestoneId))
.withDescription("make sure the students hate your admin system")
.withDueDate(new SimpleDateFormat("yyyy-MM-dd").parse("2025-01-01"))
.withAmount(30000)
.build();

try {
contractMilestoneRepository.save(milestone);

GetMilestoneRequest request =
GetMilestoneRequest.builder().withMilestoneId(milestoneId).build();

GetMilestoneResponse response =
(GetMilestoneResponse)
template.convertSendAndReceive(ServiceQueueNames.CONTRACTS, request);
assertNotNull(response);
assertEquals(HttpStatusCode.OK, response.getStatusCode());
} catch (Exception e) {
fail();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.workup.contracts;

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

import com.workup.contracts.models.Contract;
import com.workup.contracts.models.ContractMilestone;
import com.workup.contracts.repositories.ContractMilestoneRepository;
import com.workup.contracts.repositories.ContractRepository;
import com.workup.shared.commands.contracts.Milestone;
import com.workup.shared.commands.contracts.requests.InitiateContractRequest;
import com.workup.shared.commands.contracts.responses.InitiateContractResponse;
import com.workup.shared.enums.HttpStatusCode;
import com.workup.shared.enums.ServiceQueueNames;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
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 InitiateContractTests {

@Autowired ContractRepository contractRepository;

@Autowired ContractMilestoneRepository contractMilestoneRepository;

public void successTest(AmqpTemplate template) throws ParseException {
Milestone milestone =
Milestone.builder()
.withDescription("make sure the students hate your admin system")
.withDueDate(new SimpleDateFormat("yyyy-MM-dd").parse("2025-01-01"))
.withAmount(30000)
.build();

List<Milestone> milestones = new ArrayList<>();
milestones.add(milestone);

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(milestones)
.build();
InitiateContractResponse response =
(InitiateContractResponse)
template.convertSendAndReceive(ServiceQueueNames.CONTRACTS, initiateContractRequest);

assertNotNull(response);

// Now make sure that this contract is exactly the one submitted
assertEquals(HttpStatusCode.CREATED, response.getStatusCode());

Optional<Contract> contractOptional =
contractRepository.findById(UUID.fromString(response.getContractId()));
if (contractOptional.isEmpty()) fail();

Contract contract = contractOptional.get();

assertEquals("789", contract.getJobId());
assertEquals("bruh", contract.getProposalId());
assertEquals("very happy guc worker :)", contract.getJobTitle());
assertEquals(clientId, contract.getClientId());
assertEquals(freelancerId, contract.getFreelancerId());

// Now check over the milestone that it was inserted correctly with the contract also
List<String> milestoneIds = contract.getMilestonesIds();

Optional<ContractMilestone> optionalContractMilestone =
contractMilestoneRepository.findById(UUID.fromString(milestoneIds.getFirst()));
if (optionalContractMilestone.isEmpty())
fail("Milestones weren't added with the contract correctly");

ContractMilestone addedMilestone = optionalContractMilestone.get();
assertEquals("make sure the students hate your admin system", addedMilestone.getDescription());
assertEquals(
new SimpleDateFormat("yyyy-MM-dd").parse("2025-01-01"), addedMilestone.getDueDate());
assertEquals(30000, addedMilestone.getAmount());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.workup.contracts;

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

import com.workup.contracts.models.ContractMilestone;
import com.workup.contracts.repositories.ContractMilestoneRepository;
import com.workup.shared.commands.contracts.Milestone;
import com.workup.shared.commands.contracts.requests.ViewContractMilestonesRequest;
import com.workup.shared.commands.contracts.responses.ViewContractMilestonesResponse;
import com.workup.shared.enums.ServiceQueueNames;
import java.util.*;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ViewContractMilestonesTests {
@Autowired ContractMilestoneRepository contractMilestoneRepository;

public void successTest(AmqpTemplate template) {
String contractId = UUID.randomUUID().toString();
Random rand = new Random();
// create some milestones to be added to the db with same contractId
ArrayList<ContractMilestone> milestones = new ArrayList<>();
for (int i = 0; i < 10; ++i)
milestones.add(
ContractMilestone.builder()
.withDueDate(new Date())
.withAmount(rand.nextInt())
.withContractId(contractId)
.withDescription("AISFHAS SAGASV")
.withMilestoneId(UUID.randomUUID())
.build());

contractMilestoneRepository.saveAll(milestones);

ViewContractMilestonesRequest request =
ViewContractMilestonesRequest.builder().withContractId(contractId).build();

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

assertNotNull(response);

HashMap<String, ContractMilestone> mapping = new HashMap<>();
milestones.forEach(m -> mapping.put(m.getMilestoneId().toString(), m));

for (int i = 0; i < response.getContractMilestones().size(); ++i) {
Milestone currMilestone = response.getContractMilestones().get(i);
assertEquals(
mapping.get(currMilestone.getMilestoneId()).getAmount(), currMilestone.getAmount());
assertEquals(
mapping.get(currMilestone.getMilestoneId()).getContractId(),
currMilestone.getContractId());
assertEquals(
mapping.get(currMilestone.getMilestoneId()).getDescription(),
currMilestone.getDescription());
assertEquals(
mapping.get(currMilestone.getMilestoneId()).getDueDate(), currMilestone.getDueDate());
}
}
}

0 comments on commit 060cc2e

Please sign in to comment.