dimanche 21 avril 2019

Looking for an advice how to test if the currency shortcut is correct

I am writing a program which converts the values of each currency to another using simple API. I had no trouble at all developing that simple program, but I am currently struggling in testing it. I am a 3 month programmer so don't blame me for code quality - give me tips for the future.

I have already written a test which checks if the conversion from each currency to another is correct, now I am looking for a way to test if a currency shortcut(for ex. EUR, PLN, JPY, AUD..) is correct, what I mean is that test should provide to me an information when a name (AUR or JPU) is not correct. I got a class "Rates" which contains values of JSON Properties of each currency.

package pl.sda.CurrencyConverter_RZ.model;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Rates {

    @JsonProperty("BGN")
    private Double BGN;

    @JsonProperty("NZD")
    private Double NZD;

    @JsonProperty("ILS")
    private Double ILS;

    @JsonProperty("RUB")
    private Double RUB;

    @JsonProperty("CAD")
    private Double CAD;

    @JsonProperty("USD")
    private Double USD;

    @JsonProperty("PHP")
    private Double PHP;

    @JsonProperty("CHF")
    private Double CHF;

    @JsonProperty("AUD")
    private Double AUD;

    @JsonProperty("JPY")
    private Double JPY;

    @JsonProperty("TRY")
    private Double TRY;

    @JsonProperty("HKD")
    private Double HKD;

    @JsonProperty("MYR")
    private Double MYR;

    @JsonProperty("HRK")
    private Double HRK;

    @JsonProperty("CZK")
    private Double CZK;

    @JsonProperty("IDR")
    private Double IDR;

    @JsonProperty("DKK")
    private Double DKK;

    @JsonProperty("NOK")
    private Double NOK;

    @JsonProperty("HUF")
    private Double HUF;

    @JsonProperty("GBP")
    private Double GBP;

    @JsonProperty("MXN")
    private Double MXN;

    @JsonProperty("THB")
    private Double THB;

    @JsonProperty("ISK")
    private Double ISK;

    @JsonProperty("ZAR")
    private Double ZAR;

    @JsonProperty("BRL")
    private Double BRL;

    @JsonProperty("SGD")
    private Double SGD;

    @JsonProperty("PLN")
    private Double PLN;

    @JsonProperty("INR")
    private Double INR;

    @JsonProperty("KRW")
    private Double KRW;

    @JsonProperty("RON")
    private Double RON;

    @JsonProperty("CNY")
    private Double CNY;

    @JsonProperty("SEK")
    private Double SEK;

    @JsonProperty("EUR")
    private Double EUR;


}

package pl.sda.CurrencyConverter_RZ.service;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import pl.sda.CurrencyConverter_RZ.model.Currencies;

import java.math.BigDecimal;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;
class CurrencyServiceTest {

    public static Currencies currencyValue = new Currencies();
    public static CurrencyService currencyService = new CurrencyService();

    @BeforeAll
    static void setUp() {

    }

    @Test
    @DisplayName("If conversion is correct by multiplying values")
    void shouldReturnTrueByMultiplying() {
        BigDecimal fromPLNtoEUR = currencyService
                .convertCurrencyValue(1, "PLN", "EUR", "2019-04-20", 4);
        BigDecimal fromEURtoPLN = currencyService
                .convertCurrencyValue(1, "EUR", "PLN", "2019-04-20", 4);
        assertAll(() -> assertThat((fromPLNtoEUR.multiply(fromEURtoPLN)).setScale(0, BigDecimal.ROUND_HALF_DOWN))
                .isEqualTo(BigDecimal.valueOf(1)),
                () -> assertThat((fromEURtoPLN.multiply(fromPLNtoEUR)).setScale(0, BigDecimal.ROUND_HALF_DOWN))
                        .isEqualTo(BigDecimal.valueOf(1)));
    }

//    @ParameterizedTest   // <- to check if its needed
//    @CsvSource({"AUD", "JPY"})
    @DisplayName("Tests if shortcut of currency is correct")
    void shouldReturnTrueIfCurrencyShortcutIsCorrect(String currencyShortcut, String correctCurrencyShortcut) {
        //TODO
    }
}

I am now wondering if I can somehow get those names from a class (only names, not values from JSON), rather that testing them one by one. Hope that someone understand what I am trying to say. Also if someone wouldn't mind giving me tips how to improve my 1st test would be awesome. It returns a BigDecimal value from each currency to another.

Aucun commentaire:

Enregistrer un commentaire