Skip to content

Commit

Permalink
Merge branch 'master' into dependabot/maven/cucumber.version-7.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
pinsondg authored Dec 13, 2021
2 parents 3d00bb4 + ddf2066 commit 3a4d5e4
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 19 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.3.11</version>
<version>5.3.13</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
import com.dpgrandslam.stockdataservice.domain.model.options.OptionsChain;
import com.dpgrandslam.stockdataservice.domain.util.TimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.jni.Local;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
Expand All @@ -26,6 +26,7 @@
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
Expand Down Expand Up @@ -218,8 +219,11 @@ private List<LocalDate> parseDocumentForExpirationDates(Document document) {

private void validateExpirationDates(String ticker, List<LocalDate> expirationDates) throws AllOptionsExpirationDatesNotPresentException {
Set<LocalDate> expDatesCopy = new HashSet<>(expirationDates);
Set<LocalDate> storedExpirationDates = super.historicOptionsDataService.getExpirationDatesAtStartDate(ticker, timeUtils.getStartDayOfTradeWeek());
Set<LocalDate> storedExpirationDates = super.historicOptionsDataService.getExpirationDatesAtStartDate(ticker, timeUtils.getStartDayOfCurrentTradeWeek(2));
storedExpirationDates.removeAll(expDatesCopy);
storedExpirationDates = storedExpirationDates.stream()
.filter(date -> date.isAfter(LocalDate.now())
|| date.isEqual(LocalDate.now())).collect(Collectors.toSet());
if (!storedExpirationDates.isEmpty()) {
throw new AllOptionsExpirationDatesNotPresentException(new ArrayList<>(storedExpirationDates));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

import com.dpgrandslam.stockdataservice.domain.model.Holiday;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.time.*;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -58,13 +58,24 @@ public LocalDate getLastTradeDate() {
return now.toLocalDate();
}

public LocalDate getStartDayOfTradeWeek() {
LocalDate now = this.getNowAmericaNewYork().toLocalDate();
while (now.getDayOfWeek() != DayOfWeek.MONDAY) {
now = now.minusDays(1);
public LocalDate getStartDayOfCurrentTradeWeek() {
return getStartDayOfCurrentTradeWeek(0);
}

public LocalDate getStartDayOfCurrentTradeWeek(int weekOffset) {
if (weekOffset < 0) {
throw new IllegalArgumentException("Week offset must be greater than 1");
}
while (isStockMarketHoliday(now)) {
now = now.plusDays(1);
LocalDate now = this.getNowAmericaNewYork().toLocalDate();
for (int i = weekOffset; i >= 0; i--) {
if (i == weekOffset) {
now = now.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
} else {
now = now.with(TemporalAdjusters.previous(DayOfWeek.MONDAY));
}
while (isStockMarketHoliday(now)) {
now = now.plusDays(1);
}
}
if (now.isAfter(LocalDate.now())) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,14 +273,16 @@ public void loadFullOptionsChainWithAllDataBetweenDates_endDateNotToday() throws
public void loadFullOptionsChain_expirationDatesMissing_publishesEvent() throws OptionsChainLoadException {
Set<LocalDate> localDates = new HashSet<>(subject.getOptionExpirationDates("TEST"));
localDates.add(LocalDate.now().plusYears(20));
localDates.add(LocalDate.now().minusDays(1));
localDates.add(LocalDate.now());

when(historicOptionsDataService.getExpirationDatesAtStartDate(anyString(), any())).thenReturn(localDates);

subject.loadFullLiveOptionsChain("TEST");

verify(applicationEventPublisher, times(1)).publishEvent(optionChainParseFailedEventAC.capture());
verify(applicationEventPublisher, times(2)).publishEvent(optionChainParseFailedEventAC.capture());

OptionChainParseFailedEvent optionChainParseFailedEvent = optionChainParseFailedEventAC.getValue();
OptionChainParseFailedEvent optionChainParseFailedEvent = optionChainParseFailedEventAC.getAllValues().get(0);

assertEquals("TEST", optionChainParseFailedEvent.getTicker());
assertEquals(LocalDate.now().plusYears(20), optionChainParseFailedEvent.getExpiration());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,25 @@
import com.dpgrandslam.stockdataservice.domain.util.TimeUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnitRunner;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.temporal.TemporalAdjusters;

import static junit.framework.TestCase.assertFalse;
import static junit.framework.TestCase.assertTrue;
import static junit.framework.TestCase.*;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class TimeUtilsTest {

@Spy
private TimeUtils timeUtils;

@Before
public void setup() {
timeUtils = new TimeUtils();
}

@Test
public void testIsStockMarketHoliday() {
LocalDate holiday = LocalDate.of(2021, 4, 2);
Expand All @@ -28,4 +32,19 @@ public void testIsStockMarketHoliday() {

assertFalse(timeUtils.isStockMarketHoliday(nonHoliday));
}

@Test
public void testGetStartDayOfTradeWeek() {
LocalDate startDate = LocalDate.of(2021, Month.OCTOBER, 29);

when(timeUtils.getNowAmericaNewYork()).thenReturn(startDate.atStartOfDay());

LocalDate actual = timeUtils.getStartDayOfCurrentTradeWeek();

assertEquals(startDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)), actual);

actual = timeUtils.getStartDayOfCurrentTradeWeek(3);

assertEquals(LocalDate.of(2021, Month.OCTOBER, 4), actual);
}
}

0 comments on commit 3a4d5e4

Please sign in to comment.