// Path: src/main/java/com/github/vedenin/codingchallenge/common/CurrencyEnum.java
// public enum CurrencyEnum {
//     EUR("EUR", "Euro (EUR)"),
//     USD("USD", "US Dollar (USD)"),
//     GBP("GBP", "British Pound (GBP)"),
//     NZD("NZD", "New Zealand Dollar (NZD)"),
//     AUD("AUD", "Australian Dollar (AUD)"),
//     JPY("JPY", "Japanese Yen (JPY)"),
//     HUF("HUF", "Hungarian Forint (HUF)"),
//     RUB("RUB", "Russian Ruble (RUB)"),
//     PLN("PLN", "Polish Zloty (PLN)");
//     private final String code;
//     private final String description;
// 
//     CurrencyEnum(String code, String description) {
//         this.code = code;
//         this.description = description;
//     }
// 
//     public String getCode() {
//         return code;
//     }
// 
//     public String getDescription() {
//         return description;
//     }
// }
// 
// Path: src/main/java/com/github/vedenin/codingchallenge/exceptions/RestClientException.java
// public class RestClientException extends RuntimeException {
//     public RestClientException() {
//     }
// 
//     public RestClientException(String message) {
//         super(message);
//     }
// 
//     public RestClientException(String message, Throwable cause) {
//         super(message, cause);
//     }
// 
//     public RestClientException(Throwable cause) {
//         super(cause);
//     }
// 
//     public RestClientException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
//         super(message, cause, enableSuppression, writableStackTrace);
//     }
// }
// 
// Path: src/main/java/com/github/vedenin/codingchallenge/restclient/RestClient.java
// public interface RestClient {
//     BigDecimal getCurrentExchangeRates(CurrencyEnum currecnyFrom, CurrencyEnum currecnyTo);
//     BigDecimal getHistoricalExchangeRates(CurrencyEnum currencyFrom, CurrencyEnum currencyTo, Calendar calendar);
// }
// 
// Path: src/main/java/com/github/vedenin/codingchallenge/restclient/impl/currencylayer/CurrencyLayerRestClient.java
// @Service("CurrencyLayer")
// public class CurrencyLayerRestClient implements RestClient {
//     private final static String URL_LAST = "http://www.apilayer.net/api/live?format=1";
//     private final static String URL_HISTORY = "http://apilayer.net/api/historical?date=";
//     private final static String API_ID = "f7e5948888d41713110273b47c682db0";
//     private final static String API_ID_PRM = "&access_key=";
//     private static final String ERROR_WHILE_GATHERING_INFORMATION = "Error while gathering information from CurrencyLayer services";
// 
// 
//     public BigDecimal getCurrentExchangeRates(CurrencyEnum currencyFrom, CurrencyEnum currencyTo) {
//         return getExchangeRates(currencyFrom, currencyTo, getCurrentRates());
//     }
// 
//     public BigDecimal getHistoricalExchangeRates(CurrencyEnum currencyFrom, CurrencyEnum currencyTo, Calendar calendar) {
//         return getExchangeRates(currencyFrom, currencyTo, getHistoricalRates(calendar));
//     }
// 
//     private BigDecimal getExchangeRates(CurrencyEnum currencyFrom, CurrencyEnum currencyTo, CurrencyLayerRatesContainer rates) {
//         BigDecimal currencyFromRate = getRates(currencyFrom, rates);
//         BigDecimal currencyToRate = getRates(currencyTo, rates);
//         return currencyToRate.divide(currencyFromRate, 20, ROUND_FLOOR);
//     }
// 
//     public CurrencyLayerRatesContainer getCurrentRates() {
//         return getEntity(URL_LAST, API_ID, CurrencyLayerRatesContainer.class);
//     }
// 
//     public CurrencyLayerRatesContainer getHistoricalRates(Calendar date) {
//         return getEntity(URL_HISTORY + date.get(Calendar.YEAR) + "-" +
//                         getStringWithLeftPadZero(date.get(Calendar.MONTH) + 1) + "-" +
//                         getStringWithLeftPadZero(date.get(Calendar.DAY_OF_MONTH))
//                 , API_ID, CurrencyLayerRatesContainer.class);
//     }
// 
//     private String getStringWithLeftPadZero(int number) {
//         return String.format("%02d", number);
//     }
// 
//     private static BigDecimal getRates(CurrencyEnum currency, CurrencyLayerRatesContainer rates) {
//         if (rates.getQuotes() == null) {
//             throw new RestClientException(ERROR_WHILE_GATHERING_INFORMATION +
//                     (rates.getError() != null ? ", code:" + rates.getError().getCode() + ", error:" +
//                             rates.getError().getInfo() : ""));
//         } else {
//             String rate = rates.getQuotes().get("USD" + currency.getCode());
//             return new BigDecimal(rate);
//         }
//     }
// 
//     private static <T> T getEntity(String url, String api, Class<T> entityClass) {
//         Response response = null;
//         try {
//             Client client = ClientBuilder.newBuilder().build();
//             response = client.target(url + API_ID_PRM + api).request().get();
//             return response.readEntity(entityClass);
//         } catch (Exception exp) {
//             throw new RestClientException(ERROR_WHILE_GATHERING_INFORMATION + ", error: " +
//                     (response != null? response.getStatusInfo(): exp.getMessage()));
//         } finally {
//             if (response != null) {
//                 response.close();
//             }
//         }
//     }
// 
// 
// }


