You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by ds...@apache.org on 2018/11/17 00:18:14 UTC

[geode] branch feature/GEODE-6068 updated: added unit test Also the cluster config is now updated

This is an automated email from the ASF dual-hosted git repository.

dschneider pushed a commit to branch feature/GEODE-6068
in repository https://gitbox.apache.org/repos/asf/geode.git


The following commit(s) were added to refs/heads/feature/GEODE-6068 by this push:
     new 3690001  added unit test Also the cluster config is now updated
3690001 is described below

commit 36900015d7cf2db6f1633a637df819c03007d3a4
Author: Darrel Schneider <ds...@pivotal.io>
AuthorDate: Fri Nov 16 16:17:29 2018 -0800

    added unit test
    Also the cluster config is now updated
---
 .../jdbc/internal/cli/DestroyMappingCommand.java   | 101 +++++++++-
 .../internal/cli/DestroyMappingCommandTest.java    | 222 +++++++++++++++++++++
 2 files changed, 313 insertions(+), 10 deletions(-)

diff --git a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/cli/DestroyMappingCommand.java b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/cli/DestroyMappingCommand.java
index 4bf12fb..41dc55d 100644
--- a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/cli/DestroyMappingCommand.java
+++ b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/cli/DestroyMappingCommand.java
@@ -14,6 +14,9 @@
  */
 package org.apache.geode.connectors.jdbc.internal.cli;
 
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Set;
 
@@ -22,6 +25,13 @@ import org.springframework.shell.core.annotation.CliOption;
 
 import org.apache.geode.annotations.Experimental;
 import org.apache.geode.cache.configuration.CacheConfig;
+import org.apache.geode.cache.configuration.CacheConfig.AsyncEventQueue;
+import org.apache.geode.cache.configuration.CacheElement;
+import org.apache.geode.cache.configuration.DeclarableType;
+import org.apache.geode.cache.configuration.RegionAttributesType;
+import org.apache.geode.cache.configuration.RegionConfig;
+import org.apache.geode.connectors.jdbc.JdbcLoader;
+import org.apache.geode.connectors.jdbc.JdbcWriter;
 import org.apache.geode.connectors.jdbc.internal.configuration.RegionMapping;
 import org.apache.geode.distributed.DistributedMember;
 import org.apache.geode.management.cli.CliMetaData;
