Skip to content

Commit

Permalink
save work #7529
Browse files Browse the repository at this point in the history
  • Loading branch information
rymsha committed Oct 17, 2019
1 parent bbb043b commit fa18603
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,158 +4,150 @@
import java.util.HashSet;
import java.util.Set;

import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.configuration.CacheConfiguration;
import javax.cache.Cache;

import org.eclipse.jetty.server.session.SessionContext;
import org.eclipse.jetty.server.session.SessionData;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

import com.google.common.collect.Sets;

import com.enonic.xp.web.session.impl.WebSessionConfig;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class IgniteSessionDataStoreTest
@ExtendWith(MockitoExtension.class)
class IgniteSessionDataStoreTest
{
private final static String NODE = "mynode";

private IgniteSessionDataStore store;

private IgniteCache<String, SessionData> cache;
@Mock
private Cache<String, IgnitedSessionData> cache;

private Ignite ignite;

@BeforeEach
public void setup()
void setup()
throws Exception
{
store = new IgniteSessionDataStore();

final SessionContext ctx = Mockito.mock( SessionContext.class );
when( ctx.getCanonicalContextPath() ).thenReturn( "cpath" );
when( ctx.getVhost() ).thenReturn( "vhost" );
when( ctx.getWorkerName() ).thenReturn( NODE );
mockContextRun( ctx );
lenient().when( ctx.getWorkerName() ).thenReturn( NODE );

store = new IgniteSessionDataStore( cache );
store.initialize( ctx );
ignite = Mockito.mock( Ignite.class );
cache = (IgniteCache<String, SessionData>) Mockito.mock( IgniteCache.class );
when( ignite.getOrCreateCache( any( CacheConfiguration.class ) ) ).thenReturn( cache );
store.setIgnite( ignite );
store.activate( getWebSessionConfig() );
store.start();
}

@Test
public void load()
void load()
throws Exception
{
final SessionData sessionData = new SessionData( "123", null, null, 0, 0, 0, 0 );
when( cache.get( anyString() ) ).thenReturn( sessionData );
when( cache.get( anyString() ) ).thenReturn( new IgnitedSessionData( sessionData ) );

assertEquals( sessionData, store.load( "123" ) );
}

@Test
public void loadWithException()
void loadWithException()
throws Exception
{
when( cache.get( anyString() ) ).thenThrow( new RuntimeException( "Something happened" ) );
store.activate( getWebSessionConfig() );

final RuntimeException runtimeException = assertThrows( RuntimeException.class, () -> {
store.load( "123" );
} );
final RuntimeException runtimeException = assertThrows( RuntimeException.class, () -> store.load( "123" ) );
assertEquals( "Something happened", runtimeException.getMessage() );
}

@Test
public void delete()
void delete()
throws Exception
{
when( cache.remove( anyString() ) ).thenReturn( true );
store.activate( getWebSessionConfig() );

final boolean deleted = store.delete( "123" );
assertTrue( deleted );
}

@Test
public void doStore()
void doStore()
throws Exception
{
final SessionData sessionData = new SessionData( "123", null, null, 0, 0, 0, 0 );
store.activate( getWebSessionConfig() );

store.doStore( "123", sessionData, 0 );

verify( cache, Mockito.times( 1 ) ).put( eq( "cpath_vhost_123" ), any( SessionData.class ) );
verify( cache, Mockito.times( 1 ) ).put( eq( "cpath_vhost_123" ), any( IgnitedSessionData.class ) );
}

@Test
public void exists()
void exists()
throws Exception
{
final SessionData sessionData = new SessionData( "123", null, null, 0, 0, 0, 0 );
when( cache.get( anyString() ) ).thenReturn( sessionData );
when( cache.get( anyString() ) ).thenReturn( new IgnitedSessionData( sessionData ) );

final boolean exists = store.exists( "123" );
assertTrue( exists );
}

@Test
public void existsFalse()
void existsFalse()
throws Exception
{
final boolean exists = store.exists( "123" );
assertFalse( exists );
}

@Test
public void existsWithExpiryTime()
void existsWithExpiryTime()
throws Exception
{
final SessionData sessionData = new SessionData( "123", null, null, 0, 0, 0, 0 );
sessionData.setExpiry( System.currentTimeMillis() + 1000 );
when( cache.get( anyString() ) ).thenReturn( sessionData );
when( cache.get( anyString() ) ).thenReturn( new IgnitedSessionData( sessionData ) );

final boolean exists = store.exists( "123" );
assertTrue( exists );
}

@Test
public void isPassivating()
void isPassivating()
throws Exception
{
assertTrue( store.isPassivating() );
}

@Test
public void doGetExpired()
void doGetExpired()
throws Exception
{
final SessionData sessionData = new SessionData( "123", "cpath", "vhost", 0, 0, 0, 0 );
sessionData.setExpiry( System.currentTimeMillis() - 10000 );
sessionData.setLastNode( NODE );
when( cache.get( eq( getCacheKey( "123" ) ) ) ).thenReturn( sessionData );
when( cache.get( eq( getCacheKey( "123" ) ) ) ).thenReturn( new IgnitedSessionData( sessionData ) );

final SessionData sessionData2 = new SessionData( "456", "cpath", "vhost", 0, 0, 0, 0 );
sessionData2.setExpiry( System.currentTimeMillis() - 1000000000 );
sessionData2.setLastNode( "OTHER" );
when( cache.get( eq( getCacheKey( "456" ) ) ) ).thenReturn( sessionData2 );
when( cache.get( eq( getCacheKey( "456" ) ) ) ).thenReturn( new IgnitedSessionData( sessionData2 ) );

final Set<String> expiredSessionIds = store.doGetExpired( Sets.newHashSet( "123", "456", "789" ) );
assertEquals( Sets.newHashSet( "123", "456", "789" ), expiredSessionIds );
Expand All @@ -167,45 +159,13 @@ public void doGetExpired()


@Test
public void doGetExpiredEmpty()
void doGetExpiredEmpty()
throws Exception
{
final Set<String> expiredSessionIds = store.doGetExpired( new HashSet<>() );
assertEquals( new HashSet<>(), expiredSessionIds );
}

@Test
@Disabled("Should not work without ignite")
public void withoutIgnite()
throws Exception
{
final SessionData sessionData = new SessionData( "123", null, null, 0, 0, 0, 0 );
when( cache.get( anyString() ) ).thenReturn( sessionData );
when( cache.remove( anyString() ) ).thenReturn( true );

store.setIgnite( null );
assertNull( store.load( "123" ) );
assertFalse( store.delete( "123" ) );
store.doStore( "123", sessionData, 0 );
assertEquals( sessionData, store.load( "123" ) );
assertTrue( store.delete( "123" ) );

store.setIgnite( ignite );
assertEquals( sessionData, store.load( "123" ) );
assertTrue( store.delete( "123" ) );
store.store( "123", sessionData );
verify( cache, Mockito.times( 1 ) ).put( eq( "cpath_vhost_123" ), any( SessionData.class ) );
}

private void mockContextRun( final SessionContext context )
{
doAnswer( invocation -> {
Runnable runnable = (Runnable) invocation.getArguments()[0];
runnable.run();
return null;
} ).when( context ).run( any( Runnable.class ) );
}

private String getCacheKey( String id )
{
return "cpath_vhost_" + id;
Expand Down Expand Up @@ -263,7 +223,7 @@ public int session_save_period()
{
return 10;
}

public int write_timeout()
{
return 1000;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import com.fasterxml.jackson.databind.JsonNode;

import com.enonic.xp.support.JsonTestHelper;
import com.enonic.xp.web.session.impl.ignite.IgniteSessionDataStore;
import com.enonic.xp.web.session.impl.ignite.IgniteSessionDataStoreFactory;

import static org.junit.jupiter.api.Assertions.assertEquals;

Expand All @@ -32,10 +32,9 @@ public void setUp()
this.cache = (IgniteCache<Object, Object>) Mockito.mock( IgniteCache.class );
this.cacheMetrics = Mockito.mock( CacheMetrics.class );

Mockito.when( this.ignite.cache( IgniteSessionDataStore.WEB_SESSION_CACHE ) ).thenReturn( this.cache );
Mockito.when( this.cache.metrics() ).thenReturn( this.cacheMetrics );
Mockito.when( this.cache.size() ).thenReturn( 123 );
Mockito.when( this.cache.getName() ).thenReturn( IgniteSessionDataStore.WEB_SESSION_CACHE );
Mockito.when( this.cache.getName() ).thenReturn( IgniteSessionDataStoreFactory.WEB_SESSION_CACHE );
}

@Test
Expand All @@ -48,7 +47,7 @@ public void with_metrics()
final WebSessionReporter reporter = new WebSessionReporter();
reporter.setIgnite( this.ignite );

assertEquals( reporter.getName(), "cache." + IgniteSessionDataStore.WEB_SESSION_CACHE );
assertEquals( reporter.getName(), "cache." + IgniteSessionDataStoreFactory.WEB_SESSION_CACHE );

final JsonNode result = reporter.getReport();
assertJson( "metrics.json", result );
Expand Down

0 comments on commit fa18603

Please sign in to comment.