Search in sources :

Example 1 with GcpProjectIdProvider

use of com.google.cloud.spring.core.GcpProjectIdProvider in project spring-cloud-gcp by GoogleCloudPlatform.

the class GcpContextAutoConfigurationTests method testGetProjectIdProvider_withGcpProperties.

@Test
void testGetProjectIdProvider_withGcpProperties() {
    this.contextRunner.withPropertyValues("spring.cloud.gcp.projectId=tonberry").run(context -> {
        GcpProjectIdProvider projectIdProvider = context.getBean(GcpProjectIdProvider.class);
        assertThat(projectIdProvider.getProjectId()).isEqualTo("tonberry");
    });
}
Also used : DefaultGcpProjectIdProvider(com.google.cloud.spring.core.DefaultGcpProjectIdProvider) GcpProjectIdProvider(com.google.cloud.spring.core.GcpProjectIdProvider) Test(org.junit.jupiter.api.Test)

Example 2 with GcpProjectIdProvider

use of com.google.cloud.spring.core.GcpProjectIdProvider in project spring-cloud-gcp by GoogleCloudPlatform.

the class GcpContextAutoConfigurationTests method testGetProjectIdProvider_withoutGcpProperties.

@Test
void testGetProjectIdProvider_withoutGcpProperties() {
    this.contextRunner.run(context -> {
        GcpProjectIdProvider projectIdProvider = context.getBean(GcpProjectIdProvider.class);
        assertThat(projectIdProvider).isInstanceOf(DefaultGcpProjectIdProvider.class);
    });
}
Also used : DefaultGcpProjectIdProvider(com.google.cloud.spring.core.DefaultGcpProjectIdProvider) GcpProjectIdProvider(com.google.cloud.spring.core.GcpProjectIdProvider) Test(org.junit.jupiter.api.Test)

Example 3 with GcpProjectIdProvider

use of com.google.cloud.spring.core.GcpProjectIdProvider in project spring-cloud-gcp by GoogleCloudPlatform.

the class GcpDatastoreEmulatorIntegrationTests method testDatastoreEmulatorConfiguration.

