mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-19 14:05:50 +00:00
Added Consistency Checking Mechanism
Consistency checker was added wih tester
This commit is contained in:
parent
ed62231e88
commit
f4796d5d57
3 changed files with 59 additions and 13 deletions
|
@ -5,6 +5,7 @@ package org.springframework.samples.petclinic.newDataStore;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.Iterator;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import org.springframework.samples.petclinic.owner.Owner;
|
import org.springframework.samples.petclinic.owner.Owner;
|
||||||
import org.springframework.samples.petclinic.owner.OwnerRepository;
|
import org.springframework.samples.petclinic.owner.OwnerRepository;
|
||||||
|
@ -17,19 +18,13 @@ public class NewOwnerStore {
|
||||||
|
|
||||||
public Map<Integer, StaticOwner> ownerStore;
|
public Map<Integer, StaticOwner> ownerStore;
|
||||||
private final OwnerRepository owners;
|
private final OwnerRepository owners;
|
||||||
|
private Collection<Owner> ownerRepositoryData;
|
||||||
|
|
||||||
public NewOwnerStore(OwnerRepository owners) {
|
public NewOwnerStore(OwnerRepository owners) {
|
||||||
this.owners = owners;
|
this.owners = owners;
|
||||||
this.ownerStore = new HashMap<>();
|
this.ownerStore = new HashMap<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void populateStore() {
|
|
||||||
Collection<Owner> ownerRepositoryData = this.owners.findAll();
|
|
||||||
for(Owner owner : ownerRepositoryData) {
|
|
||||||
ownerStore.put(owner.getId(), convertToStaticOwner(owner));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public StaticOwner convertToStaticOwner(Owner ownerEntity) {
|
public StaticOwner convertToStaticOwner(Owner ownerEntity) {
|
||||||
return new StaticOwner(ownerEntity.getAddress(), ownerEntity.getCity(), ownerEntity.getTelephone());
|
return new StaticOwner(ownerEntity.getAddress(), ownerEntity.getCity(), ownerEntity.getTelephone());
|
||||||
}
|
}
|
||||||
|
@ -37,4 +32,37 @@ public class NewOwnerStore {
|
||||||
public Map<Integer, StaticOwner> getNewOwnerStore() {
|
public Map<Integer, StaticOwner> getNewOwnerStore() {
|
||||||
return this.ownerStore;
|
return this.ownerStore;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void forklift() {
|
||||||
|
Collection<Owner> ownerRepositoryData = this.owners.findAll();
|
||||||
|
for(Owner owner : ownerRepositoryData) {
|
||||||
|
ownerStore.put(owner.getId(), convertToStaticOwner(owner));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public int checkConsistency() {
|
||||||
|
int inconsistencies = 0;
|
||||||
|
|
||||||
|
ownerRepositoryData = this.owners.findAll();
|
||||||
|
|
||||||
|
Iterator<Owner> iterator = ownerRepositoryData.iterator();
|
||||||
|
|
||||||
|
for (Integer id: ownerStore.keySet()){
|
||||||
|
|
||||||
|
if(iterator.hasNext()) {
|
||||||
|
Owner oldOwner = iterator.next();
|
||||||
|
if(id != oldOwner.getId() || !ownerStore.get(id).equals(oldOwner)) {
|
||||||
|
inconsistencies++;
|
||||||
|
violation(id, convertToStaticOwner(oldOwner), ownerStore.get(id));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return inconsistencies;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void violation(int i, StaticOwner expected, StaticOwner actual)
|
||||||
|
{
|
||||||
|
System.out.println("Consistency Violation! \n” + “i = ” + i + “\n\t expected = " +
|
||||||
|
expected.toString() + "\n\t actual = " + actual.toString());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,4 +30,13 @@ public class StaticOwner {
|
||||||
return telephone;
|
return telephone;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean equals (Owner ownerToCompareTo) {
|
||||||
|
if((this.address != ownerToCompareTo.getAddress())|| (this.city != ownerToCompareTo.getCity()) ||
|
||||||
|
(this.telephone != ownerToCompareTo.getTelephone())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ package org.springframework.samples.petclinic.newDataStore;
|
||||||
|
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
@ -25,11 +26,17 @@ public class NewOwnerStoreTest {
|
||||||
|
|
||||||
NewOwnerStore testOwnerStore;
|
NewOwnerStore testOwnerStore;
|
||||||
|
|
||||||
@Test
|
Map<Integer, StaticOwner> ownerStore;
|
||||||
public void testPopulation() {
|
|
||||||
|
@Before
|
||||||
|
public void setup() {
|
||||||
testOwnerStore = new NewOwnerStore(owner);
|
testOwnerStore = new NewOwnerStore(owner);
|
||||||
testOwnerStore.populateStore();
|
testOwnerStore.forklift();
|
||||||
Map<Integer, StaticOwner> ownerStore = testOwnerStore.getNewOwnerStore();
|
ownerStore = testOwnerStore.getNewOwnerStore();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testForklift() {
|
||||||
|
|
||||||
for (Integer id: ownerStore.keySet()){
|
for (Integer id: ownerStore.keySet()){
|
||||||
|
|
||||||
|
@ -37,8 +44,10 @@ public class NewOwnerStoreTest {
|
||||||
String value = ownerStore.get(id).getAddress();
|
String value = ownerStore.get(id).getAddress();
|
||||||
System.out.println(key + " " + value);
|
System.out.println(key + " " + value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void consistencyCheck () {
|
||||||
|
System.out.println(testOwnerStore.checkConsistency());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue