Skip to content

Commit

Permalink
CQRS 4.5 - TransitAnalyzer - migracja danych
Browse files Browse the repository at this point in the history
  • Loading branch information
pilloPl committed Mar 2, 2022
1 parent e8f2ad3 commit 792218d
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/main/java/io/legacyfighter/cabs/config/Neo4jConfig.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.legacyfighter.cabs.config;

import io.legacyfighter.cabs.repository.TransitRepository;
import io.legacyfighter.cabs.transitanalyzer.GraphTransitAnalyzer;
import io.legacyfighter.cabs.transitanalyzer.PopulateGraphService;
import org.neo4j.graphdb.GraphDatabaseService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
Expand All @@ -23,6 +25,9 @@ GraphDatabaseService notConnectedOnProdYet(String dbPath) {
return null;
}


@Bean
PopulateGraphService populateGraphService(TransitRepository transitRepository, GraphTransitAnalyzer graphTransitAnalyzer) {
return new PopulateGraphService(transitRepository, graphTransitAnalyzer);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public interface TransitRepository extends JpaRepository<Transit, Long> {

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

List<Transit> findAllByStatus(Transit.Status status);

List<Transit> findAllByClientAndFromAndPublishedAfterAndStatusOrderByDateTimeDesc(Client client, Address from, Instant when, Transit.Status status);

List<Transit> findByClient(Client client);
Expand Down
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());
}
}
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());
}
}

0 comments on commit 792218d

Please sign in to comment.