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

Merge branch 'currency-service-tests' into 'master'

added more tests for currency service

See merge request !9
parents 5bcb258e a5cc4d56
No related branches found
No related tags found
1 merge request!9added more tests for currency service
Showing
with 119 additions and 17 deletions
......@@ -28,6 +28,6 @@ public class CurrencyController {
@GetMapping("/")
public ResponseEntity<Page<CurrencyDto>> currencies(@ModelAttribute Pageable pageRequest) {
return ResponseEntity.ok(currencyFacade.listPaged(pageRequest));
return ResponseEntity.ok(currencyFacade.listPage(pageRequest));
}
}
package cz.muni.fi.obs.facade;
import cz.muni.fi.obs.data.dbo.Currency;
import cz.muni.fi.obs.dto.CurrencyDto;
import cz.muni.fi.obs.dto.CurrencyExchangeResult;
import cz.muni.fi.obs.service.CurrencyService;
import cz.muni.fi.obs.service.ExchangeRateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class CurrencyFacade {
......@@ -28,7 +32,11 @@ public class CurrencyFacade {
return exchangeRateService.exchange(currencyFrom, currencyTo, amount);
}
public Page<CurrencyDto> listPaged(Pageable pageRequest) {
return currencyService.listPage(pageRequest);
public Page<CurrencyDto> listPage(Pageable pageRequest) {
Page<Currency> currencies = currencyService.listPage(pageRequest);
List<CurrencyDto> dtos = currencies.getContent().stream()
.map(currency -> new CurrencyDto(currency.getName(), currency.getCode()))
.collect(Collectors.toList());
return new PageImpl<>(dtos, pageRequest, currencies.getTotalElements());
}
}
......@@ -2,28 +2,22 @@ package cz.muni.fi.obs.service;
import cz.muni.fi.obs.data.dbo.Currency;
import cz.muni.fi.obs.data.repository.CurrencyRepository;
import cz.muni.fi.obs.dto.CurrencyDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class CurrencyService {
private final CurrencyRepository currencyRepository;
@Autowired
public CurrencyService(CurrencyRepository currencyRepository) {
this.currencyRepository = currencyRepository;
}
public Page<CurrencyDto> listPage(Pageable pageable) {
final Page<Currency> currencyPagedResult = currencyRepository.listPage(pageable);
List<CurrencyDto> results = currencyPagedResult.getContent().stream().map(currency ->
new CurrencyDto(currency.getName(), currency.getCode())).toList();
return new PageImpl<>(results, pageable, currencyPagedResult.getTotalElements());
public Page<Currency> listPage(Pageable pageable) {
return currencyRepository.listPage(pageable);
}
}
......@@ -26,7 +26,7 @@ public class ExchangeRateService {
this.exchangeRateRepository = exchangeRateRepository;
}
public Currency findByCode(String code) {
private Currency findByCode(String code) {
return currencyRepository.findByCode(code).orElseThrow(() -> new MissingObject(Currency.class, code));
}
......
package cz.muni.fi.obs.service;
package cz.muni.fi.obs.service.update;
import com.opencsv.CSVParser;
import com.opencsv.CSVParserBuilder;
......
package cz.muni.fi.obs.facade;
import cz.muni.fi.obs.data.dbo.Currency;
import cz.muni.fi.obs.dto.CurrencyDto;
import cz.muni.fi.obs.dto.CurrencyExchangeResult;
import cz.muni.fi.obs.service.CurrencyService;
import cz.muni.fi.obs.service.ExchangeRateService;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import java.math.BigDecimal;
import java.util.List;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
@SpringBootTest
class CurrencyFacadeTest {
@Mock
private CurrencyService currencyService;
@Mock
private ExchangeRateService exchangeRateService;
@InjectMocks
private CurrencyFacade currencyFacade;
@Autowired
private List<Currency> testData;
@Test
void exchange_betweenTwoCurrencies_returnsResult() {
String code = testData.get(1).getCode();
String code1 = testData.getFirst().getCode();
when(exchangeRateService.exchange(any(String.class), any(String.class), any(BigDecimal.class)))
.thenReturn(new CurrencyExchangeResult(code1, code, 10.2, BigDecimal.valueOf(1000), BigDecimal.valueOf(10200)));
CurrencyExchangeResult exchange = currencyFacade.exchange(code, code1, BigDecimal.valueOf(1000));
Assertions.assertThat(exchange.destAmount()).isEqualTo(BigDecimal.valueOf(10200));
}
@Test
void listPage_threeCurrenciesExist_returnsThreeCurrencies() {
Pageable pageable = Pageable.ofSize(10);
when(currencyService.listPage(pageable)).thenReturn(new PageImpl<>(testData, pageable, 3));
Page<CurrencyDto> currencies = currencyFacade.listPage(pageable);
Assertions.assertThat(currencies.getTotalElements()).isEqualTo(3);
}
}
\ No newline at end of file
package cz.muni.fi.obs.service;
import cz.muni.fi.obs.data.dbo.Currency;
import cz.muni.fi.obs.data.repository.CurrencyRepository;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import java.util.List;
import static org.mockito.Mockito.when;
@SpringBootTest
class CurrencyServiceTest {
@Mock
private CurrencyRepository currencyRepository;
@InjectMocks
private CurrencyService currencyService;
@Autowired
private List<Currency> mockData;
@Test
public void listPage_whenThreeCurrenciesPresent_returnsThreeCurrencies() {
Pageable pageable = Pageable.ofSize(10);
when(currencyRepository.listPage(pageable)).thenReturn(new PageImpl<>(mockData, pageable, 3));
Page<Currency> currencies = currencyService.listPage(pageable);
Assertions.assertThat(currencies.getTotalElements()).isEqualTo(3);
}
}
\ No newline at end of file
package cz.muni.fi.obs.service;
package cz.muni.fi.obs.service.updaters;
public class CurrencySheetsResponse {
......
package cz.muni.fi.obs.service;
package cz.muni.fi.obs.service.updaters;
import cz.muni.fi.obs.data.repository.CurrencyRepository;
import cz.muni.fi.obs.service.update.NbsCurrencyUpdateService;
......
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