import com.github.vedenin.codingchallenge.common.CurrencyEnum;
import com.github.vedenin.codingchallenge.exceptions.RestClientException;
import com.github.vedenin.codingchallenge.restclient.RestClient;
import com.github.vedenin.codingchallenge.restclient.impl.currencylayer.CurrencyLayerRestClient;
import org.junit.Assert;
import org.junit.Test;
import java.math.BigDecimal;
import java.util.GregorianCalendar;
package com.github.vedenin.codingchallenge;



/**
 * Created by vvedenin on 2/10/2017.
 */

public class CurrencyLayerRestClientTest {
    @Test
    public void ExternalTest() {
        RestClient restClient = new CurrencyLayerRestClient();
// Path: src/main/java/com/github/vedenin/codingchallenge/common/CurrencyEnum.java
// public enum CurrencyEnum {
//     EUR("EUR", "Euro (EUR)"),
//     USD("USD", "US Dollar (USD)"),
//     GBP("GBP", "British Pound (GBP)"),
//     NZD("NZD", "New Zealand Dollar (NZD)"),
//     AUD("AUD", "Australian Dollar (AUD)"),
//     JPY("JPY", "Japanese Yen (JPY)"),
//     HUF("HUF", "Hungarian Forint (HUF)"),
//     RUB("RUB", "Russian Ruble (RUB)"),
//     PLN("PLN", "Polish Zloty (PLN)");
//     private final String code;
//     private final String description;
// 
//     CurrencyEnum(String code, String description) {
//         this.code = code;
//         this.description = description;
//     }
// 
//     public String getCode() {
//         return code;
//     }
// 
//     public String getDescription() {
//         return description;
//     }
// }
// 
// Path: src/main/java/com/github/vedenin/codingchallenge/exceptions/RestClientException.java
// public class RestClientException extends RuntimeException {
//     public RestClientException() {
//     }
// 
//     public RestClientException(String message) {
//         super(message);
//     }
// 
//     public RestClientException(String message, Throwable cause) {
//         super(message, cause);
//     }
// 
//     public RestClientException(Throwable cause) {
//         super(cause);
//     }
// 
//     public RestClientException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
//         super(message, cause, enableSuppression, writableStackTrace);
//     }
// }
// 
// Path: src/main/java/com/github/vedenin/codingchallenge/restclient/RestClient.java
// public interface RestClient {
//     BigDecimal getCurrentExchangeRates(CurrencyEnum currecnyFrom, CurrencyEnum currecnyTo);
//     BigDecimal getHistoricalExchangeRates(CurrencyEnum currencyFrom, CurrencyEnum currencyTo, Calendar calendar);
// }
// 
// Path: src/main/java/com/github/vedenin/codingchallenge/restclient/impl/currencylayer/CurrencyLayerRestClient.java
// @Service("CurrencyLayer")
// public class CurrencyLayerRestClient implements RestClient {
//     private final static String URL_LAST = "http://www.apilayer.net/api/live?format=1";
//     private final static String URL_HISTORY = "http://apilayer.net/api/historical?date=";
//     private final static String API_ID = "f7e5948888d41713110273b47c682db0";
//     private final static String API_ID_PRM = "&access_key=";
//     private static final String ERROR_WHILE_GATHERING_INFORMATION = "Error while gathering information from CurrencyLayer services";
// 
// 
//     public BigDecimal getCurrentExchangeRates(CurrencyEnum currencyFrom, CurrencyEnum currencyTo) {
//         return getExchangeRates(currencyFrom, currencyTo, getCurrentRates());
//     }
// 
//     public BigDecimal getHistoricalExchangeRates(CurrencyEnum currencyFrom, CurrencyEnum currencyTo, Calendar calendar) {
//         return getExchangeRates(currencyFrom, currencyTo, getHistoricalRates(calendar));
//     }
// 
//     private BigDecimal getExchangeRates(CurrencyEnum currencyFrom, CurrencyEnum currencyTo, CurrencyLayerRatesContainer rates) {
//         BigDecimal currencyFromRate = getRates(currencyFrom, rates);
//         BigDecimal currencyToRate = getRates(currencyTo, rates);
//         return currencyToRate.divide(currencyFromRate, 20, ROUND_FLOOR);
//     }
// 
//     public CurrencyLayerRatesContainer getCurrentRates() {
//         return getEntity(URL_LAST, API_ID, CurrencyLayerRatesContainer.class);
//     }
// 
//     public CurrencyLayerRatesContainer getHistoricalRates(Calendar date) {
//         return getEntity(URL_HISTORY + date.get(Calendar.YEAR) + "-" +
//                         getStringWithLeftPadZero(date.get(Calendar.MONTH) + 1) + "-" +
//                         getStringWithLeftPadZero(date.get(Calendar.DAY_OF_MONTH))
//                 , API_ID, CurrencyLayerRatesContainer.class);
//     }
// 
//     private String getStringWithLeftPadZero(int number) {
//         return String.format("%02d", number);
//     }
// 
//     private static BigDecimal getRates(CurrencyEnum currency, CurrencyLayerRatesContainer rates) {
//         if (rates.getQuotes() == null) {
//             throw new RestClientException(ERROR_WHILE_GATHERING_INFORMATION +
//                     (rates.getError() != null ? ", code:" + rates.getError().getCode() + ", error:" +
//                             rates.getError().getInfo() : ""));
//         } else {
//             String rate = rates.getQuotes().get("USD" + currency.getCode());
//             return new BigDecimal(rate);
//         }
//     }
// 
//     private static <T> T getEntity(String url, String api, Class<T> entityClass) {
//         Response response = null;
//         try {
//             Client client = ClientBuilder.newBuilder().build();
//             response = client.target(url + API_ID_PRM + api).request().get();
//             return response.readEntity(entityClass);
//         } catch (Exception exp) {
//             throw new RestClientException(ERROR_WHILE_GATHERING_INFORMATION + ", error: " +
//                     (response != null? response.getStatusInfo(): exp.getMessage()));
//         } finally {
//             if (response != null) {
//                 response.close();
//             }
//         }
//     }
// 
// 
// }


// Path: src/test/java/com.github.vedenin.codingchallenge/CurrencyLayerRestClientTest.java
import com.github.vedenin.codingchallenge.common.CurrencyEnum;
import com.github.vedenin.codingchallenge.exceptions.RestClientException;
import com.github.vedenin.codingchallenge.restclient.RestClient;
import com.github.vedenin.codingchallenge.restclient.impl.currencylayer.CurrencyLayerRestClient;
import org.junit.Assert;
import org.junit.Test;
import java.math.BigDecimal;
import java.util.GregorianCalendar;

package com.github.vedenin.codingchallenge;



/**
 * Created by vvedenin on 2/10/2017.
 */

public class CurrencyLayerRestClientTest {
    @Test
    public void ExternalTest() {
        RestClient restClient = new CurrencyLayerRestClient();
        BigDecimal convertRates = restClient.getCurrentExchangeRates(CurrencyEnum.EUR, CurrencyEnum.RUB);