Skip to content

Commit

Permalink
Evluate Milestone command tests
Browse files Browse the repository at this point in the history
  • Loading branch information
HusseinYasser committed May 15, 2024
1 parent 96b45ea commit bba9713
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.workup.contracts;

import com.workup.contracts.logger.ContractsLogger;
import com.workup.contracts.repositories.ContractMilestoneRepository;
import com.workup.contracts.repositories.ContractRepository;
import com.workup.contracts.repositories.TerminationRequestRepository;
Expand Down Expand Up @@ -52,10 +53,12 @@ static void datasourceProperties(DynamicPropertyRegistry registry) {
@Autowired ContractRepository contractRepository;
@Autowired ContractMilestoneRepository contractMilestoneRepository;
@Autowired TerminationRequestRepository terminationRequestRepository;
@Autowired PaymentsMockingListener paymentsMockingListener;

@Autowired HandleContractTerminationTests handleContractTerminationTests;
@Autowired RequestContractTerminationTests requestContractTerminationTests;
@Autowired MarkMilestoneAsPaidTests markMilestoneAsPaidTests;
@Autowired EvaluateMilestoneTests evaluateMilestoneTests;

@BeforeEach
void clearAll() {
Expand Down Expand Up @@ -146,4 +149,23 @@ void MarkMilestoneAsPaidTest2() throws ParseException {
void MarkMilestoneAsPaidTest3() throws ParseException {
markMilestoneAsPaidTests.successTest(template);
}

@Test
void EvaluateMilestoneTest1() {
evaluateMilestoneTests.milestoneNotFoundTest(template);
}

@Test
void EvaluateMilestoneTest2() {
evaluateMilestoneTests.wrongMilestoneState(template);
}

@Test
void EvaluateMilestoneTest3() {
try {
evaluateMilestoneTests.successTest(template);
} catch (Exception e) {
ContractsLogger.print("Error Occurred in EvaluateMilestoneTest3");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
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.EvaluateMilestoneRequest;
import com.workup.shared.commands.contracts.requests.InitiateContractRequest;
import com.workup.shared.commands.contracts.responses.EvaluateMilestoneResponse;
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.MilestoneState;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
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 EvaluateMilestoneTests {

@Autowired ContractMilestoneRepository contractMilestoneRepository;

public void milestoneNotFoundTest(AmqpTemplate template) {

// Send the request with random milestone and it should not be found
EvaluateMilestoneRequest request =
EvaluateMilestoneRequest.builder()
.withMilestoneId(String.valueOf(UUID.randomUUID()))
.withEvaluatedState(MilestoneState.ACCEPTED)
.build();

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

assertNotNull(response);

assertEquals(HttpStatusCode.BAD_REQUEST, response.getStatusCode());
assertEquals("Milestone is not found", response.getErrorMessage());
}

public void wrongMilestoneState(AmqpTemplate template) {
// Create a Milestone and add it to the db and then check this milestone
// In progress (not ready for evaluation)
String milestoneId = UUID.randomUUID().toString();

EvaluateMilestoneRequest request =
EvaluateMilestoneRequest.builder()
.withMilestoneId(milestoneId)
.withEvaluatedState(MilestoneState.ACCEPTED)
.build();

ContractMilestone dbMilestone =
ContractMilestone.builder()
.withMilestoneId(UUID.fromString(milestoneId))
.withContractId(UUID.randomUUID().toString())
.withAmount(500)
.withDescription("Backend Boi")
.withDueDate(new Date())
.withStatus(MilestoneState.IN_PROGRESS)
.build();

contractMilestoneRepository.save(dbMilestone);

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

assertNotNull(response);
assertEquals(HttpStatusCode.BAD_REQUEST, response.getStatusCode());
assertEquals(
"Milestone cannot be evaluated as it has not progressed enough",
response.getErrorMessage());
}

// TODO: @Karim ElMosallamy
public void successTest(AmqpTemplate template) throws ParseException {
// create a milestone with it's contract, and evaluate this milestone
String milestoneId = UUID.randomUUID().toString();
Milestone milestone =
Milestone.builder()
.withMilestoneId(milestoneId)
.withDescription("make sure the students hate your admin system")
.withDueDate(new SimpleDateFormat("yyyy-MM-dd").parse("2025-01-01"))
.withAmount(30000)
.withStatus(MilestoneState.IN_REVIEW)
.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 contractResponse =
(InitiateContractResponse)
template.convertSendAndReceive(ServiceQueueNames.CONTRACTS, initiateContractRequest);

assertNotNull(contractResponse);

EvaluateMilestoneRequest request =
EvaluateMilestoneRequest.builder()
.withMilestoneId(milestoneId)
.withEvaluatedState(MilestoneState.ACCEPTED)
.build();

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

assertNotNull(response);
assertEquals(HttpStatusCode.OK, response.getStatusCode());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.workup.contracts;

import com.workup.shared.commands.payments.paymentrequest.requests.CreatePaymentRequestRequest;
import com.workup.shared.commands.payments.paymentrequest.responses.CreatePaymentRequestResponse;
import com.workup.shared.enums.HttpStatusCode;
import com.workup.shared.enums.ServiceQueueNames;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

@Service
@RabbitListener(queues = ServiceQueueNames.PAYMENTS)
public class PaymentsMockingListener {

CreatePaymentRequestResponse receive(CreatePaymentRequestRequest req) {
return CreatePaymentRequestResponse.builder().withStatusCode(HttpStatusCode.OK).build();
}
}

0 comments on commit bba9713

Please sign in to comment.