-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CQRS 4.5 - TransitAnalyzer - migracja danych
- Loading branch information
Showing
4 changed files
with
107 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/main/java/io/legacyfighter/cabs/transitanalyzer/PopulateGraphService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package io.legacyfighter.cabs.transitanalyzer; | ||
|
||
|
||
import io.legacyfighter.cabs.entity.Transit; | ||
import io.legacyfighter.cabs.repository.TransitRepository; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
|
||
import static io.legacyfighter.cabs.entity.Transit.Status.COMPLETED; | ||
|
||
public class PopulateGraphService { | ||
|
||
private final TransitRepository transitRepository; | ||
private final GraphTransitAnalyzer graphTransitAnalyzer; | ||
|
||
public PopulateGraphService(TransitRepository transitRepository, GraphTransitAnalyzer graphTransitAnalyzer) { | ||
this.transitRepository = transitRepository; | ||
this.graphTransitAnalyzer = graphTransitAnalyzer; | ||
} | ||
|
||
@Transactional | ||
public void populate() { | ||
transitRepository | ||
.findAllByStatus(COMPLETED) | ||
.forEach(this::addToGraph); | ||
} | ||
|
||
private void addToGraph(Transit transit) { | ||
Long clientId = transit.getClient().getId(); | ||
graphTransitAnalyzer.addTransitBetweenAddresses( | ||
clientId, | ||
transit.getId(), | ||
transit.getFrom().getHash(), | ||
transit.getTo().getHash(), | ||
transit.getStarted(), | ||
transit.getCompleteAt()); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
src/test/java/io/legacyfighter/cabs/integration/PopulateGraphServiceIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package io.legacyfighter.cabs.integration; | ||
|
||
import io.legacyfighter.cabs.common.Fixtures; | ||
import io.legacyfighter.cabs.common.TestWithGraphDB; | ||
import io.legacyfighter.cabs.entity.Address; | ||
import io.legacyfighter.cabs.entity.Client; | ||
import io.legacyfighter.cabs.entity.Driver; | ||
import io.legacyfighter.cabs.repository.TransitRepository; | ||
import io.legacyfighter.cabs.transitanalyzer.GraphTransitAnalyzer; | ||
import io.legacyfighter.cabs.transitanalyzer.PopulateGraphService; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
import java.util.List; | ||
|
||
import static java.time.Instant.now; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class PopulateGraphServiceIntegrationTest extends TestWithGraphDB { | ||
|
||
@Autowired | ||
Fixtures fixtures; | ||
|
||
@Autowired | ||
TransitRepository transitRepository; | ||
|
||
@Autowired | ||
PopulateGraphService populateGraphService; | ||
|
||
@Autowired | ||
GraphTransitAnalyzer analyzer; | ||
|
||
@Test | ||
void canPopulateGraphWithDataFromRelationalDB() { | ||
//given | ||
Client client = fixtures.aClient(); | ||
//and | ||
Driver driver = fixtures.aDriver(); | ||
//and | ||
Address address1 = new Address("100_1", "1", "1", "1", 1); | ||
Address address2 = new Address("100_2", "2", "2", "2", 2); | ||
Address address3 = new Address("100_3", "3", "3", "3", 3); | ||
Address address4 = new Address("100_4", "4", "4", "4", 3); | ||
//and | ||
fixtures.aRequestedAndCompletedTransit(10, now(), now(), client, driver, address1, address2); | ||
fixtures.aRequestedAndCompletedTransit(10, now(), now(), client, driver, address2, address3); | ||
fixtures.aRequestedAndCompletedTransit(10, now(), now(), client, driver, address3, address4); | ||
|
||
//when | ||
populateGraphService.populate(); | ||
|
||
//then | ||
List<Long> result = analyzer.analyze(client.getId(), address1.getHash()); | ||
assertThat(result).containsExactly( | ||
address1.getHash().longValue(), | ||
address2.getHash().longValue(), | ||
address3.getHash().longValue(), | ||
address4.getHash().longValue()); | ||
} | ||
} |