Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dev.sorn.fmp4j.exceptions;

public class FmpInvalidSectorException extends FmpException {
public FmpInvalidSectorException(String message, Object... args) {
super(message, args);
}
}
5 changes: 3 additions & 2 deletions src/main/java/dev/sorn/fmp4j/models/FmpCompany.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import dev.sorn.fmp4j.types.FmpCurrency;
import dev.sorn.fmp4j.types.FmpCusip;
import dev.sorn.fmp4j.types.FmpIsin;
import dev.sorn.fmp4j.types.FmpSector;
import dev.sorn.fmp4j.types.FmpSymbol;
import java.io.Serial;
import java.time.LocalDate;
Expand All @@ -30,7 +31,7 @@ public record FmpCompany(
String website,
String description,
String ceo,
String sector,
FmpSector sector,
String country,
String fullTimeEmployees,
String phone,
Expand All @@ -47,5 +48,5 @@ public record FmpCompany(
Boolean isFund)
implements FmpModel {
@Serial
private static final long serialVersionUID = 5L;
private static final long serialVersionUID = 6L;
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package dev.sorn.fmp4j.models;

import dev.sorn.fmp4j.types.FmpSector;
import dev.sorn.fmp4j.types.FmpSymbol;
import java.io.Serial;

public record FmpEtfSectorWeighting(FmpSymbol symbol, String sector, Double weightPercentage) implements FmpModel {
public record FmpEtfSectorWeighting(FmpSymbol symbol, FmpSector sector, Double weightPercentage) implements FmpModel {
@Serial
private static final long serialVersionUID = 2L;
private static final long serialVersionUID = 3L;
}
45 changes: 45 additions & 0 deletions src/main/java/dev/sorn/fmp4j/types/FmpSector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package dev.sorn.fmp4j.types;

import static java.util.Arrays.stream;

import com.fasterxml.jackson.annotation.JsonCreator;
import dev.sorn.fmp4j.exceptions.FmpInvalidSectorException;
import java.util.Objects;

public enum FmpSector implements FmpValueObject<String> {
BASIC_MATERIALS("Basic Materials"),
COMMUNICATION_SERVICES("Communication Services"),
CONSUMER_CYCLICAL("Consumer Cyclical"),
CONSUMER_DEFENSIVE("Consumer Defensive"),
ENERGY("Energy"),
FINANCIAL_SERVICES("Financial Services"),
HEALTHCARE("Healthcare"),
INDUSTRIALS("Industrials"),
REAL_ESTATE("Real Estate"),
TECHNOLOGY("Technology"),
UTILITIES("Utilities");

private final String value;

FmpSector(String value) {
this.value = value;
}

@JsonCreator
public static FmpSector sector(String value) {
return stream(values())
.filter(sector -> Objects.equals(sector.value, value))
.findFirst()
.orElseThrow(() -> new FmpInvalidSectorException("[%s] is not a valid sector", value));
}

@Override
public String value() {
return value;
}

@Override
public String toString() {
return value();
}
}
46 changes: 46 additions & 0 deletions src/test/java/dev/sorn/fmp4j/types/FmpSectorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package dev.sorn.fmp4j.types;

import static dev.sorn.fmp4j.types.FmpSector.sector;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import dev.sorn.fmp4j.exceptions.FmpInvalidSectorException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

class FmpSectorTest {
@ParameterizedTest
@ValueSource(
strings = {
"Basic Materials",
"Communication Services",
"Consumer Cyclical",
"Consumer Defensive",
"Energy",
"Financial Services",
"Healthcare",
"Industrials",
"Real Estate",
"Technology",
"Utilities",
})
void valid_sectors(String readable) {
// given // when
var sector = sector(readable);

// then
assertEquals(readable, sector.value());
assertEquals(readable, sector.toString());
}

@Test
void invalid_sector() {
// given
var invalid = "Invalid Sector";

// when // then
var e = assertThrows(FmpInvalidSectorException.class, () -> sector(invalid));
assertEquals("[Invalid Sector] is not a valid sector", e.getMessage());
}
}
3 changes: 2 additions & 1 deletion src/testFixtures/java/dev/sorn/fmp4j/CompanyTestData.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static dev.sorn.fmp4j.types.FmpCurrency.USD;
import static dev.sorn.fmp4j.types.FmpCusip.cusip;
import static dev.sorn.fmp4j.types.FmpIsin.isin;
import static dev.sorn.fmp4j.types.FmpSector.sector;
import static dev.sorn.fmp4j.types.FmpSymbol.symbol;

import dev.sorn.fmp4j.models.FmpCompany;
Expand Down Expand Up @@ -33,7 +34,7 @@ default FmpCompany aCompany() {
"https://www.apple.com",
"Apple Inc. designs, manufactures, and markets smartphones, personal computers, tablets, wearables, and accessories worldwide. The company offers iPhone, a line of smartphones; Mac, a line of personal computers; iPad, a line of multi-purpose tablets; and wearables, home, and accessories comprising AirPods, Apple TV, Apple Watch, Beats products, and HomePod. It also provides AppleCare support and cloud services; and operates various platforms, including the App Store that allow customers to discov...",
"Mr. Timothy D. Cook",
"Technology",
sector("Technology"),
"US",
"164000",
"(408) 996-1010",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package dev.sorn.fmp4j;

import static dev.sorn.fmp4j.types.FmpSector.sector;
import static dev.sorn.fmp4j.types.FmpSymbol.symbol;

import dev.sorn.fmp4j.models.FmpEtfSectorWeighting;

public interface EtfSectorWeightingTestData {
default FmpEtfSectorWeighting anEtfSectorWeighting() {
return new FmpEtfSectorWeighting(symbol("SPY"), "Utilities", 2.45);
return new FmpEtfSectorWeighting(symbol("SPY"), sector("Utilities"), 2.45);
}
}
Binary file modified src/testFixtures/resources/models/serialization/FmpCompany.snapshot
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading