Skip to content
Snippets Groups Projects
Commit efea21a0 authored by Filip Kollár's avatar Filip Kollár
Browse files

fixing tests for usage of JPA

parent 9da70bf0
No related branches found
No related tags found
1 merge request!26Resolve "Currency service perzistencia"
Showing
with 86 additions and 22 deletions
...@@ -91,7 +91,13 @@ ...@@ -91,7 +91,13 @@
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId> <artifactId>spring-boot-starter-data-jpa</artifactId>
<version>3.2.4</version> <version>${spring.boot.version}</version>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<version>1.19.6</version>
<scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>
......
package cz.muni.fi.obs.data.dbo; package cz.muni.fi.obs.data.dbo;
import jakarta.persistence.Column; import jakarta.persistence.*;
import jakarta.persistence.Entity;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import lombok.Getter; import lombok.Getter;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import lombok.Setter; import lombok.Setter;
import java.util.HashSet;
import java.util.Set; import java.util.Set;
@Getter @Getter
...@@ -28,9 +26,9 @@ public class Currency extends Dbo { ...@@ -28,9 +26,9 @@ public class Currency extends Dbo {
@Column(nullable = false) @Column(nullable = false)
private String name; private String name;
@OneToMany(mappedBy = "from") @OneToMany(mappedBy = "from", cascade = {CascadeType.REMOVE}, orphanRemoval = true)
private Set<ExchangeRate> exchangeRatesFrom; private Set<ExchangeRate> exchangeRatesFrom = new HashSet<>();
@OneToMany(mappedBy = "to") @OneToMany(mappedBy = "to", cascade = {CascadeType.REMOVE}, orphanRemoval = true)
private Set<ExchangeRate> exchangeRatesTo; private Set<ExchangeRate> exchangeRatesTo = new HashSet<>();
} }
...@@ -4,15 +4,15 @@ import jakarta.persistence.Column; ...@@ -4,15 +4,15 @@ import jakarta.persistence.Column;
import jakarta.persistence.Entity; import jakarta.persistence.Entity;
import jakarta.persistence.ManyToOne; import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table; import jakarta.persistence.Table;
import lombok.AllArgsConstructor; import lombok.*;
import lombok.Getter;
import lombok.Setter;
import java.time.Instant; import java.time.Instant;
@Getter @Getter
@Setter @Setter
@Entity @Entity
@Builder
@NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
@Table(name = "cs_exchange_rate") @Table(name = "cs_exchange_rate")
public class ExchangeRate extends Dbo { public class ExchangeRate extends Dbo {
......
...@@ -45,7 +45,7 @@ public class RepositoryDataProvider { ...@@ -45,7 +45,7 @@ public class RepositoryDataProvider {
.validUntil(now.minusSeconds(120)) // Expired .validUntil(now.minusSeconds(120)) // Expired
.build(); .build();
usd.getExchangeRates().addAll(Set.of(expiredExchangeRate, currentExchangeRate, expiredExchangeRate1)); usd.getExchangeRatesFrom().addAll(Set.of(expiredExchangeRate, currentExchangeRate, expiredExchangeRate1));
return usd; return usd;
} }
...@@ -86,7 +86,7 @@ public class RepositoryDataProvider { ...@@ -86,7 +86,7 @@ public class RepositoryDataProvider {
.validUntil(now.minusSeconds(120)) // Expired .validUntil(now.minusSeconds(120)) // Expired
.build(); .build();
euro.getExchangeRates().addAll(Set.of(expiredExchangeRate, expiredExchangeRate2, currentExchangeRate, expiredExchangeRate3)); euro.getExchangeRatesFrom().addAll(Set.of(expiredExchangeRate, expiredExchangeRate2, currentExchangeRate, expiredExchangeRate3));
return euro; return euro;
} }
...@@ -119,7 +119,7 @@ public class RepositoryDataProvider { ...@@ -119,7 +119,7 @@ public class RepositoryDataProvider {
.validUntil(now.minusSeconds(120)) // Expired .validUntil(now.minusSeconds(120)) // Expired
.build(); .build();
yuan.getExchangeRates().addAll(Set.of(expiredExchangeRate, expiredExchangeRate2, expiredExchangeRate3)); yuan.getExchangeRatesFrom().addAll(Set.of(expiredExchangeRate, expiredExchangeRate2, expiredExchangeRate3));
return yuan; return yuan;
} }
......
...@@ -69,8 +69,8 @@ public class NbsCurrencyUpdateService implements CurrencyUpdateService { ...@@ -69,8 +69,8 @@ public class NbsCurrencyUpdateService implements CurrencyUpdateService {
.validUntil(LocalDate.now().atStartOfDay().plusDays(1).toInstant(ZoneOffset.UTC)) .validUntil(LocalDate.now().atStartOfDay().plusDays(1).toInstant(ZoneOffset.UTC))
.build(); .build();
euro.getExchangeRates().add(exchangeRate); euro.getExchangeRatesFrom().add(exchangeRate);
currency.getExchangeRates().add(exchangeRate); currency.getExchangeRatesTo().add(exchangeRate);
currencyRepository.save(currency); currencyRepository.save(currency);
} }
currencyRepository.save(euro); currencyRepository.save(euro);
......
package cz.muni.fi.obs;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(CurrencyServiceApplication.class, args);
}
}
package cz.muni.fi.obs.facade; package cz.muni.fi.obs.facade;
import cz.muni.fi.obs.data.dbo.Currency; import cz.muni.fi.obs.data.dbo.Currency;
import cz.muni.fi.obs.data.provider.RepositoryDataProvider;
import cz.muni.fi.obs.dto.CurrencyDto; import cz.muni.fi.obs.dto.CurrencyDto;
import cz.muni.fi.obs.dto.CurrencyExchangeResult; import cz.muni.fi.obs.dto.CurrencyExchangeResult;
import cz.muni.fi.obs.service.CurrencyService; import cz.muni.fi.obs.service.CurrencyService;
...@@ -21,7 +22,7 @@ import java.util.List; ...@@ -21,7 +22,7 @@ import java.util.List;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@SpringBootTest @SpringBootTest(classes = {RepositoryDataProvider.class})
class CurrencyFacadeTest { class CurrencyFacadeTest {
@Mock @Mock
......
package cz.muni.fi.obs.repository; package cz.muni.fi.obs.repository;
import cz.muni.fi.obs.Application;
import cz.muni.fi.obs.data.dbo.Currency; import cz.muni.fi.obs.data.dbo.Currency;
import cz.muni.fi.obs.data.provider.RepositoryDataProvider;
import cz.muni.fi.obs.data.repository.CurrencyRepository; import cz.muni.fi.obs.data.repository.CurrencyRepository;
import cz.muni.fi.obs.exception.MissingObject; import cz.muni.fi.obs.exception.MissingObject;
import org.junit.ClassRule;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.testcontainers.containers.PostgreSQLContainer;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
@SpringBootTest(classes = {CurrencyRepository.class, RepositoryDataProvider.class}) @RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
@ContextConfiguration(initializers = {CurrencyRepositoryTest.Initializer.class})
class CurrencyRepositoryTest { class CurrencyRepositoryTest {
@Autowired @Autowired
private CurrencyRepository currencyRepository; private CurrencyRepository currencyRepository;
@Autowired
private List<Currency> initialData;
@ClassRule
public static PostgreSQLContainer<?> postgreSQLContainer = new PostgreSQLContainer<>("postgres:16.5")
.withDatabaseName("currency_db")
.withUsername("currency_service")
.withPassword("changemelater");
@BeforeEach
public void setUp() {
currencyRepository.saveAll(initialData);
}
@Test @Test
void findByCode_whenPresent_returnsCurrency() { void findByCode_whenPresent_returnsCurrency() {
Currency currency = currencyRepository.findByCode("usd").orElseThrow(() -> new MissingObject(Currency.class, "usd")); Currency currency = currencyRepository.findByCode("usd").orElseThrow(() -> new MissingObject(Currency.class, "usd"));
...@@ -59,4 +87,15 @@ class CurrencyRepositoryTest { ...@@ -59,4 +87,15 @@ class CurrencyRepositoryTest {
assertEquals(pageRequest, currencyPagedResult.getPageable()); assertEquals(pageRequest, currencyPagedResult.getPageable());
assertEquals(0, currencyPagedResult.getContent().size()); assertEquals(0, currencyPagedResult.getContent().size());
} }
static class Initializer
implements ApplicationContextInitializer<ConfigurableApplicationContext> {
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
TestPropertyValues.of(
"spring.datasource.url=" + postgreSQLContainer.getJdbcUrl(),
"spring.datasource.username=" + postgreSQLContainer.getUsername(),
"spring.datasource.password=" + postgreSQLContainer.getPassword()
).applyTo(configurableApplicationContext.getEnvironment());
}
}
} }
\ No newline at end of file
package cz.muni.fi.obs.service; package cz.muni.fi.obs.service;
import cz.muni.fi.obs.data.dbo.Currency; import cz.muni.fi.obs.data.dbo.Currency;
import cz.muni.fi.obs.data.provider.RepositoryDataProvider;
import cz.muni.fi.obs.data.repository.CurrencyRepository; import cz.muni.fi.obs.data.repository.CurrencyRepository;
import org.assertj.core.api.Assertions; import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
...@@ -16,7 +17,7 @@ import java.util.List; ...@@ -16,7 +17,7 @@ import java.util.List;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@SpringBootTest @SpringBootTest(classes = {RepositoryDataProvider.class})
class CurrencyServiceTest { class CurrencyServiceTest {
@Mock @Mock
......
...@@ -2,6 +2,7 @@ package cz.muni.fi.obs.service; ...@@ -2,6 +2,7 @@ package cz.muni.fi.obs.service;
import cz.muni.fi.obs.data.dbo.Currency; import cz.muni.fi.obs.data.dbo.Currency;
import cz.muni.fi.obs.data.dbo.ExchangeRate; import cz.muni.fi.obs.data.dbo.ExchangeRate;
import cz.muni.fi.obs.data.provider.RepositoryDataProvider;
import cz.muni.fi.obs.data.repository.CurrencyRepository; import cz.muni.fi.obs.data.repository.CurrencyRepository;
import cz.muni.fi.obs.data.repository.ExchangeRateRepository; import cz.muni.fi.obs.data.repository.ExchangeRateRepository;
import cz.muni.fi.obs.dto.CurrencyExchangeResult; import cz.muni.fi.obs.dto.CurrencyExchangeResult;
...@@ -21,7 +22,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; ...@@ -21,7 +22,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@SpringBootTest @SpringBootTest(classes = {RepositoryDataProvider.class})
class ExchangeRateServiceTest { class ExchangeRateServiceTest {
@Mock @Mock
......
spring:
datasource:
driver-class-name: org.testcontainers.jdbc.ContainerDatabaseDriver
jpa:
hibernate:
ddl-auto: create
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment