Search in sources :

Example 11 with Session

use of com.tangosol.net.Session in project micronaut-coherence by micronaut-projects.

the class CoherenceConfigurationClientTest method shouldProvidePropertySources.

@Test
public void shouldProvidePropertySources() {
    NamedMap<String, Object> applicationMap = session.getMap("application");
    applicationMap.put("hello", "app hello");
    applicationMap.put("foo", "app foo");
    applicationMap.put("my-app", "app my-app");
    applicationMap.put("test", "app test");
    applicationMap.put("backend", "app backend");
    NamedMap<String, Object> applicationFooMap = session.getMap("application-foo");
    applicationFooMap.put("bar", "app foo bar");
    NamedMap<String, Object> applicationTestMap = session.getMap("application-test");
    applicationTestMap.put("foo", "app test foo");
    NamedMap<String, Object> applicationBackendMap = session.getMap("application-backend");
    applicationBackendMap.put("foo", "app backend foo");
    applicationBackendMap.put("bar", "app backend bar");
    NamedMap<String, Object> myAppMap = session.getMap("my-app");
    myAppMap.put("hello", "my-app hello");
    myAppMap.put("foo", "my-app foo");
    NamedMap<String, Object> myAppTestMap = session.getMap("my-app-test");
    myAppTestMap.put("hello", "my-app test hello");
    NamedMap<String, Object> myAppBackendMap = session.getMap("my-app-backend");
    myAppBackendMap.put("hello", "my-app backend hello");
    myAppBackendMap.put("hello/bar", "my-app backend hello/bar");
    NamedMap<String, Object> otherAppBackendMap = session.getMap("other-app");
    otherAppBackendMap.put("hello", "other-app hello");
    ApplicationConfiguration applicationConfiguration = new ApplicationConfiguration();
    applicationConfiguration.setName("my-app");
    CoherenceClientConfiguration clientConfig = new CoherenceClientConfiguration();
    clientConfig.setEnabled(true);
    CoherenceConfigurationClient client = new CoherenceConfigurationClient(applicationConfiguration, clientConfig) {

        @Override
        protected Session buildSession(CoherenceClientConfiguration coherenceClientConfiguration) {
            Session session = mock(Session.class);
            when(session.<String, Object>getMap("application")).thenReturn(applicationMap);
            when(session.<String, Object>getMap("application-foo")).thenReturn(applicationFooMap);
            when(session.<String, Object>getMap("application-test")).thenReturn(applicationTestMap);
            when(session.<String, Object>getMap("application-backend")).thenReturn(applicationBackendMap);
            when(session.<String, Object>getMap("my-app")).thenReturn(myAppMap);
            when(session.<String, Object>getMap("my-app-test")).thenReturn(myAppTestMap);
            when(session.<String, Object>getMap("my-app-backend")).thenReturn(myAppBackendMap);
            when(session.<String, Object>getMap("other-app")).thenReturn(otherAppBackendMap);
            return session;
        }
    };
    Publisher<PropertySource> propertySourcePublisher = client.getPropertySources(context.getEnvironment());
    Iterable<PropertySource> propsIt = Flux.from(propertySourcePublisher).toIterable();
    Map<String, PropertySource> propertySources = StreamSupport.stream(propsIt.spliterator(), false).collect(Collectors.toMap(PropertySource::getName, Function.identity()));
    assertThat(propertySources.keySet().toArray(), arrayContainingInAnyOrder("application", "application-test", "application-backend", "my-app", "my-app-test", "my-app-backend"));
    PropertySource appPs = propertySources.get("application");
    assertEquals(-99, appPs.getOrder());
    Map<String, String> props = StreamSupport.stream(appPs.spliterator(), false).collect(Collectors.toMap(Function.identity(), key -> (String) appPs.get(key)));
    Map<String, String> expected = new HashMap<>();
    expected.put("hello", "app hello");
    expected.put("foo", "app foo");
    expected.put("my-app", "app my-app");
    expected.put("test", "app test");
    expected.put("backend", "app backend");
    assertEquals(expected, props);
    PropertySource appTestPs = propertySources.get("application-test");
    assertEquals(-47, appTestPs.getOrder());
    props = StreamSupport.stream(appTestPs.spliterator(), false).collect(Collectors.toMap(Function.identity(), key -> (String) appTestPs.get(key)));
    expected = new HashMap<>();
    expected.put("foo", "app test foo");
    assertEquals(expected, props);
    PropertySource appBackendPs = propertySources.get("application-backend");
    assertEquals(-45, appBackendPs.getOrder());
    props = StreamSupport.stream(appBackendPs.spliterator(), false).collect(Collectors.toMap(Function.identity(), key -> (String) appBackendPs.get(key)));
    expected = new HashMap<>();
    expected.put("foo", "app backend foo");
    expected.put("bar", "app backend bar");
    assertEquals(expected, props);
    PropertySource myAppPs = propertySources.get("my-app");
    assertEquals(-98, myAppPs.getOrder());
    props = StreamSupport.stream(myAppPs.spliterator(), false).collect(Collectors.toMap(Function.identity(), key -> (String) myAppPs.get(key)));
    expected = new HashMap<>();
    expected.put("hello", "my-app hello");
    expected.put("foo", "my-app foo");
    assertEquals(expected, props);
    PropertySource myAppTestPs = propertySources.get("my-app-test");
    assertEquals(-46, myAppTestPs.getOrder());
    props = StreamSupport.stream(myAppTestPs.spliterator(), false).collect(Collectors.toMap(Function.identity(), key -> (String) myAppTestPs.get(key)));
    expected = new HashMap<>();
    expected.put("hello", "my-app test hello");
    assertEquals(expected, props);
    PropertySource myAppBackendPs = propertySources.get("my-app-backend");
    assertEquals(-44, myAppBackendPs.getOrder());
    props = StreamSupport.stream(myAppBackendPs.spliterator(), false).collect(Collectors.toMap(Function.identity(), key -> (String) myAppBackendPs.get(key)));
    expected = new HashMap<>();
    expected.put("hello", "my-app backend hello");
    expected.put("hello/bar", "my-app backend hello/bar");
    assertEquals(expected, props);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) HashMap(java.util.HashMap) Session(com.tangosol.net.Session) Channel(io.grpc.Channel) Function(java.util.function.Function) Matchers.arrayContainingInAnyOrder(org.hamcrest.Matchers.arrayContainingInAnyOrder) Inject(javax.inject.Inject) ApplicationContext(io.micronaut.context.ApplicationContext) TestInstance(org.junit.jupiter.api.TestInstance) Map(java.util.Map) StreamSupport(java.util.stream.StreamSupport) ApplicationConfiguration(io.micronaut.runtime.ApplicationConfiguration) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) MicronautTest(io.micronaut.test.extensions.junit5.annotation.MicronautTest) NamedMap(com.tangosol.net.NamedMap) PropertySource(io.micronaut.context.env.PropertySource) Name(io.micronaut.coherence.annotation.Name) Publisher(org.reactivestreams.Publisher) GrpcSessionConfiguration(com.oracle.coherence.client.GrpcSessionConfiguration) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Collectors(java.util.stream.Collectors) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) ManagedChannelBuilder(io.grpc.ManagedChannelBuilder) Mockito(org.mockito.Mockito) Flux(reactor.core.publisher.Flux) MockedStatic(org.mockito.MockedStatic) Optional(java.util.Optional) Mockito.mock(org.mockito.Mockito.mock) HashMap(java.util.HashMap) PropertySource(io.micronaut.context.env.PropertySource) ApplicationConfiguration(io.micronaut.runtime.ApplicationConfiguration) Session(com.tangosol.net.Session) MicronautTest(io.micronaut.test.extensions.junit5.annotation.MicronautTest) Test(org.junit.jupiter.api.Test)

