Skip to content

Commit

Permalink
Added test case to sync etf-data into taxonomy
Browse files Browse the repository at this point in the history
Issue: #1908
  • Loading branch information
buchen committed Jan 6, 2021
1 parent 31447b2 commit d2e9d0d
Show file tree
Hide file tree
Showing 6 changed files with 221 additions and 20 deletions.
@@ -0,0 +1,19 @@
package name.abuchen.portfolio;

import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class TestUtilities
{
private TestUtilities()
{
}

public static String read(Class<?> clazz, String resource)
{
try (Scanner scanner = new Scanner(clazz.getResourceAsStream(resource), StandardCharsets.UTF_8.name()))
{
return scanner.useDelimiter("\\A").next(); //$NON-NLS-1$
}
}
}
@@ -0,0 +1,106 @@
package name.abuchen.portfolio.online.impl;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.nullValue;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.junit.BeforeClass;
import org.junit.Test;

import name.abuchen.portfolio.SecurityBuilder;
import name.abuchen.portfolio.TaxonomyBuilder;
import name.abuchen.portfolio.TestUtilities;
import name.abuchen.portfolio.model.AttributeType;
import name.abuchen.portfolio.model.Classification;
import name.abuchen.portfolio.model.Classification.Assignment;
import name.abuchen.portfolio.model.Client;
import name.abuchen.portfolio.model.ClientSettings;
import name.abuchen.portfolio.model.Security;
import name.abuchen.portfolio.model.Taxonomy;

@SuppressWarnings("nls")
public class ETFDataComTest
{
private static ETFDataCom.OnlineItem item;

@BeforeClass
public static void setup()
{
JSONObject object = (JSONObject) JSONValue
.parse(TestUtilities.read(ETFDataComTest.class, "etf-data.com.response.txt"));
item = ETFDataCom.OnlineItem.from(object);
}

@Test
public void testItemAttributes()
{
assertThat(item.getIsin(), is("DE000A0H0744"));
assertThat(item.getName(), is("iShares Dow Jones Asia Pacific Select Dividend 50 UCITS ETF (DE)"));

// test creation of security

ClientSettings settings = new ClientSettings();
settings.getAttributeTypes().collect(Collectors.toList()).stream().forEach(settings::removeAttributeType);

Security security = item.create(settings);

assertThat(security.getIsin(), is(item.getIsin()));
assertThat(security.getName(), is(item.getName()));

assertThat(settings.getAttributeTypes().count(), is(7L));

Function<String, Object> attribute2value = name -> {
AttributeType type = settings.getAttributeTypes().filter(a -> name.equals(a.getSource())).findFirst()
.orElseThrow(IllegalArgumentException::new);
return security.getAttributes().get(type);
};

assertThat(attribute2value.apply("etf-data.com$distributionType"), is("DISTRIBUTING"));
assertThat(attribute2value.apply("etf-data.com$distributionFrequency"), is(nullValue()));
assertThat(attribute2value.apply("etf-data.com$domicile"), is("DEU"));
}

@Test
public void testUpdateOfTaxonomy()
{
final String someIdentifier = "SOMETHING";

Client client = new Client();
Security security = new SecurityBuilder().addTo(client);
Taxonomy taxonomy = new TaxonomyBuilder().addClassification(someIdentifier).addTo(client);

boolean hasModifications = item.updateCountryAllocation(taxonomy, security);
assertThat(hasModifications, is(true));

// second update does not modify the taxonomy
hasModifications = item.updateCountryAllocation(taxonomy, security);
assertThat(hasModifications, is(false));

List<Classification> classifications = taxonomy.getClassifications(security);
assertThat(classifications.size(), is(6));
Assignment assignment = classifications.stream().filter(c -> c.getName().equals("CHN")).findAny()
.orElseThrow(IllegalArgumentException::new).getAssignments().get(0);
assertThat(assignment.getWeight(), is(451));

// modify taxonomy

assignment.setWeight(111);

classifications.stream().filter(c -> c.getName().equals("NZL")).findAny()
.orElseThrow(IllegalArgumentException::new).clearAssignments();

taxonomy.getClassificationById(someIdentifier).addAssignment(new Assignment(security));

hasModifications = item.updateCountryAllocation(taxonomy, security);
assertThat(hasModifications, is(true));
assertThat(assignment.getWeight(), is(451));
assertThat(taxonomy.getClassificationById(someIdentifier).getAssignments().isEmpty(), is(true));

}
}
Expand Up @@ -5,15 +5,14 @@

