001/*
002 * Copyright (c) 2012, 2013, Werner Keil, Credit Suisse (Anatole Tresch). Licensed under the Apache
003 * License, Version 2.0 (the "License"); you may not use this file except in compliance with the
004 * License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
005 * Unless required by applicable law or agreed to in writing, software distributed under the License
006 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
007 * or implied. See the License for the specific language governing permissions and limitations under
008 * the License. Contributors: Anatole Tresch - initial version.
009 */
010package org.javamoney.tck.tests.internal;
011
012import org.javamoney.tck.tests.conversion.TestExchangeRate;
013
014import javax.money.CurrencyUnit;
015import javax.money.MonetaryAmount;
016import javax.money.MonetaryCurrencies;
017import javax.money.convert.*;
018import java.util.Objects;
019
020/**
021 * Test ExchangeProvider. Created by Anatole on 26.04.2014.
022 */
023public class TestRateProvider implements ExchangeRateProvider{
024
025    private ProviderContext PC = ProviderContextBuilder.of("TestRateProvider", RateType.OTHER).build();
026    private ConversionContext CC = ConversionContextBuilder.create(PC, RateType.OTHER).build();
027    private CurrencyUnit TERM = new TestCurrencyUnit("FOO");
028
029    private CurrencyConversion CONVERSION = new CurrencyConversion(){
030
031        @Override
032        public CurrencyUnit getCurrency(){
033            return TERM;
034        }
035
036        @Override
037        public ConversionContext getContext() {
038            return CC;
039        }
040
041        @Override
042        public ExchangeRate getExchangeRate(MonetaryAmount sourceAmount){
043            return new TestExchangeRate.Builder(CC).setFactor(new TestNumberValue(2))
044                    .setBase(sourceAmount.getCurrency()).setTerm(TERM).build();
045        }
046
047        @Override
048        public ExchangeRateProvider getExchangeRateProvider() {
049            return TestRateProvider.this;
050        }
051
052        @Override
053        public MonetaryAmount apply(MonetaryAmount value){
054            return value.multiply(2).getFactory().setCurrency(TERM).create();
055        }
056    };
057
058    @Override
059    public ProviderContext getContext() {
060        return PC;
061    }
062
063    @Override
064    public boolean isAvailable(CurrencyUnit base, CurrencyUnit term){
065        Objects.requireNonNull(base);
066        Objects.requireNonNull(term);
067        return "FOO".equals(term.getCurrencyCode()) || "XXX".equals(term.getCurrencyCode());
068    }
069
070    @Override
071    public boolean isAvailable(ConversionQuery conversionContext){
072        Objects.requireNonNull(conversionContext);
073        Objects.requireNonNull(conversionContext.getCurrency());
074        return "FOO".equals(conversionContext.getCurrency().getCurrencyCode()) ||
075                "XXX".equals(conversionContext.getCurrency().getCurrencyCode());
076    }
077
078    @Override
079    public boolean isAvailable(String baseCode, String termCode){
080        return "Foo".equals(termCode) || "XXX".equals(termCode);
081    }
082
083
084    @Override
085    public ExchangeRate getExchangeRate(CurrencyUnit base, CurrencyUnit term){
086        if(isAvailable(base, term)){
087            return new TestExchangeRate.Builder(CC).setFactor(new TestNumberValue(2)).setBase(base).setTerm(term)
088                    .build();
089        }
090        return null;
091    }
092
093    @Override
094    public ExchangeRate getExchangeRate(ConversionQuery conversionQuery){
095        if(isAvailable(conversionQuery)){
096            return new TestExchangeRate.Builder(
097                    ConversionContextBuilder.create(getContext(), RateType.OTHER).importContext(conversionQuery)
098                            .build()).setFactor(new TestNumberValue(2)).setBase(conversionQuery.getBaseCurrency())
099                    .setTerm(conversionQuery.getCurrency()).build();
100        }
101        return null;
102    }
103
104    @Override
105    public ExchangeRate getExchangeRate(String baseCode, String termCode){
106        if(isAvailable(baseCode, termCode)){
107            return getExchangeRate(MonetaryCurrencies.getCurrency(baseCode), TERM);
108        }
109        return null;
110    }
111
112    @Override
113    public ExchangeRate getReversed(ExchangeRate rate){
114        return null;
115    }
116
117    @Override
118    public CurrencyConversion getCurrencyConversion(CurrencyUnit term){
119        return CONVERSION;
120    }
121
122    @Override
123    public CurrencyConversion getCurrencyConversion(ConversionQuery conversionQuery){
124        if(isAvailable(conversionQuery)){
125            return CONVERSION;
126        }
127        return null;
128    }
129
130    @Override
131    public CurrencyConversion getCurrencyConversion(String termCode){
132        if(TERM.getCurrencyCode().equals(termCode)){
133            return CONVERSION;
134        }
135        return null;
136    }
137
138}