/*
 * 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.internal.cache.tx;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import org.apache.geode.cache.RegionDestroyedException;
import org.apache.geode.distributed.internal.membership.InternalDistributedMember;
import org.apache.geode.internal.cache.CacheDistributionAdvisor;
import org.apache.geode.internal.cache.DistributedRegion;
import org.apache.geode.internal.cache.EntryEventImpl;
import org.apache.geode.internal.cache.RemoteOperationException;

@PowerMockIgnore("*.UnitTest")
@RunWith(PowerMockRunner.class)
@PrepareForTest(RemotePutMessage.class)
public class RemotePutMessageTest {
  @Test
  public void testDistributeNotFailWithRegionDestroyedException() throws RemoteOperationException {
    EntryEventImpl event = mock(EntryEventImpl.class);
    DistributedRegion region = mock(DistributedRegion.class);
    CacheDistributionAdvisor advisor = mock(CacheDistributionAdvisor.class);
    InternalDistributedMember member = mock(InternalDistributedMember.class);
    Set<InternalDistributedMember> replicates = new HashSet<>(Arrays.asList(member));
    RemotePutMessage.RemotePutResponse response = mock(RemotePutMessage.RemotePutResponse.class);
    Object expectedOldValue = new Object();

    when(event.getRegion()).thenReturn(region);
    when(region.getCacheDistributionAdvisor()).thenReturn(advisor);
    when(advisor.adviseInitializedReplicates()).thenReturn(replicates);
    when(response.waitForResult()).thenThrow(new RegionDestroyedException("", ""));

    PowerMockito.mockStatic(RemotePutMessage.class);
    PowerMockito
        .when(RemotePutMessage.distribute(event, 1, false, false, expectedOldValue, false, false))
        .thenCallRealMethod();
    PowerMockito.when(RemotePutMessage.send(member, region, event, 1, false, false,
        expectedOldValue, false, false, false)).thenReturn(response);

    RemotePutMessage.distribute(event, 1, false, false, expectedOldValue, false, false);
  }
}

@@ -828,16 +828,64 @@
 he.tx;%0A%0A
+import static org.mockito.ArgumentMatchers.any;%0A
 import s
@@ -1050,24 +1050,25 @@
 junit.Test;%0A
+%0A
 import org.j
@@ -1070,264 +1070,160 @@
 org.
-junit.runner.RunWith;%0Aimport org.powermock.api.mockito.PowerMockito;%0Aimport org.powermock.core.classloader.annotations.PowerMockIgnore;%0Aimport org.powermock.core.classloader.annotations.PrepareForTest;%0Aimport org.powermock.modules.junit4.PowerMockRunn
+apache.geode.CancelCriterion;%0Aimport org.apache.geode.cache.RegionDestroyedException;%0Aimport org.apache.geode.distributed.internal.DistributionManag
 er;%0A
-%0A
 impo
@@ -1246,38 +1246,54 @@
 ode.
-cache.RegionDestroyedException
+distributed.internal.InternalDistributedSystem
 ;%0Aim
@@ -1551,16 +1551,64 @@
 ntImpl;%0A
+import org.apache.geode.internal.cache.EventID;%0A
 import o
@@ -1669,111 +1669,8 @@
 n;%0A%0A
-@PowerMockIgnore(%22*.UnitTest%22)%0A@RunWith(PowerMockRunner.class)%0A@PrepareForTest(RemotePutMessage.class)%0A
 publ
@@ -1860,24 +1860,67 @@
 mpl.class);%0A
+    EventID eventID = mock(EventID.class);%0A
     Distribu
@@ -1965,24 +1965,215 @@
 ion.class);%0A
+    InternalDistributedSystem ids = mock(InternalDistributedSystem.class);%0A    DistributionManager dm = mock(DistributionManager.class);%0A    CancelCriterion cc = mock(CancelCriterion.class);%0A
     CacheDis
@@ -2592,16 +2592,66 @@
 egion);%0A
+    when(event.getEventId()).thenReturn(eventID);%0A
     when
@@ -2870,338 +2870,246 @@
 ));%0A
-%0A
     
-PowerMockito.mockStatic(RemotePutMessage.class);%0A    PowerMockito%0A        .when(RemotePutMessage.distribute(event, 1, false, false, expectedOldValue, false, false))%0A        .thenCallRealMethod();%0A    PowerMockito.when(RemotePutMessage.send(member, region, event, 1, false, false,%0A        expectedOldValue, false, false, false
+when(region.getSystem()).thenReturn(ids);%0A    when(region.getDistributionManager()).thenReturn(dm);%0A    when(ids.getDistributionManager()).thenReturn(dm);%0A    when(dm.getCancelCriterion()).thenReturn(cc);%0A    when(dm.putOutgoing(any()
 )).t
@@ -3118,24 +3118,20 @@
 nReturn(
-response
+null
 );%0A%0A    

remove PowerMock from RemotePutMessageTest