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);
}
}
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);
+ }
}
+
}