Example usage for org.springframework.session.data.gemfire AbstractGemFireOperationsSessionRepository.GemFireSessionAttributes AbstractGemFireOperationsSessionRepository.GemFireSessionAttributes

List of usage examples for org.springframework.session.data.gemfire AbstractGemFireOperationsSessionRepository.GemFireSessionAttributes AbstractGemFireOperationsSessionRepository.GemFireSessionAttributes

Introduction

In this page you can find the example usage for org.springframework.session.data.gemfire AbstractGemFireOperationsSessionRepository.GemFireSessionAttributes AbstractGemFireOperationsSessionRepository.GemFireSessionAttributes.

Prototype

AbstractGemFireOperationsSessionRepository.GemFireSessionAttributes

Source Link

Usage

From source file:org.springframework.session.data.gemfire.AbstractGemFireOperationsSessionRepositoryTest.java

@Test
public void sessionFromData() throws Exception {
    final long expectedCreationTime = 1L;
    final long expectedLastAccessedTime = 2L;

    final int expectedMaxInactiveIntervalInSeconds = (int) TimeUnit.HOURS.toSeconds(6);

    final String expectedPrincipalName = "jblum";

    DataInput mockDataInput = mock(DataInput.class);

    given(mockDataInput.readUTF()).willReturn("2").willReturn(expectedPrincipalName);
    given(mockDataInput.readLong()).willReturn(expectedCreationTime).willReturn(expectedLastAccessedTime);
    given(mockDataInput.readInt()).willReturn(expectedMaxInactiveIntervalInSeconds);

    @SuppressWarnings("serial")
    AbstractGemFireOperationsSessionRepository.GemFireSession session = new AbstractGemFireOperationsSessionRepository.GemFireSession(
            "1") {
        @Override//from  w w w .ja v  a2  s  .  c  o m
        @SuppressWarnings("unchecked")
        <T> T readObject(DataInput in) throws ClassNotFoundException, IOException {
            assertThat(in).isNotNull();

            AbstractGemFireOperationsSessionRepository.GemFireSessionAttributes sessionAttributes = new AbstractGemFireOperationsSessionRepository.GemFireSessionAttributes();

            sessionAttributes.setAttribute("attrOne", "testOne");
            sessionAttributes.setAttribute("attrTwo", "testTwo");

            return (T) sessionAttributes;
        }
    };

    session.fromData(mockDataInput);

    Set<String> expectedAttributeNames = asSet("attrOne", "attrTwo",
            FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME);

    assertThat(session.getId()).isEqualTo("2");
    assertThat(session.getCreationTime()).isEqualTo(expectedCreationTime);
    assertThat(session.getLastAccessedTime()).isEqualTo(expectedLastAccessedTime);
    assertThat(session.getMaxInactiveIntervalInSeconds()).isEqualTo(expectedMaxInactiveIntervalInSeconds);
    assertThat(session.getPrincipalName()).isEqualTo(expectedPrincipalName);
    assertThat(session.getAttributeNames().size()).isEqualTo(3);
    assertThat(session.getAttributeNames().containsAll(expectedAttributeNames)).isTrue();
    assertThat(String.valueOf(session.getAttribute("attrOne"))).isEqualTo("testOne");
    assertThat(String.valueOf(session.getAttribute("attrTwo"))).isEqualTo("testTwo");
    assertThat(String.valueOf(session.getAttribute(FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME)))
            .isEqualTo(expectedPrincipalName);

    verify(mockDataInput, times(2)).readUTF();
    verify(mockDataInput, times(2)).readLong();
    verify(mockDataInput, times(2)).readInt();
}

From source file:org.springframework.session.data.gemfire.AbstractGemFireOperationsSessionRepositoryTest.java

