use of org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreMappingContext in project spring-cloud-gcp by spring-cloud.
the class PartTreeDatastoreQueryTests method initMocks.
@Before
public void initMocks() {
this.queryMethod = mock(DatastoreQueryMethod.class);
when(this.queryMethod.getReturnedObjectType()).thenReturn((Class) TestEntity.class);
this.datastoreTemplate = mock(DatastoreTemplate.class);
this.datastoreMappingContext = new DatastoreMappingContext();
this.datastoreEntityConverter = mock(DatastoreEntityConverter.class);
this.readWriteConversions = new TwoStepsConversions(new DatastoreCustomConversions(), null, this.datastoreMappingContext);
when(this.datastoreTemplate.getDatastoreEntityConverter()).thenReturn(this.datastoreEntityConverter);
when(this.datastoreEntityConverter.getConversions()).thenReturn(this.readWriteConversions);
}
use of org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreMappingContext in project spring-cloud-gcp by spring-cloud.
the class DatastoreQueryLookupStrategyTests method initMocks.
@Before
public void initMocks() {
this.datastoreTemplate = mock(DatastoreTemplate.class);
this.datastoreMappingContext = new DatastoreMappingContext();
this.queryMethod = mock(DatastoreQueryMethod.class);
this.evaluationContextProvider = mock(QueryMethodEvaluationContextProvider.class);
this.datastoreQueryLookupStrategy = getDatastoreQueryLookupStrategy();
}
use of org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreMappingContext in project spring-cloud-gcp by spring-cloud.
the class DatastoreTemplateTests method setup.
@Before
public void setup() {
this.datastoreTemplate = new DatastoreTemplate(() -> this.datastore, this.datastoreEntityConverter, new DatastoreMappingContext(), this.objectToKeyFactory);
when(this.datastoreEntityConverter.getConversions()).thenReturn(this.readWriteConversions);
// The readWriteConversions are only mocked for purposes of collection-conversion
// for
// descendants. no other conversions take place in the template.
doAnswer((invocation) -> new LinkedList<>(invocation.getArgument(0))).when(this.readWriteConversions).convertOnRead(any(), any(), (Class) any());
this.ob1 = new TestEntity();
this.ob2 = new TestEntity();
this.ob1.id = "value1";
this.ob2.id = "value2";
Entity ce1 = Entity.newBuilder(this.keyChild1).build();
Query childTestEntityQuery = Query.newEntityQueryBuilder().setKind("child_entity").setFilter(PropertyFilter.hasAncestor(this.key1)).build();
this.childEntity1 = createChildEntity();
this.ob1.childEntities = new LinkedList<>();
this.childEntity2 = new ChildEntity();
this.ob1.childEntities.add(this.childEntity2);
this.childEntity3 = new ChildEntity();
this.ob1.childEntities.add(this.childEntity3);
this.childEntity4 = new ChildEntity();
this.ob1.singularReference = this.childEntity4;
this.ob1.multipleReference = new LinkedList<>();
this.childEntity5 = new ChildEntity();
this.ob1.multipleReference.add(this.childEntity5);
this.childEntity6 = new ChildEntity();
this.ob1.multipleReference.add(this.childEntity6);
this.ob1.lazyMultipleReference = new LinkedList<>();
this.childEntity7 = new ChildEntity();
this.ob1.lazyMultipleReference.add(this.childEntity7);
// mocked query results for entities and child entities.
QueryResults childTestEntityQueryResults = mock(QueryResults.class);
doAnswer((invocation) -> {
Arrays.asList(ce1).iterator().forEachRemaining(invocation.getArgument(0));
return null;
}).when(childTestEntityQueryResults).forEachRemaining(any());
QueryResults testEntityQueryResults = mock(QueryResults.class);
doAnswer((invocation) -> {
Arrays.asList(this.e1, this.e2).iterator().forEachRemaining(invocation.getArgument(0));
return null;
}).when(testEntityQueryResults).forEachRemaining(any());
setUpConverters(ce1, childTestEntityQuery, childTestEntityQueryResults, testEntityQueryResults);
}
use of org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreMappingContext in project spring-cloud-gcp by spring-cloud.
the class DatastoreTemplateTests method multipleNamespaceTest.
@Test
public void multipleNamespaceTest() {
Datastore databaseClient1 = mock(Datastore.class);
Datastore databaseClient2 = mock(Datastore.class);
AtomicInteger currentClient = new AtomicInteger(1);
Supplier<Integer> regionProvider = currentClient::getAndIncrement;
// this client selector will alternate between the two clients
ConcurrentHashMap<Integer, Datastore> store = new ConcurrentHashMap<>();
Supplier<Datastore> clientProvider = () -> store.computeIfAbsent(regionProvider.get(), u -> u % 2 == 1 ? databaseClient1 : databaseClient2);
DatastoreTemplate template = new DatastoreTemplate(clientProvider, this.datastoreEntityConverter, new DatastoreMappingContext(), this.objectToKeyFactory);
ChildEntity childEntity = new ChildEntity();
childEntity.id = createFakeKey("key");
when(this.objectToKeyFactory.getKeyFromObject(same(childEntity), any())).thenReturn(childEntity.id);
// this first save should use the first client
template.save(childEntity);
verify(databaseClient1, times(1)).put((FullEntity<?>[]) any());
verify(databaseClient2, times(0)).put((FullEntity<?>[]) any());
// this second save should use the second client
template.save(childEntity);
verify(databaseClient1, times(1)).put((FullEntity<?>[]) any());
verify(databaseClient2, times(1)).put((FullEntity<?>[]) any());
// this third save should use the first client again
template.save(childEntity);
verify(databaseClient1, times(2)).put((FullEntity<?>[]) any());
verify(databaseClient2, times(1)).put((FullEntity<?>[]) any());
}
use of org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreMappingContext in project spring-cloud-gcp by spring-cloud.
the class DefaultDatastoreEntityConverterTests method PrivateCustomMapExceptionTest.
@Test
public void PrivateCustomMapExceptionTest() {
ServiceConfigurationPrivateCustomMap config = new ServiceConfigurationPrivateCustomMap("a", new PrivateCustomMap());
DatastoreEntityConverter entityConverter = new DefaultDatastoreEntityConverter(new DatastoreMappingContext(), new DatastoreServiceObjectToKeyFactory(() -> this.datastore));
Entity.Builder builder = getEntityBuilder();
entityConverter.write(config, builder);
Entity entity = builder.build();
assertThatThrownBy(() -> {
entityConverter.read(ServiceConfigurationPrivateCustomMap.class, entity);
}).isInstanceOf(DatastoreDataException.class).hasMessageContaining("Unable to create an instance of a custom map type: " + "class org.springframework.cloud.gcp.data.datastore.core.convert." + "DefaultDatastoreEntityConverterTests$PrivateCustomMap " + "(make sure the class is public and has a public no-args constructor)");
}
Aggregations