Example 12 with Session

use of com.tangosol.net.Session in project micronaut-coherence by micronaut-projects.

the class CoherenceConfigurationClientTest method shouldCreateSession.

@Test
public void shouldCreateSession() {
    ApplicationConfiguration applicationConfiguration = mock(ApplicationConfiguration.class);
    CoherenceClientConfiguration coherenceClientConfiguration = new CoherenceClientConfiguration();
    CoherenceConfigurationClient client = new CoherenceConfigurationClient(applicationConfiguration, coherenceClientConfiguration);
    GrpcSessionConfiguration.Builder builder = mock(GrpcSessionConfiguration.Builder.class);
    GrpcSessionConfiguration grpcSessionConfiguration = mock(GrpcSessionConfiguration.class);
    when(builder.build()).thenReturn(grpcSessionConfiguration);
    Session session = mock(Session.class);
    try (MockedStatic<GrpcSessionConfiguration> grpcSessionConfigStaticMock = Mockito.mockStatic(GrpcSessionConfiguration.class);
        MockedStatic<Session> sessionStaticMock = Mockito.mockStatic(Session.class)) {
        grpcSessionConfigStaticMock.when(() -> GrpcSessionConfiguration.builder(any(Channel.class))).thenReturn(builder);
        sessionStaticMock.when(() -> Session.create(eq(grpcSessionConfiguration))).thenReturn(Optional.of(session));
        Session s = client.buildSession(coherenceClientConfiguration);
        assertEquals(session, s);
        verify(builder).build();
        sessionStaticMock.verify(() -> Session.create(grpcSessionConfiguration), times(1));
    }
}
Also used : GrpcSessionConfiguration(com.oracle.coherence.client.GrpcSessionConfiguration) Channel(io.grpc.Channel) ApplicationConfiguration(io.micronaut.runtime.ApplicationConfiguration) Session(com.tangosol.net.Session) MicronautTest(io.micronaut.test.extensions.junit5.annotation.MicronautTest) Test(org.junit.jupiter.api.Test)

Example 13 with Session

