/*
 * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
 * agreements. See the NOTICE file distributed with this work for additional information regarding
 * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance with the License. You may obtain a
 * copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package org.apache.geode.modules.session.catalina.internal;

import static org.apache.geode.test.awaitility.GeodeAwaitility.await;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doCallRealMethod;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import javax.servlet.http.HttpSession;

import junitparams.JUnitParamsRunner;
import junitparams.Parameters;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import org.apache.geode.cache.CustomExpiry;
import org.apache.geode.cache.ExpirationAction;
import org.apache.geode.cache.ExpirationAttributes;
import org.apache.geode.cache.Region;
import org.apache.geode.cache.RegionShortcut;
import org.apache.geode.modules.session.catalina.AbstractSessionCache;
import org.apache.geode.modules.session.catalina.SessionCache;
import org.apache.geode.modules.session.catalina.callback.SessionExpirationCacheListener;

@RunWith(JUnitParamsRunner.class)
public class DeltaSessionStatisticsIntegrationTest extends AbstractDeltaSessionIntegrationTest {
  private DeltaSessionStatistics statistics;

  @Before
  public void setUp() {
    statistics = new DeltaSessionStatistics(server.getCache().getDistributedSystem(), "testApp");
  }

  @Test
  @Parameters({"REPLICATE", "PARTITION"})
  public void sessionsCreatedStatisticsShouldBeIncrementedWhenSessionIsAdded(
      RegionShortcut regionShortcut) {
    httpSessionRegion = server.getCache()
        .<String, HttpSession>createRegionFactory(regionShortcut)
        .create(REGION_NAME);

    SessionCache mockSessionCache = mock(AbstractSessionCache.class);
    doCallRealMethod().when(mockSessionCache).putSession(any());
    when(mockSessionCache.getStatistics()).thenReturn(statistics);
    when(mockSessionCache.getOperatingRegion()).thenReturn(httpSessionRegion);

    mockDeltaSessionManager();
    doCallRealMethod().when(deltaSessionManager).add(any());
    when(deltaSessionManager.getSessionCache()).thenReturn(mockSessionCache);

    deltaSessionManager.add(new TestDeltaSession(deltaSessionManager, TEST_SESSION_ID));
    await().untilAsserted(() -> assertThat(httpSessionRegion.isEmpty()).isFalse());
    assertThat(statistics.getSessionsCreated()).isEqualTo(1);
  }

  @Test
  @Parameters({"REPLICATE", "PARTITION"})
  public void sessionsExpiredStatisticsShouldBeIncrementedWhenSessionExpires(
      RegionShortcut regionShortcut) {
    httpSessionRegion = server.getCache()
        .<String, HttpSession>createRegionFactory(regionShortcut)
        .setCustomEntryTimeToLive(new TestCustomExpiry())
        .addCacheListener(new SessionExpirationCacheListener())
        .create(REGION_NAME);
    mockDeltaSessionManager();
    when(deltaSessionManager.getStatistics()).thenReturn(statistics);
    httpSessionRegion.put(TEST_SESSION_ID,
        new TestDeltaSession(deltaSessionManager, TEST_SESSION_ID));

    await().untilAsserted(() -> assertThat(httpSessionRegion.isEmpty()).isTrue());
    assertThat(statistics.getSessionsExpired()).isEqualTo(1);
  }

  @Test
  @Parameters({"REPLICATE", "PARTITION"})
  public void sessionsInvalidatedStatisticsShouldBeIncrementedWhenSessionInInvalidated(
      RegionShortcut regionShortcut) {
    httpSessionRegion = server.getCache()
        .<String, HttpSession>createRegionFactory(regionShortcut)
        .create(REGION_NAME);
    mockDeltaSessionManager();
    when(deltaSessionManager.getStatistics()).thenReturn(statistics);
    TestDeltaSession deltaSession = new TestDeltaSession(deltaSessionManager, TEST_SESSION_ID);
    httpSessionRegion.put(deltaSession.getId(), deltaSession);

    deltaSession.invalidate();
    await().untilAsserted(() -> assertThat(httpSessionRegion.isEmpty()).isTrue());
    assertThat(statistics.getSessionsInvalidated()).isEqualTo(1);
  }

  static class TestCustomExpiry implements CustomExpiry<String, HttpSession> {
    @Override
    public ExpirationAttributes getExpiry(Region.Entry<String, HttpSession> entry) {
      return new ExpirationAttributes(1, ExpirationAction.DESTROY);
    }
  }
}

@@ -1112,16 +1112,138 @@
 o.mock;%0A
+import static org.mockito.Mockito.spy;%0Aimport static org.mockito.Mockito.times;%0Aimport static org.mockito.Mockito.verify;%0A
 import s
@@ -1757,32 +1757,95 @@
 ctSessionCache;%0A
+import org.apache.geode.modules.session.catalina.DeltaSession;%0A
 import org.apach
@@ -3831,55 +3831,44 @@
     
-http
+Delta
 Session
-Region.put(TEST_SESSION_ID,%0A        
+ spyDeltasSession = spy(
 new 
@@ -3928,40 +3928,160 @@
 ));%0A
-%0A    await().untilAsserted(() -%3E
+    httpSessionRegion.put(TEST_SESSION_ID, spyDeltasSession);%0A%0A    await().untilAsserted(() -%3E verify(spyDeltasSession, times(1)).processExpired());%0A   
  ass
@@ -4117,33 +4117,32 @@
 mpty()).isTrue()
-)
 ;%0A    assertThat

Fix DeltaSessionStatisticsIntegrationTest (#4316)