import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.Scanner;

import org.junit.Test;

import name.abuchen.portfolio.TestUtilities;
import name.abuchen.portfolio.model.LatestSecurityPrice;
import name.abuchen.portfolio.model.Security;
import name.abuchen.portfolio.model.SecurityPrice;
Expand All @@ -29,7 +28,7 @@ public void testHistorical()
{
HTMLTableQuoteFeed feed = new HTMLTableQuoteFeed();

String html = read("HTMLTableQuoteFeedHistoricalSample.html");
String html = TestUtilities.read(getClass(), "HTMLTableQuoteFeedHistoricalSample.html");

QuoteFeedData data = feed.getHistoricalQuotes(html);

Expand All @@ -52,7 +51,7 @@ public void testGetLatestQuote()
@Override
String getHtml(String url) throws IOException, URISyntaxException
{
return read("HTMLTableQuoteFeedNonHistoricalSample.html");
return TestUtilities.read(getClass(), "HTMLTableQuoteFeedNonHistoricalSample.html");
}
};
Security security = new Security("foo", "EUR");
Expand All @@ -75,13 +74,4 @@ private void assertPrice(LatestSecurityPrice price, String date, long value, lon
assertThat(price.getHigh(), is(high));
assertThat(price.getLow(), is(low));
}

private String read(String resourceName)
{
try (Scanner scanner = new Scanner(getClass().getResourceAsStream(resourceName), StandardCharsets.UTF_8.name()))
{
return scanner.useDelimiter("\\A").next(); //$NON-NLS-1$
}
}

}
@@ -0,0 +1,86 @@
{
"assetClass": "EQUITY",
"baseCurrency": "EUR",
"distributionFrequency": null,
"distributionType": "DISTRIBUTING",
"domicile": "DEU",
"indexName": "DJ Asia Pacific Select Dividend 50 Index",
"isin": "DE000A0H0744",
"listings": [
{
"bloombergTicker": "APSDEX GY",
"currency": "EUR",
"exchange": "XETRA",
"reutersTicker": "EXXW.DE",
"ticker": "EXXW"
}
],
"name": "iShares Dow Jones Asia Pacific Select Dividend 50 UCITS ETF (DE)",
"provider": "iShares",
"regions": [
{
"country": "HKG",
"percentage": 39.49
},
{
"country": "AUS",
"percentage": 23.47
},
{
"country": "JPN",
"percentage": 18.37
},
{
"country": "SGP",
"percentage": 11.69
},
{
"country": "CHN",
"percentage": 4.51
},
{
"country": "NZL",
"percentage": 2.27
}
],
"replicationMethod": "PHYSICAL",
"sectors": [
{
"percentage": 31.71,
"sector": "FINANCIALS"
},
{
"percentage": 20.78,
"sector": "REAL_ESTATE"
},
{
"percentage": 15.6,
"sector": "INDUSTRIALS"
},
{
"percentage": 8.47,
"sector": "COMMUNICATION_SERVICES"
},
{
"percentage": 8.35,
"sector": "MATERIALS"
},
{
"percentage": 5.71,
"sector": "UTILITIES"
},
{
"percentage": 4.08,
"sector": "ENERGY"
},
{
"percentage": 3.44,
"sector": "INFORMATION_TECHNOLOGY"
},
{
"percentage": 1.66,
"sector": "CONSUMER_DISCRETIONARY"
}
],
"totalFee": 0.31
}
Expand Up @@ -213,6 +213,11 @@ public void removeAssignment(Assignment assignment)
assignments.remove(assignment);
}

public void clearAssignments()
{
assignments.clear();
}

public int getWeight()
{
return weight;
Expand Down
Expand Up @@ -225,20 +225,15 @@ private boolean updateAttributes(Attributes attributes, ClientSettings settings)
newAttribute.setSource(id);
newAttribute.setTarget(Security.class);

if (id2value.getValue() instanceof Double)
if (id.endsWith("$ter")) //$NON-NLS-1$
{
newAttribute.setType(Double.class);
newAttribute.setConverter(PercentConverter.class);
}
else if (id2value.getValue() instanceof String)
else
{
newAttribute.setType(String.class);
newAttribute.setConverter(StringConverter.class);

}
else
{
throw new IllegalArgumentException();
}

// add new attribute to settings
Expand Down

0 comments on commit d2e9d0d

Please sign in to comment.