@Test
void testDatastoreEmulatorConfiguration() {
    DatastoreOptions.Builder builder = DatastoreOptions.newBuilder();
    new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(GcpDatastoreAutoConfiguration.class, GcpContextAutoConfiguration.class, DatastoreTransactionManagerAutoConfiguration.class, DatastoreRepositoriesAutoConfiguration.class, GcpDatastoreEmulatorAutoConfiguration.class)).withUserConfiguration(TestConfiguration.class).withPropertyValues("spring.cloud.gcp.project-id=test-project", "spring.cloud.gcp.datastore.namespace=test-namespace", "spring.cloud.gcp.datastore.emulator.port=8181", "spring.cloud.gcp.datastore.emulator.enabled=true", "spring.cloud.gcp.datastore.emulator.consistency=0.9").run(context -> {
        DatastoreTemplate datastore = context.getBean(DatastoreTemplate.class);
        Datastore datastoreClient = (Datastore) ((Supplier) context.getBean(context.getBeanNamesForType(ResolvableType.forClassWithGenerics(Supplier.class, Datastore.class))[0])).get();
        GcpProjectIdProvider projectIdProvider = context.getBean(GcpProjectIdProvider.class);
        builder.setServiceFactory(datastoreOptions -> datastoreClient).setProjectId(projectIdProvider.getProjectId());
        EmulatorEntityTest entity = new EmulatorEntityTest();
        entity.setProperty("property-test");
        datastore.save(entity);
        assertThat(entity.getId()).isNotNull();
        assertThat(datastore.findById(entity.getId(), EmulatorEntityTest.class).getProperty()).isEqualTo("property-test");
    });
    Datastore datastore = builder.build().getService();
    EntityQuery query = Query.newEntityQueryBuilder().setKind("RandomKind").setFilter(StructuredQuery.PropertyFilter.eq("key", "value")).build();
    assertThatExceptionOfType(DatastoreException.class).isThrownBy(() -> datastore.run(query));
}
Also used : Query(com.google.cloud.datastore.Query) AutoConfigurationPackage(org.springframework.boot.autoconfigure.AutoConfigurationPackage) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ApplicationContextRunner(org.springframework.boot.test.context.runner.ApplicationContextRunner) Datastore(com.google.cloud.datastore.Datastore) Supplier(java.util.function.Supplier) GcpProjectIdProvider(com.google.cloud.spring.core.GcpProjectIdProvider) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) CredentialsProvider(com.google.api.gax.core.CredentialsProvider) EnabledIfSystemProperty(org.junit.jupiter.api.condition.EnabledIfSystemProperty) ResolvableType(org.springframework.core.ResolvableType) StructuredQuery(com.google.cloud.datastore.StructuredQuery) AutoConfigurations(org.springframework.boot.autoconfigure.AutoConfigurations) DatastoreTransactionManagerAutoConfiguration(com.google.cloud.spring.autoconfigure.datastore.DatastoreTransactionManagerAutoConfiguration) GcpDatastoreAutoConfiguration(com.google.cloud.spring.autoconfigure.datastore.GcpDatastoreAutoConfiguration) Credentials(com.google.auth.Credentials) DatastoreException(com.google.cloud.datastore.DatastoreException) DatastoreOptions(com.google.cloud.datastore.DatastoreOptions) EntityQuery(com.google.cloud.datastore.EntityQuery) Test(org.junit.jupiter.api.Test) GcpContextAutoConfiguration(com.google.cloud.spring.autoconfigure.core.GcpContextAutoConfiguration) DatastoreRepositoriesAutoConfiguration(com.google.cloud.spring.autoconfigure.datastore.DatastoreRepositoriesAutoConfiguration) DatastoreTemplate(com.google.cloud.spring.data.datastore.core.DatastoreTemplate) GcpDatastoreEmulatorAutoConfiguration(com.google.cloud.spring.autoconfigure.datastore.GcpDatastoreEmulatorAutoConfiguration) Bean(org.springframework.context.annotation.Bean) Id(org.springframework.data.annotation.Id) Mockito.mock(org.mockito.Mockito.mock) Datastore(com.google.cloud.datastore.Datastore) DatastoreTemplate(com.google.cloud.spring.data.datastore.core.DatastoreTemplate) ApplicationContextRunner(org.springframework.boot.test.context.runner.ApplicationContextRunner) Supplier(java.util.function.Supplier) DatastoreOptions(com.google.cloud.datastore.DatastoreOptions) DatastoreException(com.google.cloud.datastore.DatastoreException) EntityQuery(com.google.cloud.datastore.EntityQuery) GcpProjectIdProvider(com.google.cloud.spring.core.GcpProjectIdProvider) Test(org.junit.jupiter.api.Test)

Example 4 with GcpProjectIdProvider

use of com.google.cloud.spring.core.GcpProjectIdProvider in project spring-cloud-gcp by GoogleCloudPlatform.

the class StackdriverJsonLayout method start.

@Override
public void start() {
    super.start();
    // If no Project ID set, then attempt to resolve it with the default project ID provider
    if (!StringUtils.hasText(this.projectId) || this.projectId.endsWith("_IS_UNDEFINED")) {
        GcpProjectIdProvider projectIdProvider = new DefaultGcpProjectIdProvider();
        this.projectId = projectIdProvider.getProjectId();
    }
    this.filteredMdcFields = new HashSet<>(Arrays.asList(traceIdMdcField, spanIdMdcField, StackdriverTraceConstants.MDC_FIELD_SPAN_EXPORT));
}
Also used : DefaultGcpProjectIdProvider(com.google.cloud.spring.core.DefaultGcpProjectIdProvider) GcpProjectIdProvider(com.google.cloud.spring.core.GcpProjectIdProvider) DefaultGcpProjectIdProvider(com.google.cloud.spring.core.DefaultGcpProjectIdProvider)