@@ -60,17 +70,88 @@ public class DestroyMappingCommand extends SingleGfshCommand {
 
   @Override
   public void updateClusterConfig(String group, CacheConfig cacheConfig, Object configObject) {
-    String region = (String) configObject;
-    RegionMapping existingCacheElement = cacheConfig.findCustomRegionElement("/" + region,
-        RegionMapping.ELEMENT_ID, RegionMapping.class);
+    String regionName = (String) configObject;
+    RegionConfig regionConfig = findRegionConfig(cacheConfig, regionName);
+    if (regionConfig == null) {
+      return;
+    }
+    removeJdbcMappingFromRegion(regionConfig);
+    removeJdbcQueueFromCache(cacheConfig, regionName);
+    RegionAttributesType attributes = getRegionAttributes(regionConfig);
+    removeJdbcLoader(attributes);
+    removeJdbcWriter(attributes);
+    removeJdbcAsyncEventQueueId(attributes, regionName);
+  }
+
+  private void removeJdbcLoader(RegionAttributesType attributes) {
+    DeclarableType cacheLoader = attributes.getCacheLoader();
+    if (cacheLoader != null) {
+      if (JdbcLoader.class.getName().equals(cacheLoader.getClassName())) {
+        attributes.setCacheLoader(null);
+      }
+    }
+  }
+
+  private void removeJdbcWriter(RegionAttributesType attributes) {
+    DeclarableType cacheWriter = attributes.getCacheWriter();
+    if (cacheWriter != null) {
+      if (JdbcWriter.class.getName().equals(cacheWriter.getClassName())) {
+        attributes.setCacheWriter(null);
+      }
+    }
+  }
+
+  private void removeJdbcAsyncEventQueueId(RegionAttributesType attributes, String regionName) {
+    String queueName = CreateMappingCommand.createAsyncEventQueueName(regionName);
+    String queueIds = attributes.getAsyncEventQueueIds();
+    if (queueIds == null) {
+      return;
+    }
+    List<String> queues = new ArrayList<>(Arrays.asList(queueIds.split(",")));
+    if (queues.contains(queueName)) {
+      queues.remove(queueName);
+      String newQueueIds = String.join(",", queues);
+      attributes.setAsyncEventQueueIds(newQueueIds);
+    }
+  }
+
+  private void removeJdbcQueueFromCache(CacheConfig cacheConfig, String regionName) {
+    String queueName = CreateMappingCommand.createAsyncEventQueueName(regionName);
+    Iterator<AsyncEventQueue> iterator = cacheConfig.getAsyncEventQueues().iterator();
+    while (iterator.hasNext()) {
+      AsyncEventQueue queue = iterator.next();
+      if (queueName.equals(queue.getId())) {
+        iterator.remove();
+        break;
+      }
+    }
+  }
+
+  private void removeJdbcMappingFromRegion(RegionConfig regionConfig) {
+    Iterator<CacheElement> iterator = regionConfig.getCustomRegionElements().iterator();
+    while (iterator.hasNext()) {
+      CacheElement element = iterator.next();
+      if (element instanceof RegionMapping) {
+        iterator.remove();
+        break;
+      }
+    }
+  }
+
+  private RegionConfig findRegionConfig(CacheConfig cacheConfig, String regionName) {
+    return cacheConfig.getRegions().stream()
+        .filter(region -> region.getName().equals(regionName)).findFirst().orElse(null);
+  }
 
-    if (existingCacheElement != null) {
-      cacheConfig
-          .getRegions()
-          .stream()
-          .filter(regionConfig -> regionConfig.getName().equals(region))
-          .forEach(
-              regionConfig -> regionConfig.getCustomRegionElements().remove(existingCacheElement));
+  private RegionAttributesType getRegionAttributes(RegionConfig regionConfig) {
+    RegionAttributesType attributes;
+    List<RegionAttributesType> attributesList = regionConfig.getRegionAttributes();
+    if (attributesList.isEmpty()) {
+      attributes = new RegionAttributesType();
+      attributesList.add(attributes);
+    } else {
+      attributes = attributesList.get(0);
     }
+    return attributes;
   }
 }
diff --git a/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/cli/DestroyMappingCommandTest.java b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/cli/DestroyMappingCommandTest.java
new file mode 100644
index 0000000..33cf333
--- /dev/null
+++ b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/cli/DestroyMappingCommandTest.java
@@ -0,0 +1,222 @@
+/*
+ * 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.connectors.jdbc.internal.cli;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import org.apache.geode.cache.configuration.CacheConfig;
+import org.apache.geode.cache.configuration.CacheConfig.AsyncEventQueue;
+import org.apache.geode.cache.configuration.CacheElement;
+import org.apache.geode.cache.configuration.DeclarableType;
+import org.apache.geode.cache.configuration.RegionAttributesDataPolicy;
+import org.apache.geode.cache.configuration.RegionAttributesType;
+import org.apache.geode.cache.configuration.RegionConfig;
+import org.apache.geode.connectors.jdbc.JdbcWriter;
+import org.apache.geode.connectors.jdbc.internal.configuration.RegionMapping;
+import org.apache.geode.distributed.internal.DistributionManager;
+import org.apache.geode.distributed.internal.membership.InternalDistributedMember;
+import org.apache.geode.internal.cache.InternalCache;
+import org.apache.geode.management.cli.Result;
+import org.apache.geode.management.internal.cli.functions.CliFunctionResult;
+import org.apache.geode.management.internal.cli.result.model.ResultModel;
+
+public class DestroyMappingCommandTest {
+
+  private InternalCache cache;
+  private DestroyMappingCommand destroyRegionMappingCommand;
+
+  private String regionName;
+  private DistributionManager distributionManager;
+  private Set<InternalDistributedMember> members;
+  private List<CliFunctionResult> results;
+  private CliFunctionResult successFunctionResult;
+  private RegionMapping mapping;
+  private CacheConfig cacheConfig;
+  RegionConfig matchingRegion;
+  RegionAttributesType matchingRegionAttributes;
+
+  @Before
+  public void setup() {
+    regionName = "regionName";
+    cache = mock(InternalCache.class);
+    distributionManager = mock(DistributionManager.class);
+    when(cache.getDistributionManager()).thenReturn(distributionManager);
+    members = new HashSet<>();
+    members.add(mock(InternalDistributedMember.class));
+    when(distributionManager.getNormalDistributionManagerIds()).thenReturn(members);
+    destroyRegionMappingCommand = spy(DestroyMappingCommand.class);
+    destroyRegionMappingCommand.setCache(cache);
+    results = new ArrayList<>();
+    successFunctionResult = mock(CliFunctionResult.class);
+    when(successFunctionResult.isSuccessful()).thenReturn(true);
+
+    doReturn(results).when(destroyRegionMappingCommand).executeAndGetFunctionResult(any(), any(),
+        any());
+
+    mapping = mock(RegionMapping.class);
+    when(mapping.getRegionName()).thenReturn(regionName);
+
+    cacheConfig = mock(CacheConfig.class);
+
+    matchingRegion = mock(RegionConfig.class);
+    when(matchingRegion.getName()).thenReturn(regionName);
+    List<RegionAttributesType> attributesList = new ArrayList<>();
+    matchingRegionAttributes = mock(RegionAttributesType.class);
+    when(matchingRegionAttributes.getDataPolicy()).thenReturn(RegionAttributesDataPolicy.REPLICATE);
+    attributesList.add(matchingRegionAttributes);
+    when(matchingRegion.getRegionAttributes()).thenReturn(attributesList);
+  }
+
+  @Test
+  public void destroyMappingGivenARegionNameReturnsTheNameAsTheConfigObject() {
+    results.add(successFunctionResult);
+
+    ResultModel result = destroyRegionMappingCommand.destroyMapping(regionName);
+
+    assertThat(result.getStatus()).isSameAs(Result.Status.OK);
+    assertThat(result.getConfigObject()).isEqualTo(regionName);
+  }
+
+  @Test
+  public void updateClusterConfigWithNoRegionsDoesNotThrowException() {
+    when(cacheConfig.getRegions()).thenReturn(Collections.emptyList());
+
+    destroyRegionMappingCommand.updateClusterConfig(null, cacheConfig, regionName);
+  }
+
+  @Test
+  public void updateClusterConfigWithOneNonMatchingRegionDoesNotRemoveMapping() {
+    List<RegionConfig> list = new ArrayList<>();
+    RegionConfig nonMatchingRegion = mock(RegionConfig.class);
+    when(nonMatchingRegion.getName()).thenReturn("nonMatchingRegion");
+    List<CacheElement> listCacheElements = new ArrayList<>();
+    RegionMapping nonMatchingMapping = mock(RegionMapping.class);
+    listCacheElements.add(nonMatchingMapping);
+    when(nonMatchingRegion.getCustomRegionElements()).thenReturn(listCacheElements);
+    list.add(nonMatchingRegion);
+    when(cacheConfig.getRegions()).thenReturn(list);
+
+    destroyRegionMappingCommand.updateClusterConfig(null, cacheConfig, regionName);
+
+    assertThat(listCacheElements).isEqualTo(Arrays.asList(nonMatchingMapping));
+  }
+
+  @Test
+  public void updateClusterConfigWithOneMatchingRegionDoesRemoveMapping() {
+    List<RegionConfig> list = new ArrayList<>();
+    List<CacheElement> listCacheElements = new ArrayList<>();
+    RegionMapping matchingMapping = mock(RegionMapping.class);
+    listCacheElements.add(matchingMapping);
+    when(matchingRegion.getCustomRegionElements()).thenReturn(listCacheElements);
+    list.add(matchingRegion);
+    when(cacheConfig.getRegions()).thenReturn(list);
+
+    destroyRegionMappingCommand.updateClusterConfig(null, cacheConfig, regionName);
+
+    assertThat(listCacheElements).isEmpty();
+  }
+
+  @Test
+  public void updateClusterConfigWithOneMatchingRegionAndJdbcAsyncQueueRemovesTheQueue() {
+    List<RegionConfig> list = new ArrayList<>();
+    List<CacheElement> listCacheElements = new ArrayList<>();
+    RegionMapping matchingMapping = mock(RegionMapping.class);
+    listCacheElements.add(matchingMapping);
+    when(matchingRegion.getCustomRegionElements()).thenReturn(listCacheElements);
+    list.add(matchingRegion);
+    when(cacheConfig.getRegions()).thenReturn(list);
+    AsyncEventQueue queue = mock(AsyncEventQueue.class);
+    String queueName = CreateMappingCommand.createAsyncEventQueueName(regionName);
+    when(queue.getId()).thenReturn(queueName);
+    List<AsyncEventQueue> queueList = new ArrayList<>();
+    queueList.add(queue);
+    when(cacheConfig.getAsyncEventQueues()).thenReturn(queueList);
+
+    destroyRegionMappingCommand.updateClusterConfig(null, cacheConfig, regionName);
+
+    assertThat(queueList).isEmpty();
+  }
+
+  @Test
+  public void updateClusterConfigWithOneMatchingRegionAndJdbcWriterRemovesTheWriter() {
+    List<RegionConfig> list = new ArrayList<>();
+    List<CacheElement> listCacheElements = new ArrayList<>();
+    RegionMapping matchingMapping = mock(RegionMapping.class);
+    listCacheElements.add(matchingMapping);
+    when(matchingRegion.getCustomRegionElements()).thenReturn(listCacheElements);
+    list.add(matchingRegion);
+    when(cacheConfig.getRegions()).thenReturn(list);
+    DeclarableType cacheWriter = mock(DeclarableType.class);
+    when(cacheWriter.getClassName()).thenReturn(JdbcWriter.class.getName());
+    when(matchingRegionAttributes.getCacheWriter()).thenReturn(cacheWriter);
+
+    destroyRegionMappingCommand.updateClusterConfig(null, cacheConfig, regionName);
+
+    verify(matchingRegionAttributes, times(1)).setCacheWriter(null);
+  }
+
+  @Test
+  public void updateClusterConfigWithOneMatchingRegionAndJdbcAsyncQueueIdRemovesTheId() {
+    List<RegionConfig> list = new ArrayList<>();
+    List<CacheElement> listCacheElements = new ArrayList<>();
+    RegionMapping matchingMapping = mock(RegionMapping.class);
+    listCacheElements.add(matchingMapping);
+    when(matchingRegion.getCustomRegionElements()).thenReturn(listCacheElements);
+    list.add(matchingRegion);
+    when(cacheConfig.getRegions()).thenReturn(list);
+    String queueName = CreateMappingCommand.createAsyncEventQueueName(regionName);
+    when(matchingRegionAttributes.getAsyncEventQueueIds()).thenReturn(queueName);
+
+    destroyRegionMappingCommand.updateClusterConfig(null, cacheConfig, regionName);
+
+    verify(matchingRegionAttributes, times(1)).setAsyncEventQueueIds("");
+  }
+
+  @Test
+  public void updateClusterConfigWithOneMatchingRegionAndJdbcAsyncQueueIdsRemovesTheId() {
+    List<RegionConfig> list = new ArrayList<>();
+    List<CacheElement> listCacheElements = new ArrayList<>();
+    RegionMapping matchingMapping = mock(RegionMapping.class);
+    listCacheElements.add(matchingMapping);
+    when(matchingRegion.getCustomRegionElements()).thenReturn(listCacheElements);
+    list.add(matchingRegion);
+    when(cacheConfig.getRegions()).thenReturn(list);
+    String queueName = CreateMappingCommand.createAsyncEventQueueName(regionName);
+    when(matchingRegionAttributes.getAsyncEventQueueIds())
+        .thenReturn(queueName + "1," + queueName + "," + queueName + "2");
+
+    destroyRegionMappingCommand.updateClusterConfig(null, cacheConfig, regionName);
+
+    verify(matchingRegionAttributes, times(1))
+        .setAsyncEventQueueIds(queueName + "1," + queueName + "2");
+  }
+}