@Test
public void sessionToDataThenFromDataWhenPrincipalNameIsNullGetsHandledProperly()
        throws ClassNotFoundException, IOException {

    final long beforeOrAtCreationTime = System.currentTimeMillis();

    @SuppressWarnings("serial")
    AbstractGemFireOperationsSessionRepository.GemFireSession expectedSession = new AbstractGemFireOperationsSessionRepository.GemFireSession(
            "123") {
        @Override//from   w ww. ja  v  a2s  . com
        void writeObject(Object obj, DataOutput out) throws IOException {
            assertThat(obj)
                    .isInstanceOf(AbstractGemFireOperationsSessionRepository.GemFireSessionAttributes.class);
            assertThat(out).isNotNull();
        }
    };

    assertThat(expectedSession.getId()).isEqualTo("123");
    assertThat(expectedSession.getCreationTime()).isGreaterThanOrEqualTo(beforeOrAtCreationTime);
    assertThat(expectedSession.getLastAccessedTime()).isGreaterThanOrEqualTo(expectedSession.getCreationTime());
    assertThat(expectedSession.getMaxInactiveIntervalInSeconds()).isEqualTo(0);
    assertThat(expectedSession.getPrincipalName()).isNull();

    ByteArrayOutputStream outBytes = new ByteArrayOutputStream();

    expectedSession.toData(new DataOutputStream(outBytes));

    @SuppressWarnings("serial")
    AbstractGemFireOperationsSessionRepository.GemFireSession deserializedSession = new AbstractGemFireOperationsSessionRepository.GemFireSession(
            "0") {
        @Override
        @SuppressWarnings("unchecked")
        <T> T readObject(DataInput in) throws ClassNotFoundException, IOException {
            return (T) new AbstractGemFireOperationsSessionRepository.GemFireSessionAttributes();
        }
    };

    deserializedSession.fromData(new DataInputStream(new ByteArrayInputStream(outBytes.toByteArray())));

    assertThat(deserializedSession).isEqualTo(expectedSession);
    assertThat(deserializedSession.getCreationTime()).isEqualTo(expectedSession.getCreationTime());
    assertThat(deserializedSession.getLastAccessedTime()).isEqualTo(expectedSession.getLastAccessedTime());
    assertThat(deserializedSession.getMaxInactiveIntervalInSeconds())
            .isEqualTo(expectedSession.getMaxInactiveIntervalInSeconds());
    assertThat(deserializedSession.getPrincipalName()).isNull();
}

From source file:org.springframework.session.data.gemfire.AbstractGemFireOperationsSessionRepositoryTest.java

@Test
public void sessionAttributesFromSession() {
    Session mockSession = mock(Session.class);

    given(mockSession.getAttributeNames()).willReturn(asSet("attrOne", "attrTwo"));
    given(mockSession.getAttribute(eq("attrOne"))).willReturn("testOne");
    given(mockSession.getAttribute(eq("attrTwo"))).willReturn("testTwo");

    AbstractGemFireOperationsSessionRepository.GemFireSessionAttributes sessionAttributes = new AbstractGemFireOperationsSessionRepository.GemFireSessionAttributes();

    assertThat(sessionAttributes.getAttributeNames().isEmpty()).isTrue();

    sessionAttributes.from(mockSession);

    assertThat(sessionAttributes.getAttributeNames().size()).isEqualTo(2);
    assertThat(sessionAttributes.getAttributeNames().containsAll(asSet("attrOne", "attrTwo"))).isTrue();
    assertThat(String.valueOf(sessionAttributes.getAttribute("attrOne"))).isEqualTo("testOne");
    assertThat(String.valueOf(sessionAttributes.getAttribute("attrTwo"))).isEqualTo("testTwo");

    verify(mockSession, times(1)).getAttributeNames();
    verify(mockSession, times(1)).getAttribute(eq("attrOne"));
    verify(mockSession, times(1)).getAttribute(eq("attrTwo"));
}

From source file:org.springframework.session.data.gemfire.AbstractGemFireOperationsSessionRepositoryTest.java

@Test
public void sessionAttributesFromSessionAttributes() {
    AbstractGemFireOperationsSessionRepository.GemFireSessionAttributes source = new AbstractGemFireOperationsSessionRepository.GemFireSessionAttributes();

    source.setAttribute("attrOne", "testOne");
    source.setAttribute("attrTwo", "testTwo");

    AbstractGemFireOperationsSessionRepository.GemFireSessionAttributes target = new AbstractGemFireOperationsSessionRepository.GemFireSessionAttributes();

    assertThat(target.getAttributeNames().isEmpty()).isTrue();

    target.from(source);// w w  w  .jav a 2s .  c om

    assertThat(target.getAttributeNames().size()).isEqualTo(2);
    assertThat(target.getAttributeNames().containsAll(asSet("attrOne", "attrTwo"))).isTrue();
    assertThat(String.valueOf(target.getAttribute("attrOne"))).isEqualTo("testOne");
    assertThat(String.valueOf(target.getAttribute("attrTwo"))).isEqualTo("testTwo");
}

From source file:org.springframework.session.data.gemfire.AbstractGemFireOperationsSessionRepositoryTest.java

