Merge Conflicts Addressed

This commit is contained in:
EulPi 2018-04-02 23:27:21 -04:00
commit 2562a955ae
3 changed files with 66 additions and 9 deletions

View file

@ -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,8 +18,12 @@ public class NewOwnerStore {
private HashMap<Integer, StaticOwner> ownerStore; private HashMap<Integer, StaticOwner> ownerStore;
private final OwnerRepository owners; private final OwnerRepository owners;
private static NewOwnerStore storeSingleton; private static NewOwnerStore storeSingleton;
private Collection<Owner> ownerRepositoryData;
private NewOwnerStore(OwnerRepository owners) { private NewOwnerStore(OwnerRepository owners) {
this.owners = owners; this.owners = owners;
this.ownerStore = new HashMap<>(); this.ownerStore = new HashMap<>();
@ -32,7 +37,9 @@ public class NewOwnerStore {
return storeSingleton; return storeSingleton;
} }
public void populateStore() {
public void forklift() {
Collection<Owner> ownerRepositoryData = this.owners.findAll(); Collection<Owner> ownerRepositoryData = this.owners.findAll();
for(Owner owner : ownerRepositoryData) { for(Owner owner : ownerRepositoryData) {
ownerStore.put(owner.getId(), StaticOwner.convertToStaticOwner(owner)); ownerStore.put(owner.getId(), StaticOwner.convertToStaticOwner(owner));
@ -55,8 +62,45 @@ public class NewOwnerStore {
// Replace // Replace
getStore().put(owner.getId(), StaticOwner.convertToStaticOwner(owner)); getStore().put(owner.getId(), StaticOwner.convertToStaticOwner(owner));
}
// Report whether inexistent or inconsistent based on exists // Report whether inexistent or inconsistent based on exists
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, StaticOwner.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());
}
public void save(Owner owner){
//actual write to datastore
owners.save(owner);
//shadow write to new datastore
ownerStore.put(owner.getId(), StaticOwner.convertToStaticOwner(owner));
}
//this is for testing to introduce inconsistencies
public void testPutInOldDatastoreOnly(Owner owner){
owners.save(owner);
} }
} }

View file

@ -94,7 +94,6 @@ public class StaticOwner {
return telephone; return telephone;
} }
public static StaticOwner convertToStaticOwner(Owner owner) { public static StaticOwner convertToStaticOwner(Owner owner) {
return new StaticOwner(owner.getId(), owner.getLastName(), owner.getFirstName(), return new StaticOwner(owner.getId(), owner.getLastName(), owner.getFirstName(),
owner.getAddress(), owner.getCity(), owner.getTelephone()); owner.getAddress(), owner.getCity(), owner.getTelephone());

View file

@ -3,9 +3,14 @@
*/ */
package org.springframework.samples.petclinic.newDataStore; package org.springframework.samples.petclinic.newDataStore;
import java.util.*; import java.util.*;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.Iterator;
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;
@ -29,12 +34,18 @@ public class NewOwnerStoreTest {
NewOwnerStore testOwnerStore; NewOwnerStore testOwnerStore;
@Test HashMap<Integer, StaticOwner> ownerStore;
public void testPopulation() {
testOwnerStore = NewOwnerStore.getInstance(owners);
testOwnerStore.populateStore();
Map<Integer, StaticOwner> ownerStore = testOwnerStore.getStore();
@Before
public void setup() {
testOwnerStore = NewOwnerStore.getInstance(owners);
testOwnerStore.forklift();
ownerStore = testOwnerStore.getStore();
}
@Test
public void testForklift() {
for (Integer id: ownerStore.keySet()){ for (Integer id: ownerStore.keySet()){
Integer key = id; Integer key = id;
@ -46,7 +57,7 @@ public class NewOwnerStoreTest {
@Test @Test
public void testShadowRead() { public void testShadowRead() {
testOwnerStore = NewOwnerStore.getInstance(owners); testOwnerStore = NewOwnerStore.getInstance(owners);
testOwnerStore.populateStore(); testOwnerStore.forklift();
Collection<Owner> results = this.owners.findByLastName(""); Collection<Owner> results = this.owners.findByLastName("");
@ -95,5 +106,8 @@ public class NewOwnerStoreTest {
return inconsistencies; return inconsistencies;
} }
@Test
public void consistencyCheck () {
System.out.println(testOwnerStore.checkConsistency());
}
} }