feat: ExchangeController & tests

This commit is contained in:
Piotr Dec 2024-05-25 00:33:33 +02:00
parent 720937bd6c
commit 95ed5f6ae7
Signed by: stawros
GPG key ID: F89F27AD8F881A91
8 changed files with 85 additions and 21 deletions

View file

@ -48,7 +48,7 @@ public class EntityCreator {
currencies.put("PLN", new Currency("PLN", pln));
}
if (usd > 0) {
currencies.put("USD", new Currency("USD", pln));
currencies.put("USD", new Currency("USD", usd));
}
return new User(Constants.NAME, Constants.SURNAME, Constants.PESEL, currencies);
}

View file

@ -5,6 +5,7 @@ import eu.ztsh.wymiana.RepositoryBasedTest;
import eu.ztsh.wymiana.data.repository.UserRepository;
import eu.ztsh.wymiana.exception.ExchangeFailedException;
import eu.ztsh.wymiana.exception.InsufficientFundsException;
import eu.ztsh.wymiana.exception.UserNotFoundException;
import eu.ztsh.wymiana.validation.InstanceValidator;
import eu.ztsh.wymiana.validation.ValidationFailedException;
import jakarta.transaction.Transactional;
@ -175,7 +176,7 @@ class CurrencyServiceTest extends RepositoryBasedTest {
.toSell(USD_SELL)
.build();
assertThatThrownBy(() -> currencyService.exchange(entity))
.isInstanceOf(ExchangeFailedException.class);
.isInstanceOf(UserNotFoundException.class);
}
@Test

View file

@ -4,7 +4,6 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import eu.ztsh.wymiana.EntityCreator;
import eu.ztsh.wymiana.WireMockExtension;
import eu.ztsh.wymiana.model.User;
import eu.ztsh.wymiana.util.UserMapper;
import org.junit.jupiter.api.ClassOrderer;
import org.junit.jupiter.api.DisplayName;
@ -83,7 +82,7 @@ class ApplicationIntegrationTests {
.uri(endpoint)
.bodyValue(EntityCreator.userRequest().pln(100).build())
.exchange()
.expectStatus().is2xxSuccessful();
.expectStatus().isNoContent();
}
@Test
@ -94,7 +93,7 @@ class ApplicationIntegrationTests {
.bodyValue(EntityCreator.userRequest().pesel(INVALID_PESEL).build())
.exchange()
.expectStatus()
.is4xxClientError();
.isBadRequest();
}
@Test
@ -104,7 +103,7 @@ class ApplicationIntegrationTests {
.uri(endpoint)
.bodyValue(EntityCreator.userRequest().build())
.exchange()
.expectStatus().is4xxClientError()
.expectStatus().isEqualTo(409)
.expectBody(String.class).isEqualTo("User with PESEL %s already exists".formatted(PESEL));
}
@ -114,7 +113,7 @@ class ApplicationIntegrationTests {
webTestClient.get()
.uri(endpoint.concat("/").concat(PESEL))
.exchange()
.expectStatus().is2xxSuccessful()
.expectStatus().isOk()
.expectBody().json(asJson(UserMapper.entityToPojo(EntityCreator.userEntity().pln(100).build())));
}
@ -124,7 +123,7 @@ class ApplicationIntegrationTests {
webTestClient.get()
.uri(endpoint.concat("/").concat(ANOTHER_PESEL))
.exchange()
.expectStatus().is4xxClientError();
.expectStatus().isNotFound();
}
@Test
@ -133,7 +132,7 @@ class ApplicationIntegrationTests {
webTestClient.get()
.uri(endpoint.concat("/").concat(INVALID_PESEL))
.exchange()
.expectStatus().is4xxClientError();
.expectStatus().isBadRequest();
}
}
@ -158,18 +157,19 @@ class ApplicationIntegrationTests {
.toSell(PLN)
.build())
.exchange()
.expectStatus().is5xxServerError();
.expectStatus().isEqualTo(500);
}
@Test
@DisplayName("03.2: Perform valid money exchange")
void exchangeTest() throws JsonProcessingException {
void exchangeTest() {
var date = getTodayOrLastFriday();
WireMockExtension.response(
URI_PATTERN.formatted(date),
200,
new ObjectMapper().writeValueAsString(EntityCreator.rates(date))
asJson(EntityCreator.rates(date))
);
var expected = asJson(EntityCreator.user(100 - PLN, USD_BUY));
webTestClient.post()
.uri(endpoint)
.bodyValue(EntityCreator.exchangeRequest()
@ -178,8 +178,9 @@ class ApplicationIntegrationTests {
.toSell(PLN)
.build())
.exchange()
.expectStatus().is2xxSuccessful()
.expectBody(User.class).isEqualTo(EntityCreator.user(100 - PLN, USD_BUY));
.expectStatus().isOk()
.expectBody().json(expected);
WireMockExtension.verifyGet(2, URI_PATTERN.formatted(date));
}
@Test
@ -194,8 +195,8 @@ class ApplicationIntegrationTests {
.toSell(PLN)
.build())
.exchange()
.expectStatus().is4xxClientError()
.expectBody(String.class).isEqualTo("An exchange error has occurred");
.expectStatus().isNotFound()
.expectBody(String.class).isEqualTo("User with PESEL %s not found".formatted(ANOTHER_PESEL));
}
@Test
@ -210,7 +211,7 @@ class ApplicationIntegrationTests {
.toSell(PLN)
.build())
.exchange()
.expectStatus().is4xxClientError();
.expectStatus().isBadRequest();
}
@Test
@ -219,13 +220,12 @@ class ApplicationIntegrationTests {
webTestClient.post()
.uri(endpoint)
.bodyValue(EntityCreator.exchangeRequest()
.pesel(ANOTHER_PESEL)
.from(USD_SYMBOL)
.to(PLN_SYMBOL)
.toBuy(PLN)
.build())
.exchange()
.expectStatus().is4xxClientError();
.expectStatus().isBadRequest();
}
private String getTodayOrLastFriday() {