Example 5 with GcpProjectIdProvider

use of com.google.cloud.spring.core.GcpProjectIdProvider in project spring-cloud-gcp by GoogleCloudPlatform.

the class DefaultSubscriberFactoryTests method testGetRetrySettings_notPresentInMap_pickGlobal.

@Test
void testGetRetrySettings_notPresentInMap_pickGlobal() {
    GcpProjectIdProvider projectIdProvider = () -> "project";
    DefaultSubscriberFactory factory = new DefaultSubscriberFactory(projectIdProvider, this.pubSubConfig);
    RetrySettings expectedRetrySettings = RetrySettings.newBuilder().setTotalTimeout(Duration.ofSeconds(10L)).setInitialRetryDelay(Duration.ofSeconds(10L)).setRetryDelayMultiplier(10).setMaxRetryDelay(Duration.ofSeconds(10L)).setMaxAttempts(10).setInitialRpcTimeout(Duration.ofSeconds(10L)).setRpcTimeoutMultiplier(10).setMaxRpcTimeout(Duration.ofSeconds(10L)).build();
    factory.setGlobalRetrySettings(expectedRetrySettings);
    RetrySettings actualRetrySettings = factory.getRetrySettings("mySubscription");
    assertThat(actualRetrySettings.getTotalTimeout()).isEqualTo(Duration.ofSeconds(10));
    assertThat(actualRetrySettings.getInitialRetryDelay()).isEqualTo(Duration.ofSeconds(10));
    assertThat(actualRetrySettings.getRetryDelayMultiplier()).isEqualTo(10.0);
    assertThat(actualRetrySettings.getInitialRpcTimeout()).isEqualTo(Duration.ofSeconds(10));
    assertThat(actualRetrySettings.getMaxRetryDelay()).isEqualTo(Duration.ofSeconds(10));
    assertThat(actualRetrySettings.getMaxAttempts()).isEqualTo(10);
    assertThat(actualRetrySettings.getRpcTimeoutMultiplier()).isEqualTo(10.0);
    assertThat(actualRetrySettings.getMaxRpcTimeout()).isEqualTo(Duration.ofSeconds(10));
}
Also used : RetrySettings(com.google.api.gax.retrying.RetrySettings) GcpProjectIdProvider(com.google.cloud.spring.core.GcpProjectIdProvider) Test(org.junit.jupiter.api.Test)

Aggregations

GcpProjectIdProvider (com.google.cloud.spring.core.GcpProjectIdProvider)40 Test (org.junit.jupiter.api.Test)39 ApplicationContextRunner (org.springframework.boot.test.context.runner.ApplicationContextRunner)18 DefaultSubscriberFactory (com.google.cloud.spring.pubsub.support.DefaultSubscriberFactory)12 PubSubConfiguration (com.google.cloud.spring.pubsub.core.PubSubConfiguration)10 RetrySettings (com.google.api.gax.retrying.RetrySettings)7 FlowControlSettings (com.google.api.gax.batching.FlowControlSettings)5 SubscriberStubSettings (com.google.cloud.pubsub.v1.stub.SubscriberStubSettings)4 DefaultGcpProjectIdProvider (com.google.cloud.spring.core.DefaultGcpProjectIdProvider)3 CredentialsProvider (com.google.api.gax.core.CredentialsProvider)2 Subscriber (com.google.cloud.pubsub.v1.Subscriber)2 SubscriberStub (com.google.cloud.pubsub.v1.stub.SubscriberStub)2 ProjectSubscriptionName (com.google.pubsub.v1.ProjectSubscriptionName)2 Code (com.google.api.gax.rpc.StatusCode.Code)1 Credentials (com.google.auth.Credentials)1 Datastore (com.google.cloud.datastore.Datastore)1 DatastoreException (com.google.cloud.datastore.DatastoreException)1 DatastoreOptions (com.google.cloud.datastore.DatastoreOptions)1 EntityQuery (com.google.cloud.datastore.EntityQuery)1 Query (com.google.cloud.datastore.Query)1