@Test
public void sessionAttributesToData() throws Exception {
    final DataOutput mockDataOutput = mock(DataOutput.class);

    @SuppressWarnings("serial")
    AbstractGemFireOperationsSessionRepository.GemFireSessionAttributes sessionAttributes = new AbstractGemFireOperationsSessionRepository.GemFireSessionAttributes() {
        private int count = 0;

        @Override//ww w  .j av a 2  s .com
        void writeObject(Object obj, DataOutput out) throws IOException {
            assertThat(Arrays.asList("testOne", "testTwo").get(count++)).isEqualTo(String.valueOf(obj));
            assertThat(out).isSameAs(mockDataOutput);
        }
    };

    sessionAttributes.setAttribute("attrOne", "testOne");
    sessionAttributes.setAttribute("attrTwo", "testTwo");

    sessionAttributes.toData(mockDataOutput);

    verify(mockDataOutput, times(1)).writeInt(eq(2));
    verify(mockDataOutput, times(1)).writeUTF(eq("attrOne"));
    verify(mockDataOutput, times(1)).writeUTF(eq("attrTwo"));
}

From source file:org.springframework.session.data.gemfire.AbstractGemFireOperationsSessionRepositoryTest.java

@Test
public void sessionAttributesFromData() throws Exception {
    final DataInput mockDataInput = mock(DataInput.class);

    given(mockDataInput.readInt()).willReturn(2);
    given(mockDataInput.readUTF()).willReturn("attrOne").willReturn("attrTwo");

    @SuppressWarnings("serial")
    AbstractGemFireOperationsSessionRepository.GemFireSessionAttributes sessionAttributes = new AbstractGemFireOperationsSessionRepository.GemFireSessionAttributes() {
        private int count = 0;

        @Override/*from  w  ww  .ja  v a 2  s .  c o m*/
        @SuppressWarnings("unchecked")
        <T> T readObject(DataInput in) throws ClassNotFoundException, IOException {
            assertThat(in).isSameAs(mockDataInput);
            return (T) Arrays.asList("testOne", "testTwo").get(count++);
        }
    };

    assertThat(sessionAttributes.getAttributeNames().isEmpty()).isTrue();

    sessionAttributes.fromData(mockDataInput);

    assertThat(sessionAttributes.getAttributeNames().size()).isEqualTo(2);
    assertThat(sessionAttributes.getAttributeNames().containsAll(asSet("attrOne", "attrTwo"))).isTrue();
    assertThat(String.valueOf(sessionAttributes.getAttribute("attrOne"))).isEqualTo("testOne");
    assertThat(String.valueOf(sessionAttributes.getAttribute("attrTwo"))).isEqualTo("testTwo");

    verify(mockDataInput, times(1)).readInt();
    verify(mockDataInput, times(2)).readUTF();
}

From source file:org.springframework.session.data.gemfire.AbstractGemFireOperationsSessionRepositoryTest.java

@Test
public void sessionAttributesHasDeltaIsFalse() {
    assertThat(new AbstractGemFireOperationsSessionRepository.GemFireSessionAttributes().hasDelta()).isFalse();
}

From source file:org.springframework.session.data.gemfire.AbstractGemFireOperationsSessionRepositoryTest.java

@Test
public void sessionAttributesHasDeltaIsTrue() {
    AbstractGemFireOperationsSessionRepository.GemFireSessionAttributes sessionAttributes = new AbstractGemFireOperationsSessionRepository.GemFireSessionAttributes();

    assertThat(sessionAttributes.hasDelta()).isFalse();

    sessionAttributes.setAttribute("attrOne", "testOne");

    assertThat(String.valueOf(sessionAttributes.getAttribute("attrOne"))).isEqualTo("testOne");
    assertThat(sessionAttributes.hasDelta()).isTrue();
}

From source file:org.springframework.session.data.gemfire.AbstractGemFireOperationsSessionRepositoryTest.java

@Test
public void sessionAttributesToDelta() throws Exception {
    final DataOutput mockDataOutput = mock(DataOutput.class);

    @SuppressWarnings("serial")
    AbstractGemFireOperationsSessionRepository.GemFireSessionAttributes sessionAttributes = new AbstractGemFireOperationsSessionRepository.GemFireSessionAttributes() {
        private int count = 0;

        @Override/*from   w w w . j  a  v  a2  s. c  o  m*/
        void writeObject(Object obj, DataOutput out) throws IOException {
            assertThat(Arrays.asList("testOne", "testTwo", "testThree").get(count++))
                    .isEqualTo(String.valueOf(obj));
            assertThat(out).isSameAs(mockDataOutput);
        }
    };

    sessionAttributes.setAttribute("attrOne", "testOne");
    sessionAttributes.setAttribute("attrTwo", "testTwo");

    assertThat(sessionAttributes.hasDelta()).isTrue();

    sessionAttributes.toDelta(mockDataOutput);

    assertThat(sessionAttributes.hasDelta()).isFalse();

    verify(mockDataOutput, times(1)).writeInt(eq(2));
    verify(mockDataOutput, times(1)).writeUTF("attrOne");
    verify(mockDataOutput, times(1)).writeUTF("attrTwo");
    reset(mockDataOutput);

    sessionAttributes.setAttribute("attrOne", "testOne");

    assertThat(sessionAttributes.hasDelta()).isFalse();

    sessionAttributes.toDelta(mockDataOutput);

    verify(mockDataOutput, times(1)).writeInt(eq(0));
    verify(mockDataOutput, never()).writeUTF(any(String.class));
    reset(mockDataOutput);

    sessionAttributes.setAttribute("attrTwo", "testThree");

    assertThat(sessionAttributes.hasDelta()).isTrue();

    sessionAttributes.toDelta(mockDataOutput);

    verify(mockDataOutput, times(1)).writeInt(eq(1));
    verify(mockDataOutput, times(1)).writeUTF(eq("attrTwo"));
}

