Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ExchangeRateRecord entity #7

Merged
merged 1 commit into from
Feb 6, 2024
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
77 changes: 77 additions & 0 deletions src/main/java/com/melouk/personal/data/ExchangeRateRecord.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.melouk.personal.data;



import jakarta.persistence.CollectionTable;
import jakarta.persistence.Column;
import jakarta.persistence.ElementCollection;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.MapKeyColumn;
import jakarta.persistence.OrderColumn;
import jakarta.persistence.Table;

import java.time.ZonedDateTime;
import java.util.Map;
import java.util.Objects;

@Entity
@Table(name = "exchange_rate_record")
public final class ExchangeRateRecord {
@Id
@GeneratedValue
@Column(name = "id")
private long id;

private final ZonedDateTime timeLastUpdateUnix;
@OrderColumn(name = "source_currency")
private final String sourceCurrency;
@ElementCollection
@CollectionTable(name = "exchange_rate_mapping",
joinColumns = {@JoinColumn(name = "exchange_rate_record_id", referencedColumnName = "id")})
@MapKeyColumn(name = "target_currency")
@Column(name = "rate")
private final Map<String, Double> conversionRates;

public ExchangeRateRecord(ZonedDateTime timeLastUpdateUnix,
String sourceCurrency,
Map<String, Double> conversionRates) {
this.timeLastUpdateUnix = ZonedDateTime.from(timeLastUpdateUnix);
this.sourceCurrency = sourceCurrency;
this.conversionRates = Map.copyOf(conversionRates);
}

public long getId() {
return id;
}

public ZonedDateTime getTimeLastUpdateUnix() {
return timeLastUpdateUnix;
}

public String getSourceCurrency() {
return sourceCurrency;
}

public Map<String, Double> getConversionRates() {
return conversionRates;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ExchangeRateRecord that = (ExchangeRateRecord) o;
return id == that.id &&
Objects.equals(timeLastUpdateUnix, that.timeLastUpdateUnix) &&
Objects.equals(sourceCurrency, that.sourceCurrency) &&
Objects.equals(conversionRates, that.conversionRates);
}

@Override
public int hashCode() {
return Objects.hash(id, timeLastUpdateUnix, sourceCurrency, conversionRates);
}
}
12 changes: 12 additions & 0 deletions src/main/java/com/melouk/personal/data/ExchangeRateRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.melouk.personal.data;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface ExchangeRateRepository extends JpaRepository<ExchangeRateRecord, Long> {

Optional<ExchangeRateRecord> findTopBySourceCurrencyOrderByTimeLastUpdateUnixDesc(String sourceCurrency);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.melouk.personal.data;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import java.time.ZonedDateTime;
import java.util.Map;
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;

@ExtendWith(SpringExtension.class)
@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
class ExchangeRateRepositoryIT {

private static final Map<String, Double> USD_TO_EUR = Map.of("EUR", 1.0);
private static final String USD = "USD";
@Autowired
private ExchangeRateRepository rateRepository;

@Test
void testFindLatestForSameSourceCurrency() {
ExchangeRateRecord yesterdayRate =
new ExchangeRateRecord(ZonedDateTime.now().minusDays(1), USD, USD_TO_EUR);

rateRepository.save(yesterdayRate);
ExchangeRateRecord todayRate =
new ExchangeRateRecord(ZonedDateTime.now(), USD, USD_TO_EUR);
ExchangeRateRecord expected = rateRepository.save(todayRate);
Optional<ExchangeRateRecord> test = rateRepository.findTopBySourceCurrencyOrderByTimeLastUpdateUnixDesc(USD);
assertThat(test).isPresent().hasValue(expected);
}

@Test
void testReturnsEmptyForNonPersistedRates() {
Optional<ExchangeRateRecord> test = rateRepository.findTopBySourceCurrencyOrderByTimeLastUpdateUnixDesc("EUR");

assertThat(test).isEmpty();
}
}
Loading