Skip to content

Commit

Permalink
StoreResolver tests
Browse files Browse the repository at this point in the history
  • Loading branch information
paulcwarren committed May 25, 2020
1 parent e77cd4a commit 64cc93e
Showing 1 changed file with 124 additions and 0 deletions.
124 changes: 124 additions & 0 deletions spring-content-commons/src/test/java/it/StoresIT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package it;

import com.github.paulcwarren.ginkgo4j.Ginkgo4jRunner;
import internal.org.springframework.content.commons.storeservice.StoresImpl;
import org.junit.runner.RunWith;
import org.springframework.content.commons.repository.Store;
import org.springframework.content.commons.repository.factory.StoreFactory;
import org.springframework.content.commons.repository.factory.testsupport.TestContentStore;
import org.springframework.content.commons.repository.factory.testsupport.TestStoreFactoryBean;
import org.springframework.content.commons.storeservice.StoreFilter;
import org.springframework.content.commons.storeservice.StoreInfo;
import org.springframework.content.commons.storeservice.StoreResolver;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import static com.github.paulcwarren.ginkgo4j.Ginkgo4jDSL.*;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.fail;

@RunWith(Ginkgo4jRunner.class)
public class StoresIT {

private StoresImpl stores;

private List<StoreFactory> factories = new ArrayList<>();
private StoreResolver resolver;

{
Describe("#getStore", () -> {

Context("when there are two stores that the filter matches but no store resolver", () -> {

BeforeEach(() -> {
TestStoreFactoryBean factory1 = new TestStoreFactoryBean();
factory1.setStoreInterface(WrongStore.class);
factory1.setBeanClassLoader(this.getClass().getClassLoader());
factories.add(factory1);

TestStoreFactoryBean factory2 = new TestStoreFactoryBean();
factory2.setStoreInterface(RightStore.class);
factory2.setBeanClassLoader(this.getClass().getClassLoader());
factories.add(factory2);
});

JustBeforeEach(() -> {
stores = new StoresImpl();
stores.setFactories(factories);
});

It("should return the right store", () -> {

try {
stores.getStore(Store.class, new StoreFilter() {
@Override
public String name() {
return "test";
}

@Override
public boolean matches(StoreInfo info) {
return true;
}
});
fail("exception not thrown");
} catch (Exception e) {
assertThat(e.getMessage(), containsString("unable to resolve store"));
}
});
});

Context("when there are two stores that the filter matches and a store resolver", () -> {

BeforeEach(() -> {
TestStoreFactoryBean factory1 = new TestStoreFactoryBean();
factory1.setStoreInterface(WrongStore.class);
factory1.setBeanClassLoader(this.getClass().getClassLoader());
factories.add(factory1);

TestStoreFactoryBean factory2 = new TestStoreFactoryBean();
factory2.setStoreInterface(RightStore.class);
factory2.setBeanClassLoader(this.getClass().getClassLoader());
factories.add(factory2);
});

JustBeforeEach(() -> {
stores = new StoresImpl();
stores.setFactories(factories);
stores.addStoreResolver("test", new StoreResolver() {
@Override
public StoreInfo resolve(StoreInfo... stores) {
for (StoreInfo info : stores) {
if (info.getInterface().equals(RightStore.class)) {
return info;
}
}
return null;
}
});
});

It("should return the right store", () -> {
StoreInfo info = stores.getStore(Store.class, new StoreFilter() {
@Override
public String name() {
return "test";
}
@Override
public boolean matches(StoreInfo info) {
return true;
}
});

assertThat(info.getInterface(), is(RightStore.class));
});
});
});
}

public interface RightStore extends TestContentStore<Object, Serializable>{};
public interface WrongStore extends TestContentStore<Object, Serializable>{};
}

0 comments on commit 64cc93e

Please sign in to comment.