From 6a00262597639dc13bddd9b6a85c4c502f79ed63 Mon Sep 17 00:00:00 2001 From: =?utf8?q?V=C3=A1s=C3=A1ry=20D=C3=A1niel?= Date: Tue, 23 Jan 2024 17:39:58 +0100 Subject: [PATCH] Exchange rate client added --- .../service/mnb/ExchangeRateServiceIT.java | 30 +++++-- .../lis/service/mnb/ExchangeRateService.java | 89 ++++++++++++------- 2 files changed, 78 insertions(+), 41 deletions(-) diff --git a/lis-app/src/test/java/hu/user/lis/service/mnb/ExchangeRateServiceIT.java b/lis-app/src/test/java/hu/user/lis/service/mnb/ExchangeRateServiceIT.java index 8ce3e5b..ffc2d8c 100644 --- a/lis-app/src/test/java/hu/user/lis/service/mnb/ExchangeRateServiceIT.java +++ b/lis-app/src/test/java/hu/user/lis/service/mnb/ExchangeRateServiceIT.java @@ -1,34 +1,46 @@ package hu.user.lis.service.mnb; +import hu.user.lis.db.Currency; import lombok.extern.log4j.Log4j2; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.context.annotation.ComponentScan; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit4.SpringRunner; +import java.time.LocalDate; + +import static org.junit.Assert.assertNotNull; + @Log4j2 @SpringBootTest @RunWith(SpringRunner.class) @ActiveProfiles("dev") -@ComponentScan("hu.user.lis") @TestPropertySource("classpath:application-dev.yaml") -//@EnableAutoConfiguration(exclude = { -// DataSourceAutoConfiguration.class, -// DataSourceTransactionManagerAutoConfiguration.class, -// HibernateJpaAutoConfiguration.class -//}) public class ExchangeRateServiceIT { @Autowired private ExchangeRateService exchangeRateService; @Test - public void test() { - exchangeRateService.getInfo(); + public void printInfoTest() { + exchangeRateService.printInfo(); + } + + @Test + public void getCurrentExchangeRateTest() { + Double exchangeRate = exchangeRateService.getExchangeRate(Currency.EUR); + assertNotNull(exchangeRate); + log.info("Current EURO rate : {}", exchangeRate); + } + + @Test + public void getExchangeRateFrom10DaysAgoTest() { + Double exchangeRate = exchangeRateService.getExchangeRate(Currency.EUR, LocalDate.now().plusDays(-10)); + assertNotNull(exchangeRate); + log.info("EURO rate 10 days ago : {}", exchangeRate); } } diff --git a/lis-service/src/main/java/hu/user/lis/service/mnb/ExchangeRateService.java b/lis-service/src/main/java/hu/user/lis/service/mnb/ExchangeRateService.java index 9b7aebc..de95eaf 100644 --- a/lis-service/src/main/java/hu/user/lis/service/mnb/ExchangeRateService.java +++ b/lis-service/src/main/java/hu/user/lis/service/mnb/ExchangeRateService.java @@ -1,73 +1,98 @@ package hu.user.lis.service.mnb; +import hu.user.lis.db.Currency; import hu.user.lis.service.mnb.client.MNBArfolyamServiceSoap; import hu.user.lis.service.mnb.client.MNBArfolyamServiceSoapImpl; +import lombok.extern.log4j.Log4j2; +import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; +import org.xml.sax.SAXException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import java.io.IOException; import java.io.StringReader; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.util.Objects; +@Log4j2 @Service public class ExchangeRateService { + private final MNBArfolyamServiceSoap remoteExchangeService; - public void getInfo() { + private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); + + public ExchangeRateService() { MNBArfolyamServiceSoapImpl impl = new MNBArfolyamServiceSoapImpl(); - MNBArfolyamServiceSoap service = impl.getCustomBindingMNBArfolyamServiceSoap(); - try { - // USD,EUR árfolyam 2015-01-01 és 2015-01-10 közötti lekérdezése - String resp = service.getExchangeRates("2015-01-01", "2015-01-10", "USD,EUR"); + remoteExchangeService = impl.getCustomBindingMNBArfolyamServiceSoap(); + } - DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); - DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); - InputSource is = new InputSource(new StringReader(resp)); - Document doc = dBuilder.parse(is); + private Document getDocument(String response) throws ParserConfigurationException, SAXException, IOException { + DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); + InputSource is = new InputSource(new StringReader(response)); + Document doc = dBuilder.parse(is); + doc.getDocumentElement().normalize(); + return doc; + } - doc.getDocumentElement().normalize(); + public Double getExchangeRate(Currency currency) { + return getExchangeRate(currency, null); + } - // "Day" nevu elemek kiszedése - NodeList nList = doc.getElementsByTagName("Day"); + public Double getExchangeRate(Currency currency, LocalDate date) { + Double result = null; + try { + String response; + if (Objects.isNull(date)) { + response = remoteExchangeService.getCurrentExchangeRates(); + } else { + String dateParam = date.format(formatter); + response = remoteExchangeService.getExchangeRates(dateParam, dateParam, currency.toString()); + } - // iterálás a "Day" elemeken + Document doc = getDocument(response); + NodeList nList = doc.getElementsByTagName("Day"); for (int temp = 0; temp < nList.getLength(); temp++) { Node nNode = nList.item(temp); if (nNode.getNodeType() == Node.ELEMENT_NODE) { - Element eElement = (Element) nNode; - // "date" nevu attribútum értékének kinyerése - System.out.println("Date : " + eElement.getAttribute("date")); - - // az elem alatt lévo "Rate" elemek kinyerése NodeList rateList = eElement.getElementsByTagName("Rate"); for (int temp1 = 0; temp1 < rateList.getLength(); temp1++) { Node nNode1 = rateList.item(temp1); if (nNode.getNodeType() == Node.ELEMENT_NODE) { Element eElement1 = (Element) nNode1; - - // "curr" nevu attribútum értékének kinyerése // a deviza neve // - System.out.println("Currency : " + eElement1.getAttribute("curr")); - - // "unit" nevu attribútum értékének kinyerése // a deviza egysége // - System.out.println("Unit : " + eElement1.getAttribute("unit")); - - // az xml tag értéke // jelen esetben a deviza árfolyama // - System.out.println("Value : " + eElement1.getTextContent()); - + if (eElement1.getAttribute("curr").equals(currency.toString())) { + String strValue = eElement1.getTextContent(); + if (StringUtils.isNotBlank(strValue)) { + result = Double.parseDouble(strValue.replace(",", ".")); + break; + } + } } } - System.out.println(); } } - - } catch (Exception e) { - System.err.print(e); + log.error(e); } + return result; + } + public void printInfo() { + try { + String response = remoteExchangeService.getInfo(); + log.info(response); + } catch (Exception e) { + log.error(e); + } } + } -- 2.54.0