Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.upkeep.infrastructure.adapter.in.rest.common.exception;

import com.upkeep.domain.exception.AlreadyMemberException;
import com.upkeep.domain.exception.BudgetAlreadyExistsException;
import com.upkeep.domain.exception.CompanyNotFoundException;
import com.upkeep.domain.exception.CompanySlugAlreadyExistsException;
import com.upkeep.domain.exception.CustomerAlreadyExistsException;
Expand Down Expand Up @@ -151,6 +152,13 @@ private Response handleDomainException(DomainException exception, String traceId
))
.build();

case BudgetAlreadyExistsException e -> Response
.status(409)
.entity(ApiResponse.error(
ApiError.of("BUDGET_ALREADY_EXISTS", e.getMessage(), traceId)
))
.build();

default -> Response
.status(422)
.entity(ApiResponse.error(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.upkeep.application.port.out.audit.AuditEventRepository;
import com.upkeep.application.port.out.budget.BudgetRepository;
import com.upkeep.application.port.out.membership.MembershipRepository;
import com.upkeep.domain.exception.BudgetAlreadyExistsException;
import com.upkeep.domain.exception.MembershipNotFoundException;
import com.upkeep.domain.exception.UnauthorizedOperationException;
import com.upkeep.domain.model.audit.AuditEvent;
Expand All @@ -19,6 +20,7 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.time.Instant;
import java.util.Optional;
import java.util.UUID;

Expand Down Expand Up @@ -137,6 +139,33 @@ void shouldSetBudgetWithDifferentCurrencies() {
assertEquals(100000L, result.amountCents());
}

@Test
@DisplayName("should throw exception when budget already exists for current month")
void shouldThrowExceptionWhenBudgetAlreadyExists() {
String companyId = UUID.randomUUID().toString();
String userId = UUID.randomUUID().toString();

Membership ownerMembership = createOwnerMembership(userId, companyId);
when(membershipRepository.findByCustomerIdAndCompanyId(
any(CustomerId.class),
any(CompanyId.class)
)).thenReturn(Optional.of(ownerMembership));

// Mock that a budget already exists for the current month
Budget existingBudget = mock(Budget.class);
when(budgetRepository.findByCompanyIdAndEffectiveFrom(
any(CompanyId.class),
any(Instant.class)
)).thenReturn(Optional.of(existingBudget));

SetBudgetCommand command = new SetBudgetCommand(companyId, userId, 50000L, Currency.EUR);

assertThrows(BudgetAlreadyExistsException.class, () -> useCase.execute(command));

verify(budgetRepository, never()).save(any(Budget.class));
verify(auditEventRepository, never()).save(any(AuditEvent.class));
}

private Membership createOwnerMembership(String userId, String companyId) {
return Membership.create(
CustomerId.from(userId),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,4 +313,41 @@ void shouldSetBudgetWithDifferentCurrencies() {
.body("data.currency", equalTo(currency));
}
}

@Test
@DisplayName("should reject duplicate budget for same month")
void shouldRejectDuplicateBudgetForSameMonth() {
String uniqueId = UUID.randomUUID().toString().substring(0, 8);
String email = "duplicate-budget-" + uniqueId + "@example.com";
String token = createUserAndGetToken(email);
String companyId = createCompany(token, "Duplicate Budget Company", "duplicate-budget-" + uniqueId);

String requestBody = """
{
"amountCents": 50000,
"currency": "EUR"
}
""";

// First budget should succeed
given()
.contentType(ContentType.JSON)
.cookie("access_token", token)
.body(requestBody)
.when()
.post("/api/companies/" + companyId + "/budget")
.then()
.statusCode(201);

// Second budget for the same month should fail
given()
.contentType(ContentType.JSON)
.cookie("access_token", token)
.body(requestBody)
.when()
.post("/api/companies/" + companyId + "/budget")
.then()
.statusCode(409)
.body("error.code", equalTo("BUDGET_ALREADY_EXISTS"));
}
}
Loading