-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added BE validation when creating or updating dependency #54
1. For circular dependencies 2. For dependency with same source and target (self cycle) 3. For invalid situations / inputs 4. Unit tests for all the stuff
- Loading branch information
tillias
committed
Oct 26, 2020
1 parent
ba8ccae
commit 58af8bd
Showing
11 changed files
with
691 additions
and
215 deletions.
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
122 changes: 122 additions & 0 deletions
122
src/main/java/com/github/microcatalog/service/custom/DependencyService.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,122 @@ | ||
package com.github.microcatalog.service.custom; | ||
|
||
import com.github.microcatalog.domain.Dependency; | ||
import com.github.microcatalog.domain.Microservice; | ||
import com.github.microcatalog.repository.DependencyRepository; | ||
import com.github.microcatalog.service.custom.exceptions.CircularDependenciesException; | ||
import com.github.microcatalog.service.custom.exceptions.SelfCircularException; | ||
import com.github.microcatalog.web.rest.errors.BadRequestAlertException; | ||
import org.jgrapht.Graph; | ||
import org.jgrapht.alg.cycle.CycleDetector; | ||
import org.jgrapht.graph.DefaultEdge; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.stereotype.Service; | ||
|
||
import javax.transaction.Transactional; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
|
||
@Service | ||
@Transactional | ||
public class DependencyService { | ||
|
||
private final Logger log = LoggerFactory.getLogger(DependencyService.class); | ||
|
||
private final GraphLoaderService graphLoaderService; | ||
private final DependencyRepository repository; | ||
|
||
public DependencyService(GraphLoaderService graphLoaderService, DependencyRepository repository) { | ||
this.graphLoaderService = graphLoaderService; | ||
this.repository = repository; | ||
} | ||
|
||
public Dependency create(final Dependency dependency) { | ||
if (dependency.getId() != null) { | ||
throw new BadRequestAlertException("A new dependency cannot already have an ID", getEntityName(), "idexists"); | ||
} | ||
|
||
validateSelfCycle(dependency); | ||
validateIfAdded(dependency); | ||
|
||
return repository.save(dependency); | ||
} | ||
|
||
public Dependency update(final Dependency dependency) { | ||
if (dependency.getId() == null) { | ||
throw new BadRequestAlertException("Invalid id", getEntityName(), "idnull"); | ||
} | ||
|
||
validateSelfCycle(dependency); | ||
|
||
// TODO | ||
// FIXME | ||
// We need second method which updates existing instead of adding new one! | ||
validateIfUpdated(dependency); | ||
|
||
return repository.save(dependency); | ||
} | ||
|
||
public List<Dependency> findAll() { | ||
return repository.findAll(); | ||
} | ||
|
||
public Optional<Dependency> findById(Long id) { | ||
return repository.findById(id); | ||
} | ||
|
||
public void deleteById(Long id) { | ||
repository.deleteById(id); | ||
} | ||
|
||
private String getEntityName() { | ||
return Dependency.class.getSimpleName().toLowerCase(); | ||
} | ||
|
||
private void validateSelfCycle(final Dependency dependency) { | ||
if (dependency == null) { | ||
return; | ||
} | ||
|
||
if (dependency.getSource() == null || dependency.getTarget() == null) { | ||
return; | ||
} | ||
|
||
final Long sourceId = dependency.getSource().getId(); | ||
if (Objects.equals(sourceId, dependency.getTarget().getId())) { | ||
throw new SelfCircularException(String.format("Source id is the same as target id: %s", sourceId)); | ||
} | ||
} | ||
|
||
private void validateIfAdded(final Dependency toBeAdded) { | ||
final Graph<Microservice, DefaultEdge> graph = graphLoaderService.loadGraph(); | ||
graph.addEdge(toBeAdded.getSource(), toBeAdded.getTarget()); | ||
|
||
checkCycles(graph); | ||
} | ||
|
||
private void validateIfUpdated(final Dependency dependency) { | ||
final Graph<Microservice, DefaultEdge> graph = graphLoaderService.loadGraph(); | ||
final Dependency persistent = repository.findById(dependency.getId()) | ||
.orElseThrow(() -> new IllegalArgumentException("Trying to update dependency, but it was removed from data source")); | ||
|
||
// check what will be if we remove existing edge and replace it with updated one | ||
|
||
final DefaultEdge currentEdge = graph.getEdge(persistent.getSource(), persistent.getTarget()); | ||
graph.removeEdge(currentEdge); | ||
|
||
graph.addEdge(dependency.getSource(), dependency.getTarget()); | ||
|
||
checkCycles(graph); | ||
} | ||
|
||
private void checkCycles(final Graph<Microservice, DefaultEdge> graph) { | ||
final CycleDetector<Microservice, DefaultEdge> cycleDetector = new CycleDetector<>(graph); | ||
if (cycleDetector.detectCycles()) { | ||
final Set<Microservice> cycles = cycleDetector.findCycles(); | ||
throw new CircularDependenciesException("Circular dependency will be introduced.", cycles); | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/com/github/microcatalog/service/custom/GraphLoaderService.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,42 @@ | ||
package com.github.microcatalog.service.custom; | ||
|
||
import com.github.microcatalog.domain.Dependency; | ||
import com.github.microcatalog.domain.Microservice; | ||
import com.github.microcatalog.repository.DependencyRepository; | ||
import com.github.microcatalog.repository.MicroserviceRepository; | ||
import org.jgrapht.Graph; | ||
import org.jgrapht.graph.DefaultDirectedGraph; | ||
import org.jgrapht.graph.DefaultEdge; | ||
import org.springframework.stereotype.Service; | ||
|
||
import javax.transaction.Transactional; | ||
import java.util.List; | ||
|
||
@Service | ||
@Transactional | ||
public class GraphLoaderService { | ||
|
||
private final MicroserviceRepository microserviceRepository; | ||
private final DependencyRepository dependencyRepository; | ||
|
||
public GraphLoaderService(MicroserviceRepository microserviceRepository, DependencyRepository dependencyRepository) { | ||
this.microserviceRepository = microserviceRepository; | ||
this.dependencyRepository = dependencyRepository; | ||
} | ||
|
||
public Graph<Microservice, DefaultEdge> loadGraph() { | ||
List<Microservice> microservices = microserviceRepository.findAll(); | ||
List<Dependency> dependencies = dependencyRepository.findAll(); | ||
|
||
return createGraph(microservices, dependencies); | ||
} | ||
|
||
private Graph<Microservice, DefaultEdge> createGraph(final List<Microservice> nodes, final List<Dependency> edges) { | ||
final Graph<Microservice, DefaultEdge> graph = new DefaultDirectedGraph<>(DefaultEdge.class); | ||
|
||
nodes.forEach(graph::addVertex); | ||
edges.forEach(d -> graph.addEdge(d.getSource(), d.getTarget())); | ||
|
||
return graph; | ||
} | ||
} |
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
22 changes: 22 additions & 0 deletions
22
...java/com/github/microcatalog/service/custom/exceptions/CircularDependenciesException.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,22 @@ | ||
package com.github.microcatalog.service.custom.exceptions; | ||
|
||
import com.github.microcatalog.domain.Microservice; | ||
|
||
import java.util.Set; | ||
|
||
/** | ||
* Occurs when adding new dependency will introduce cycle | ||
*/ | ||
public class CircularDependenciesException extends RuntimeException { | ||
private final Set<Microservice> cycles; | ||
|
||
public CircularDependenciesException(String message, Set<Microservice> cycles) { | ||
super(message); | ||
|
||
this.cycles = cycles; | ||
} | ||
|
||
public Set<Microservice> getCycles() { | ||
return cycles; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/github/microcatalog/service/custom/exceptions/SelfCircularException.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,10 @@ | ||
package com.github.microcatalog.service.custom.exceptions; | ||
|
||
/** | ||
* Occurs when there is attempt creating dependency with source and target pointing to the same microservice | ||
*/ | ||
public class SelfCircularException extends RuntimeException { | ||
public SelfCircularException(String message) { | ||
super(message); | ||
} | ||
} |
Oops, something went wrong.