From source file:org.springframework.session.data.gemfire.AbstractGemFireOperationsSessionRepositoryTest.java

@Test
public void sessionAttributesFromDelta() throws Exception {
    final DataInput mockDataInput = mock(DataInput.class);

    given(mockDataInput.readInt()).willReturn(2);
    given(mockDataInput.readUTF()).willReturn("attrOne").willReturn("attrTwo");

    @SuppressWarnings("serial")
    AbstractGemFireOperationsSessionRepository.GemFireSessionAttributes sessionAttributes = new AbstractGemFireOperationsSessionRepository.GemFireSessionAttributes() {
        private int count = 0;

        @Override//  ww  w . j a  v  a 2  s.co  m
        @SuppressWarnings("unchecked")
        <T> T readObject(DataInput in) throws ClassNotFoundException, IOException {
            assertThat(in).isSameAs(mockDataInput);
            return (T) Arrays.asList("testOne", "testTwo", "testThree").get(count++);
        }
    };

    sessionAttributes.setAttribute("attrOne", "one");
    sessionAttributes.setAttribute("attrTwo", "two");

    assertThat(sessionAttributes.getAttributeNames().size()).isEqualTo(2);
    assertThat(sessionAttributes.getAttributeNames().containsAll(asSet("attrOne", "attrTwo"))).isTrue();
    assertThat(String.valueOf(sessionAttributes.getAttribute("attrOne"))).isEqualTo("one");
    assertThat(String.valueOf(sessionAttributes.getAttribute("attrTwo"))).isEqualTo("two");
    assertThat(sessionAttributes.hasDelta()).isTrue();

    sessionAttributes.fromDelta(mockDataInput);

    assertThat(sessionAttributes.getAttributeNames().size()).isEqualTo(2);
    assertThat(sessionAttributes.getAttributeNames().containsAll(asSet("attrOne", "attrTwo"))).isTrue();
    assertThat(String.valueOf(sessionAttributes.getAttribute("attrOne"))).isEqualTo("testOne");
    assertThat(String.valueOf(sessionAttributes.getAttribute("attrTwo"))).isEqualTo("testTwo");
    assertThat(sessionAttributes.hasDelta()).isFalse();

    verify(mockDataInput, times(1)).readInt();
    verify(mockDataInput, times(2)).readUTF();
    reset(mockDataInput);

    given(mockDataInput.readInt()).willReturn(1);
    given(mockDataInput.readUTF()).willReturn("attrTwo");

    sessionAttributes.setAttribute("attrOne", "one");
    sessionAttributes.setAttribute("attrTwo", "two");

    assertThat(sessionAttributes.getAttributeNames().size()).isEqualTo(2);
    assertThat(sessionAttributes.getAttributeNames().containsAll(asSet("attrOne", "attrTwo"))).isTrue();
    assertThat(String.valueOf(sessionAttributes.getAttribute("attrOne"))).isEqualTo("one");
    assertThat(String.valueOf(sessionAttributes.getAttribute("attrTwo"))).isEqualTo("two");
    assertThat(sessionAttributes.hasDelta()).isTrue();

    sessionAttributes.fromDelta(mockDataInput);

    assertThat(sessionAttributes.getAttributeNames().size()).isEqualTo(2);
    assertThat(sessionAttributes.getAttributeNames().containsAll(asSet("attrOne", "attrTwo"))).isTrue();
    assertThat(String.valueOf(sessionAttributes.getAttribute("attrOne"))).isEqualTo("one");
    assertThat(String.valueOf(sessionAttributes.getAttribute("attrTwo"))).isEqualTo("testThree");
    assertThat(sessionAttributes.hasDelta()).isTrue();

    verify(mockDataInput, times(1)).readInt();
    verify(mockDataInput, times(1)).readUTF();
}