Skip to content

Commit 792218d

Browse files
committed
CQRS 4.5 - TransitAnalyzer - migracja danych
1 parent e8f2ad3 commit 792218d

File tree

4 files changed

+107
-1
lines changed

4 files changed

+107
-1
lines changed

src/main/java/io/legacyfighter/cabs/config/Neo4jConfig.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package io.legacyfighter.cabs.config;
22

3+
import io.legacyfighter.cabs.repository.TransitRepository;
34
import io.legacyfighter.cabs.transitanalyzer.GraphTransitAnalyzer;
5+
import io.legacyfighter.cabs.transitanalyzer.PopulateGraphService;
46
import org.neo4j.graphdb.GraphDatabaseService;
57
import org.springframework.beans.factory.annotation.Value;
68
import org.springframework.context.annotation.Bean;
@@ -23,6 +25,9 @@ GraphDatabaseService notConnectedOnProdYet(String dbPath) {
2325
return null;
2426
}
2527

26-
28+
@Bean
29+
PopulateGraphService populateGraphService(TransitRepository transitRepository, GraphTransitAnalyzer graphTransitAnalyzer) {
30+
return new PopulateGraphService(transitRepository, graphTransitAnalyzer);
31+
}
2732
}
2833

src/main/java/io/legacyfighter/cabs/repository/TransitRepository.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ public interface TransitRepository extends JpaRepository<Transit, Long> {
1515

1616
List<Transit> findAllByClientAndFromAndStatusOrderByDateTimeDesc(Client client, Address from, Transit.Status status);
1717

18+
List<Transit> findAllByStatus(Transit.Status status);
19+
1820
List<Transit> findAllByClientAndFromAndPublishedAfterAndStatusOrderByDateTimeDesc(Client client, Address from, Instant when, Transit.Status status);
1921

2022
List<Transit> findByClient(Client client);
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package io.legacyfighter.cabs.transitanalyzer;
2+
3+
4+
import io.legacyfighter.cabs.entity.Transit;
5+
import io.legacyfighter.cabs.repository.TransitRepository;
6+
import org.springframework.transaction.annotation.Transactional;
7+
8+
9+
import static io.legacyfighter.cabs.entity.Transit.Status.COMPLETED;
10+
11+
public class PopulateGraphService {
12+
13+
private final TransitRepository transitRepository;
14+
private final GraphTransitAnalyzer graphTransitAnalyzer;
15+
16+
public PopulateGraphService(TransitRepository transitRepository, GraphTransitAnalyzer graphTransitAnalyzer) {
17+
this.transitRepository = transitRepository;
18+
this.graphTransitAnalyzer = graphTransitAnalyzer;
19+
}
20+
21+
@Transactional
22+
public void populate() {
23+
transitRepository
24+
.findAllByStatus(COMPLETED)
25+
.forEach(this::addToGraph);
26+
}
27+
28+
private void addToGraph(Transit transit) {
29+
Long clientId = transit.getClient().getId();
30+
graphTransitAnalyzer.addTransitBetweenAddresses(
31+
clientId,
32+
transit.getId(),
33+
transit.getFrom().getHash(),
34+
transit.getTo().getHash(),
35+
transit.getStarted(),
36+
transit.getCompleteAt());
37+
}
38+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package io.legacyfighter.cabs.integration;
2+
3+
import io.legacyfighter.cabs.common.Fixtures;
4+
import io.legacyfighter.cabs.common.TestWithGraphDB;
5+
import io.legacyfighter.cabs.entity.Address;
6+
import io.legacyfighter.cabs.entity.Client;
7+
import io.legacyfighter.cabs.entity.Driver;
8+
import io.legacyfighter.cabs.repository.TransitRepository;
9+
import io.legacyfighter.cabs.transitanalyzer.GraphTransitAnalyzer;
10+
import io.legacyfighter.cabs.transitanalyzer.PopulateGraphService;
11+
import org.junit.jupiter.api.Test;
12+
import org.springframework.beans.factory.annotation.Autowired;
13+
14+
import java.util.List;
15+
16+
import static java.time.Instant.now;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
20+
class PopulateGraphServiceIntegrationTest extends TestWithGraphDB {
21+
22+
@Autowired
23+
Fixtures fixtures;
24+
25+
@Autowired
26+
TransitRepository transitRepository;
27+
28+
@Autowired
29+
PopulateGraphService populateGraphService;
30+
31+
@Autowired
32+
GraphTransitAnalyzer analyzer;
33+
34+
@Test
35+
void canPopulateGraphWithDataFromRelationalDB() {
36+
//given
37+
Client client = fixtures.aClient();
38+
//and
39+
Driver driver = fixtures.aDriver();
40+
//and
41+
Address address1 = new Address("100_1", "1", "1", "1", 1);
42+
Address address2 = new Address("100_2", "2", "2", "2", 2);
43+
Address address3 = new Address("100_3", "3", "3", "3", 3);
44+
Address address4 = new Address("100_4", "4", "4", "4", 3);
45+
//and
46+
fixtures.aRequestedAndCompletedTransit(10, now(), now(), client, driver, address1, address2);
47+
fixtures.aRequestedAndCompletedTransit(10, now(), now(), client, driver, address2, address3);
48+
fixtures.aRequestedAndCompletedTransit(10, now(), now(), client, driver, address3, address4);
49+
50+
//when
51+
populateGraphService.populate();
52+
53+
//then
54+
List<Long> result = analyzer.analyze(client.getId(), address1.getHash());
55+
assertThat(result).containsExactly(
56+
address1.getHash().longValue(),
57+
address2.getHash().longValue(),
58+
address3.getHash().longValue(),
59+
address4.getHash().longValue());
60+
}
61+
}

0 commit comments

Comments
 (0)