Search in sources :

Example 6 with DatastoreMappingContext

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);
}
Also used : TestEntity(org.springframework.cloud.gcp.data.datastore.it.TestEntity) TwoStepsConversions(org.springframework.cloud.gcp.data.datastore.core.convert.TwoStepsConversions) DatastoreMappingContext(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreMappingContext) DatastoreTemplate(org.springframework.cloud.gcp.data.datastore.core.DatastoreTemplate) DatastoreCustomConversions(org.springframework.cloud.gcp.data.datastore.core.convert.DatastoreCustomConversions) DatastoreEntityConverter(org.springframework.cloud.gcp.data.datastore.core.convert.DatastoreEntityConverter) Before(org.junit.Before)

Example 7 with DatastoreMappingContext

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();
}
Also used : DatastoreMappingContext(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreMappingContext) DatastoreTemplate(org.springframework.cloud.gcp.data.datastore.core.DatastoreTemplate) QueryMethodEvaluationContextProvider(org.springframework.data.repository.query.QueryMethodEvaluationContextProvider) Before(org.junit.Before)

Example 8 with DatastoreMappingContext

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);
}
Also used : FullEntity(com.google.cloud.datastore.FullEntity) Entity(com.google.cloud.datastore.Entity) DatastoreMappingContext(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreMappingContext) Query(com.google.cloud.datastore.Query) KeyQuery(com.google.cloud.datastore.KeyQuery) GqlQuery(com.google.cloud.datastore.GqlQuery) StructuredQuery(com.google.cloud.datastore.StructuredQuery) EntityQuery(com.google.cloud.datastore.EntityQuery) QueryResults(com.google.cloud.datastore.QueryResults) Before(org.junit.Before)

Example 9 with DatastoreMappingContext

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());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Datastore(com.google.cloud.datastore.Datastore) DatastoreMappingContext(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreMappingContext) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) FullEntity(com.google.cloud.datastore.FullEntity) Test(org.junit.Test)

Example 10 with DatastoreMappingContext

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)");
}
Also used : FullEntity(com.google.cloud.datastore.FullEntity) Entity(com.google.cloud.datastore.Entity) EmbeddedEntity(org.springframework.cloud.gcp.data.datastore.core.convert.TestItemWithEmbeddedEntity.EmbeddedEntity) BaseEntity(com.google.cloud.datastore.BaseEntity) DatastoreMappingContext(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreMappingContext) DatastoreDataException(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreDataException) Test(org.junit.Test)

Aggregations

DatastoreMappingContext (org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreMappingContext)14 FullEntity (com.google.cloud.datastore.FullEntity)10 Test (org.junit.Test)10 Entity (com.google.cloud.datastore.Entity)9 BaseEntity (com.google.cloud.datastore.BaseEntity)8 EmbeddedEntity (org.springframework.cloud.gcp.data.datastore.core.convert.TestItemWithEmbeddedEntity.EmbeddedEntity)8 EntityValue (com.google.cloud.datastore.EntityValue)5 ListValue (com.google.cloud.datastore.ListValue)5 NullValue (com.google.cloud.datastore.NullValue)4 StringValue (com.google.cloud.datastore.StringValue)4 Value (com.google.cloud.datastore.Value)4 Before (org.junit.Before)4 DiscriminatorValue (org.springframework.cloud.gcp.data.datastore.core.mapping.DiscriminatorValue)4 DatastoreTemplate (org.springframework.cloud.gcp.data.datastore.core.DatastoreTemplate)3 EntityQuery (com.google.cloud.datastore.EntityQuery)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 MapBuilder (org.springframework.cloud.gcp.core.util.MapBuilder)2 Datastore (com.google.cloud.datastore.Datastore)1 GqlQuery (com.google.cloud.datastore.GqlQuery)1