Skip to content

Commit

Permalink
start working on #54
Browse files Browse the repository at this point in the history
  • Loading branch information
OlliL committed May 1, 2024
1 parent 1190fa5 commit 869741e
Show file tree
Hide file tree
Showing 15 changed files with 281 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,15 @@ public ResponseEntity<ListEtfOverviewResponse> listEtfOverview(
final Year year = Year.of(requestYear);
final LocalDateTime endOfMonth = LocalDateTime.of(requestYear.intValue(), month, 1, 23, 59, 59, 999999999)
.with(TemporalAdjusters.lastDayOfMonth());
final UserID userId = super.getUserId();
final ListEtfOverviewResponse response = new ListEtfOverviewResponse();
final List<EtfSummaryTransport> transports = new ArrayList<>();

final List<Etf> etfs = this.etfService.getAllEtf(super.getUserId());
for (final Etf etf : etfs) {
final EtfValue etfValue = this.etfService.getEtfValueEndOfMonth(etf.getIsin(), year, month);
final List<EtfFlow> allEtfFlows = this.etfService.getAllEtfFlowsUntil(etf.getId(), endOfMonth);
final List<EtfFlowWithTaxInfo> etfFlows = this.etfService.calculateEffectiveEtfFlows(allEtfFlows);
final List<EtfFlow> allEtfFlows = this.etfService.getAllEtfFlowsUntil(userId, etf.getId(), endOfMonth);
final List<EtfFlowWithTaxInfo> etfFlows = this.etfService.calculateEffectiveEtfFlows(userId, allEtfFlows);
if (etfFlows != null && !etfFlows.isEmpty()) {
final EtfSummaryTransport transport = new EtfSummaryTransport();
transport.setEtfId(etf.getId().getId());
Expand Down Expand Up @@ -151,12 +152,12 @@ public ResponseEntity<ListEtfFlowsResponse> listEtfFlowsById(@PathVariable("id")

final ListEtfFlowsResponse response = this.getDefaultListEtfFlowsResponse();

final List<EtfFlow> etfFlows = this.etfService.getAllEtfFlowsUntil(etfId, LocalDateTime.now());
final List<EtfFlow> etfFlows = this.etfService.getAllEtfFlowsUntil(userId, etfId, LocalDateTime.now());
final List<EtfFlowTransport> etfFlowTransports = super.mapList(etfFlows, EtfFlowTransport.class);
response.setEtfFlowTransports(etfFlowTransports);

final List<EtfFlowWithTaxInfo> etfEffectiveFlows = new ArrayList<>(
this.etfService.calculateEffectiveEtfFlows(etfFlows));
this.etfService.calculateEffectiveEtfFlows(userId, etfFlows));
Collections.sort(etfEffectiveFlows, Collections.reverseOrder(new EtfFlowComparator()));
final List<EtfEffectiveFlowTransport> etfEffectiveFlowTransports = super.mapList(etfEffectiveFlows,
EtfEffectiveFlowTransport.class);
Expand Down Expand Up @@ -199,6 +200,13 @@ public ResponseEntity<CalcEtfSaleResponse> calcEtfSale(@RequestBody final CalcEt
return ResponseEntity.ok(response);
}

final UserID userId = this.getUserId();
final EtfID etfId = new EtfID(request.getEtfId());

if (this.etfService.getEtfById(userId, etfId) == null) {
return ResponseEntity.ok(response);
}

final BigDecimal pieces = request.getPieces().abs();
final BigDecimal askPrice = request.getAskPrice().abs();
final BigDecimal bidPrice = request.getBidPrice().abs();
Expand All @@ -208,14 +216,13 @@ public ResponseEntity<CalcEtfSaleResponse> calcEtfSale(@RequestBody final CalcEt
this.settingService.setClientCalcEtfSaleTransactionCosts(this.getUserId(),
new ClientCalcEtfSaleTransactionCosts(request.getTransactionCosts()));

final EtfID etfId = new EtfID(request.getEtfId());
BigDecimal openPieces = pieces;
BigDecimal originalBuyPrice = BigDecimal.ZERO;
BigDecimal overallPreliminaryLumpSum = BigDecimal.ZERO;

final List<EtfFlow> etfFlows = this.etfService.getAllEtfFlowsUntil(etfId, LocalDateTime.now());
final List<EtfFlow> etfFlows = this.etfService.getAllEtfFlowsUntil(userId, etfId, LocalDateTime.now());
final List<EtfFlowWithTaxInfo> effectiveEtfFlows = new ArrayList<>(
this.etfService.calculateEffectiveEtfFlows(etfFlows));
this.etfService.calculateEffectiveEtfFlows(userId, etfFlows));

if (effectiveEtfFlows != null && !effectiveEtfFlows.isEmpty()) {
for (final EtfFlowWithTaxInfo etfFlow : effectiveEtfFlows) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import java.util.List;

import org.laladev.moneyjinn.model.access.UserID;
import org.laladev.moneyjinn.model.etf.EtfFlow;
import org.laladev.moneyjinn.model.etf.EtfFlowID;
import org.laladev.moneyjinn.model.validation.ValidationResult;
Expand Down Expand Up @@ -64,13 +65,14 @@ protected void addBeanMapper() {
@Override
public ResponseEntity<EtfFlowTransport> create(@RequestBody final EtfFlowTransport etfFlowTransport,
@RequestHeader(value = HEADER_PREFER, required = false) final List<String> prefer) {
final UserID userId = super.getUserId();
final EtfFlow etfFlow = super.map(etfFlowTransport, EtfFlow.class);
etfFlow.setId(null);
final ValidationResult validationResult = this.etfService.validateEtfFlow(etfFlow);
final ValidationResult validationResult = this.etfService.validateEtfFlow(userId, etfFlow);

this.throwValidationExceptionIfInvalid(validationResult);

final EtfFlowID etfId = this.etfService.createEtfFlow(etfFlow);
final EtfFlowID etfId = this.etfService.createEtfFlow(userId, etfFlow);

etfFlow.setId(etfId);

Expand All @@ -81,21 +83,23 @@ public ResponseEntity<EtfFlowTransport> create(@RequestBody final EtfFlowTranspo
@Override
public ResponseEntity<EtfFlowTransport> update(@RequestBody final EtfFlowTransport etfFlowTransport,
@RequestHeader(value = HEADER_PREFER, required = false) final List<String> prefer) {
final UserID userId = super.getUserId();
final EtfFlow etfFlow = super.map(etfFlowTransport, EtfFlow.class);
final ValidationResult validationResult = this.etfService.validateEtfFlow(etfFlow);
final ValidationResult validationResult = this.etfService.validateEtfFlow(userId, etfFlow);

this.throwValidationExceptionIfInvalid(validationResult);

this.etfService.updateEtfFlow(etfFlow);
this.etfService.updateEtfFlow(userId, etfFlow);

return this.preferedReturn(prefer, etfFlow, EtfFlowTransport.class);
}

@Override
public ResponseEntity<Void> delete(@PathVariable("id") final Long id) {
final UserID userId = super.getUserId();
final EtfFlowID etfFlowId = new EtfFlowID(id);

this.etfService.deleteEtfFlow(etfFlowId);
this.etfService.deleteEtfFlow(userId, etfFlowId);

return ResponseEntity.noContent().build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.time.Year;
import java.util.List;

import org.laladev.moneyjinn.model.access.UserID;
import org.laladev.moneyjinn.model.etf.EtfID;
import org.laladev.moneyjinn.model.etf.EtfPreliminaryLumpSum;
import org.laladev.moneyjinn.model.etf.EtfPreliminaryLumpSumID;
Expand Down Expand Up @@ -65,9 +66,10 @@ protected void addBeanMapper() {

@Override
public ResponseEntity<List<Integer>> readAllYears(@PathVariable("etfId") final Long requestEtfId) {
final UserID userId = super.getUserId();
final EtfID etfId = new EtfID(requestEtfId);

final List<Year> allYears = this.etfService.getAllEtfPreliminaryLumpSumYears(etfId);
final List<Year> allYears = this.etfService.getAllEtfPreliminaryLumpSumYears(userId, etfId);

return allYears.isEmpty() ? ResponseEntity.notFound().build()
: ResponseEntity.ok(allYears.stream().map(Year::getValue).toList());
Expand All @@ -76,12 +78,13 @@ public ResponseEntity<List<Integer>> readAllYears(@PathVariable("etfId") final L
@Override
public ResponseEntity<EtfPreliminaryLumpSumTransport> readOne(@PathVariable("etfId") final Long requestEtfId,
@PathVariable("year") final Integer requestYear) {
final UserID userId = super.getUserId();
final EtfID etfId = new EtfID(requestEtfId);
final Year year = Year.of(requestYear);
final EtfPreliminaryLumpSumID id = new EtfPreliminaryLumpSumID(
new EtfPreliminaryLumpSumIDValues(etfId, year));

final EtfPreliminaryLumpSum etfPreliminaryLumpSum = this.etfService.getEtfPreliminaryLumpSum(id);
final EtfPreliminaryLumpSum etfPreliminaryLumpSum = this.etfService.getEtfPreliminaryLumpSum(userId, id);

return etfPreliminaryLumpSum == null ? ResponseEntity.notFound().build()
: ResponseEntity.ok(super.map(etfPreliminaryLumpSum, EtfPreliminaryLumpSumTransport.class));
Expand All @@ -91,12 +94,14 @@ public ResponseEntity<EtfPreliminaryLumpSumTransport> readOne(@PathVariable("etf
public ResponseEntity<EtfPreliminaryLumpSumTransport> create(
@RequestBody final EtfPreliminaryLumpSumTransport transport,
@RequestHeader(value = HEADER_PREFER, required = false) final List<String> prefer) {
final UserID userId = super.getUserId();
final EtfPreliminaryLumpSum etfPreliminaryLumpSum = super.map(transport, EtfPreliminaryLumpSum.class);
final ValidationResult validationResult = this.etfService.validateEtfPreliminaryLumpSum(etfPreliminaryLumpSum);
final ValidationResult validationResult = this.etfService.validateEtfPreliminaryLumpSum(userId,
etfPreliminaryLumpSum);

this.throwValidationExceptionIfInvalid(validationResult);

this.etfService.createEtfPreliminaryLumpSum(etfPreliminaryLumpSum);
this.etfService.createEtfPreliminaryLumpSum(userId, etfPreliminaryLumpSum);

return this.preferedReturn(prefer, etfPreliminaryLumpSum, EtfPreliminaryLumpSumTransport.class);
}
Expand All @@ -105,25 +110,28 @@ public ResponseEntity<EtfPreliminaryLumpSumTransport> create(
public ResponseEntity<EtfPreliminaryLumpSumTransport> update(
@RequestBody final EtfPreliminaryLumpSumTransport transport,
@RequestHeader(value = HEADER_PREFER, required = false) final List<String> prefer) {
final UserID userId = super.getUserId();
final EtfPreliminaryLumpSum etfPreliminaryLumpSum = super.map(transport, EtfPreliminaryLumpSum.class);
final ValidationResult validationResult = this.etfService.validateEtfPreliminaryLumpSum(etfPreliminaryLumpSum);
final ValidationResult validationResult = this.etfService.validateEtfPreliminaryLumpSum(userId,
etfPreliminaryLumpSum);

this.throwValidationExceptionIfInvalid(validationResult);

this.etfService.updateEtfPreliminaryLumpSum(etfPreliminaryLumpSum);
this.etfService.updateEtfPreliminaryLumpSum(userId, etfPreliminaryLumpSum);

return this.preferedReturn(prefer, etfPreliminaryLumpSum, EtfPreliminaryLumpSumTransport.class);
}

@Override
public ResponseEntity<Void> delete(@PathVariable("etfId") final Long requestEtfId,
@PathVariable("year") final Integer requestYear) {
final UserID userId = super.getUserId();
final EtfID etfId = new EtfID(requestEtfId);
final Year year = Year.of(requestYear);
final EtfPreliminaryLumpSumID id = new EtfPreliminaryLumpSumID(
new EtfPreliminaryLumpSumIDValues(etfId, year));

this.etfService.deleteEtfPreliminaryLumpSum(id);
this.etfService.deleteEtfPreliminaryLumpSum(userId, id);

return ResponseEntity.noContent().build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.laladev.moneyjinn.AbstractTest;
import org.laladev.moneyjinn.model.access.Group;
import org.laladev.moneyjinn.model.access.GroupID;
import org.laladev.moneyjinn.model.access.User;
import org.laladev.moneyjinn.model.access.UserID;
import org.laladev.moneyjinn.model.etf.Etf;
import org.laladev.moneyjinn.model.etf.EtfFlow;
import org.laladev.moneyjinn.model.etf.EtfPreliminaryLumpSum;
import org.laladev.moneyjinn.model.exception.BusinessException;
import org.laladev.moneyjinn.service.api.IEtfService;

Expand All @@ -15,18 +21,54 @@ class EtfServiceTest extends AbstractTest {
private IEtfService etfService;

@Test
void test_createWithInvalidEntity_raisesException() {
void test_createEtfFlowWithInvalidEntity_raisesException() {
final EtfFlow etfFlow = new EtfFlow();
Assertions.assertThrows(BusinessException.class, () -> {
this.etfService.createEtfFlow(etfFlow);
this.etfService.createEtfFlow(new UserID(1L), etfFlow);
});
}

@Test
void test_updateWithInvalidEntity_raisesException() {
void test_updateEtfFlowWithInvalidEntity_raisesException() {
final EtfFlow etfFlow = new EtfFlow();
Assertions.assertThrows(BusinessException.class, () -> {
this.etfService.updateEtfFlow(etfFlow);
this.etfService.updateEtfFlow(new UserID(1L), etfFlow);
});
}

@Test
void test_createEtfWithInvalidEntity_raisesException() {
final Etf etf = new Etf();
etf.setUser(new User(new UserID(1L)));
etf.setGroup(new Group(new GroupID(2L)));
Assertions.assertThrows(BusinessException.class, () -> {
this.etfService.createEtf(etf);
});
}

@Test
void test_updateWithInvalidEntity_raisesException() {
final Etf etf = new Etf();
etf.setUser(new User(new UserID(1L)));
etf.setGroup(new Group(new GroupID(2L)));
Assertions.assertThrows(BusinessException.class, () -> {
this.etfService.updateEtf(etf);
});
}

@Test
void test_createEtfPreliminaryLumpSumWithInvalidEntity_raisesException() {
final EtfPreliminaryLumpSum etfPreliminaryLumpSum = new EtfPreliminaryLumpSum();
Assertions.assertThrows(BusinessException.class, () -> {
this.etfService.createEtfPreliminaryLumpSum(new UserID(1L), etfPreliminaryLumpSum);
});
}

@Test
void test_updateEtfPreliminaryLumpSumWithInvalidEntity_raisesException() {
final EtfPreliminaryLumpSum etfPreliminaryLumpSum = new EtfPreliminaryLumpSum();
Assertions.assertThrows(BusinessException.class, () -> {
this.etfService.updateEtfPreliminaryLumpSum(new UserID(1L), etfPreliminaryLumpSum);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void test_etfWithoutDataSameGroup_Successful() throws Exception {
@Test
void test_etfFromOtherGroup_nothingHappened() throws Exception {
final var etfId = new EtfID(EtfTransportBuilder.ETF_ID_2);
final var userId = new UserID(UserTransportBuilder.USER1_ID);
final var userId = new UserID(UserTransportBuilder.ADMIN_ID);

var etf = this.etfService.getEtfById(userId, etfId);
Assertions.assertNotNull(etf);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.laladev.moneyjinn.core.error.ErrorCode;
import org.laladev.moneyjinn.model.access.UserID;
import org.laladev.moneyjinn.model.etf.EtfFlowID;
import org.laladev.moneyjinn.server.builder.EtfFlowTransportBuilder;
import org.laladev.moneyjinn.server.builder.EtfTransportBuilder;
import org.laladev.moneyjinn.server.builder.UserTransportBuilder;
import org.laladev.moneyjinn.server.builder.ValidationItemTransportBuilder;
import org.laladev.moneyjinn.server.model.EtfFlowTransport;
import org.laladev.moneyjinn.server.model.ValidationItemTransport;
Expand Down Expand Up @@ -45,47 +47,50 @@ private void testError(final EtfFlowTransport transport, final ErrorCode errorCo
void test_standardRequest_Successfull_MinimalReturn() throws Exception {
final EtfFlowTransport transport = new EtfFlowTransportBuilder().forNewFlow().build();

final UserID userId = new UserID(UserTransportBuilder.USER1_ID);
final var etfFlowId = new EtfFlowID(EtfFlowTransportBuilder.NEXT_ID);

var etfFlow = this.etfService.getEtfFlowById(etfFlowId);
var etfFlow = this.etfService.getEtfFlowById(userId, etfFlowId);
Assertions.assertNull(etfFlow);

super.callUsecaseExpect204Minimal(transport);

etfFlow = this.etfService.getEtfFlowById(etfFlowId);
etfFlow = this.etfService.getEtfFlowById(userId, etfFlowId);
Assertions.assertNotNull(etfFlow);
}

@Test
void test_standardRequest_Successfull_RepresentationReturn() throws Exception {
final EtfFlowTransport transport = new EtfFlowTransportBuilder().forNewFlow().build();

final UserID userId = new UserID(UserTransportBuilder.USER1_ID);
final var etfFlowId = new EtfFlowID(EtfFlowTransportBuilder.NEXT_ID);

var etfFlow = this.etfService.getEtfFlowById(etfFlowId);
var etfFlow = this.etfService.getEtfFlowById(userId, etfFlowId);
Assertions.assertNull(etfFlow);

final EtfFlowTransport actualTransport = super.callUsecaseExpect200Representation(transport,
EtfFlowTransport.class);
transport.setEtfflowid(EtfFlowTransportBuilder.NEXT_ID);

Assertions.assertEquals(transport, actualTransport);
etfFlow = this.etfService.getEtfFlowById(etfFlowId);
etfFlow = this.etfService.getEtfFlowById(userId, etfFlowId);
Assertions.assertNotNull(etfFlow);
}

@Test
void test_standardRequest_Successfull_DefaultReturn() throws Exception {
final EtfFlowTransport transport = new EtfFlowTransportBuilder().forNewFlow().build();

final UserID userId = new UserID(UserTransportBuilder.USER1_ID);
final var etfFlowId = new EtfFlowID(EtfFlowTransportBuilder.NEXT_ID);

var etfFlow = this.etfService.getEtfFlowById(etfFlowId);
var etfFlow = this.etfService.getEtfFlowById(userId, etfFlowId);
Assertions.assertNull(etfFlow);

super.callUsecaseExpect204(transport);

etfFlow = this.etfService.getEtfFlowById(etfFlowId);
etfFlow = this.etfService.getEtfFlowById(userId, etfFlowId);
Assertions.assertNotNull(etfFlow);
}

Expand All @@ -94,14 +99,15 @@ void test_nanosecondsNotSet_Successfull_DefaultReturn() throws Exception {
final EtfFlowTransport transport = new EtfFlowTransportBuilder().forNewFlow().build();
transport.setNanoseconds(null);

final UserID userId = new UserID(UserTransportBuilder.USER1_ID);
final var etfFlowId = new EtfFlowID(EtfFlowTransportBuilder.NEXT_ID);

var etfFlow = this.etfService.getEtfFlowById(etfFlowId);
var etfFlow = this.etfService.getEtfFlowById(userId, etfFlowId);
Assertions.assertNull(etfFlow);

super.callUsecaseExpect204(transport);

etfFlow = this.etfService.getEtfFlowById(etfFlowId);
etfFlow = this.etfService.getEtfFlowById(userId, etfFlowId);
Assertions.assertNotNull(etfFlow);
}

Expand Down
Loading

0 comments on commit 869741e

Please sign in to comment.