use of com.tangosol.net.Session in project micronaut-coherence by micronaut-projects.

the class GrpcProxyTest method shouldHaveGrpcSession.

@Test
public void shouldHaveGrpcSession() {
    Coherence coherence = context.findBean(Coherence.class).orElseThrow(() -> new AssertionError("Could not find Coherence bean"));
    Session session = coherence.getSession("grpc-client");
    assertThat(session, is(instanceOf(GrpcRemoteSession.class)));
    NamedCache<String, String> cache = session.getCache("test");
    cache.put("Key-1", "Value-1");
    assertThat(cache.get("Key-1"), is("Value-1"));
}
Also used : Coherence(com.tangosol.net.Coherence) Session(com.tangosol.net.Session) GrpcRemoteSession(com.oracle.coherence.client.GrpcRemoteSession) Test(org.junit.jupiter.api.Test) MicronautTest(io.micronaut.test.extensions.junit5.annotation.MicronautTest)

Example 14 with Session

use of com.tangosol.net.Session in project micronaut-coherence by micronaut-projects.

the class CoherenceConfigurationClient method getPropertySources.

@Override
public Publisher<PropertySource> getPropertySources(Environment environment) {
    if (!coherenceClientConfiguration.isEnabled()) {
        return Flux.empty();
    }
    Session session = buildSession(coherenceClientConfiguration);
    Map<Integer, String> keys = buildSourceNames(applicationConfiguration, environment);
    for (Map.Entry<Integer, String> entry : keys.entrySet()) {
        final Integer priority = entry.getKey();
        final String propertySource = entry.getValue();
        NamedMap<String, Object> configMap = session.getMap(propertySource);
        Flux<PropertySource> propertySourceFlux = Flux.just(PropertySource.of(propertySource, configMap, priority));
        propertySources.add(propertySourceFlux);
    }
    return Flux.merge(propertySources);
}
Also used : HashMap(java.util.HashMap) Map(java.util.Map) NamedMap(com.tangosol.net.NamedMap) Session(com.tangosol.net.Session) PropertySource(io.micronaut.context.env.PropertySource) EnvironmentPropertySource(io.micronaut.context.env.EnvironmentPropertySource)

Example 15 with Session

use of com.tangosol.net.Session in project coherence-hibernate by coherence-community.

the class AfterInsertProcessorTests method insertValue.

@Test
public void insertValue() throws ExecutionException, InterruptedException {
    // Create the Coherence instance from the configuration
    final Coherence coherence = Coherence.clusterMember();
    coherence.start().get();
    final Session coherenceSession = coherence.getSession();
    final NamedCache<Long, CoherenceRegionValue> fooCache = coherenceSession.getCache("foo");
    assertThat(fooCache.size()).isEqualTo(0);
    final CoherenceRegionValue coherenceRegionValue = new CoherenceRegionValue("bar", 1, Instant.now().toEpochMilli());
    final AfterInsertProcessor afterInsertProcessor = new AfterInsertProcessor(coherenceRegionValue);
    Boolean result = fooCache.<Boolean>invoke(1L, afterInsertProcessor);
    assertThat(result).isTrue();
    assertThat(fooCache.size()).isEqualTo(1);
    assertThat(fooCache.get(1L)).isEqualTo(coherenceRegionValue);
    coherence.close();
}
Also used : CoherenceRegionValue(com.oracle.coherence.hibernate.cache.v53.region.CoherenceRegionValue) Coherence(com.tangosol.net.Coherence) Session(com.tangosol.net.Session) Test(org.junit.Test)

Aggregations

Session (com.tangosol.net.Session)28 Test (org.junit.jupiter.api.Test)14 GrpcSessionConfiguration (com.oracle.coherence.client.GrpcSessionConfiguration)5 Coherence (com.tangosol.net.Coherence)5 Filter (com.tangosol.util.Filter)5 LocalPlatform (com.oracle.bedrock.runtime.LocalPlatform)4 Platform (com.oracle.bedrock.runtime.Platform)4 GetClusterSize (com.oracle.bedrock.runtime.coherence.callables.GetClusterSize)4 GetLocalMemberId (com.oracle.bedrock.runtime.coherence.callables.GetLocalMemberId)4 AbstractTest (com.oracle.bedrock.testsupport.junit.AbstractTest)4 MicronautTest (io.micronaut.test.extensions.junit5.annotation.MicronautTest)4 SessionName (com.oracle.coherence.spring.annotation.SessionName)3 NamedCache (com.tangosol.net.NamedCache)3 ValueExtractor (com.tangosol.util.ValueExtractor)3 Channel (io.grpc.Channel)3 Name (com.oracle.coherence.spring.annotation.Name)2 NamedMap (com.tangosol.net.NamedMap)2 Publisher (com.tangosol.net.topic.Publisher)2 MapEventTransformer (com.tangosol.util.MapEventTransformer)2 MapEventFilter (com.tangosol.util.filter.MapEventFilter)2