You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by jo...@apache.org on 2014/08/27 16:04:59 UTC

[18/35] git commit: AMBARI-7002 - Views: Creating instance for a view should respond with 409 conflict if that instance already exists

AMBARI-7002 - Views: Creating instance for a view should respond with 409 conflict if that instance already exists


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/d566bcaf
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/d566bcaf
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/d566bcaf

Branch: refs/heads/branch-alerts-dev
Commit: d566bcaf6338803fb1a91762333c3b2aec23f1d0
Parents: 6d098ca
Author: tbeerbower <tb...@hortonworks.com>
Authored: Sun Aug 24 21:14:29 2014 -0400
Committer: tbeerbower <tb...@hortonworks.com>
Committed: Tue Aug 26 12:24:40 2014 -0400

----------------------------------------------------------------------
 .../internal/ViewInstanceResourceProvider.java  |  9 +-
 .../apache/ambari/server/view/ViewRegistry.java | 15 ++++
 .../ViewInstanceResourceProviderTest.java       | 89 ++++++++++++++++++--
 3 files changed, 107 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/d566bcaf/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ViewInstanceResourceProvider.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ViewInstanceResourceProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ViewInstanceResourceProvider.java
index ae02f0a..ce23e79 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ViewInstanceResourceProvider.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ViewInstanceResourceProvider.java
@@ -19,6 +19,7 @@
 package org.apache.ambari.server.controller.internal;
 
 import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.DuplicateResourceException;
 import org.apache.ambari.server.controller.spi.NoSuchParentResourceException;
 import org.apache.ambari.server.controller.spi.NoSuchResourceException;
 import org.apache.ambari.server.controller.spi.Predicate;
@@ -332,7 +333,13 @@ public class ViewInstanceResourceProvider extends AbstractResourceProvider {
       @Override
       public Void invoke() throws AmbariException {
         try {
-          ViewRegistry.getInstance().installViewInstance(toEntity(properties));
+          ViewRegistry       viewRegistry   = ViewRegistry.getInstance();
+          ViewInstanceEntity instanceEntity = toEntity(properties);
+
+          if (viewRegistry.instanceExists(instanceEntity)) {
+            throw new DuplicateResourceException("The instance " + instanceEntity.getName() + " already exists.");
+          }
+          viewRegistry.installViewInstance(instanceEntity);
         } catch (org.apache.ambari.view.SystemException e) {
           throw new AmbariException("Caught exception trying to create view instance.", e);
         }

http://git-wip-us.apache.org/repos/asf/ambari/blob/d566bcaf/ambari-server/src/main/java/org/apache/ambari/server/view/ViewRegistry.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/view/ViewRegistry.java b/ambari-server/src/main/java/org/apache/ambari/server/view/ViewRegistry.java
index 7542876..6e5f0a2 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/view/ViewRegistry.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/view/ViewRegistry.java
@@ -466,6 +466,21 @@ public class ViewRegistry {
   }
 
   /**
+   * Determine whether or not the given view instance exists.
+   *
+   * @param instanceEntity  the view instance entity
+   *
+   * @return true if the the given view instance exists; false otherwise
+   */
+  public boolean instanceExists(ViewInstanceEntity instanceEntity) {
+
+    ViewEntity viewEntity = getDefinition(instanceEntity.getViewName());
+
+    return viewEntity != null &&
+        (getInstanceDefinition(viewEntity.getCommonName(), viewEntity.getVersion(), instanceEntity.getName()) != null);
+  }
+
+  /**
    * Install the given view instance with its associated view.
    *
    * @param instanceEntity  the view instance entity

http://git-wip-us.apache.org/repos/asf/ambari/blob/d566bcaf/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ViewInstanceResourceProviderTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ViewInstanceResourceProviderTest.java b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ViewInstanceResourceProviderTest.java
index 52f0231..f409f07 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ViewInstanceResourceProviderTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ViewInstanceResourceProviderTest.java
@@ -18,21 +18,23 @@
 
 package org.apache.ambari.server.controller.internal;
 
-import org.apache.ambari.server.controller.spi.Predicate;
-import org.apache.ambari.server.controller.spi.Request;
 import org.apache.ambari.server.controller.spi.Resource;
+import org.apache.ambari.server.controller.spi.ResourceAlreadyExistsException;
+import org.apache.ambari.server.controller.utilities.PropertyHelper;
 import org.apache.ambari.server.orm.entities.ViewEntity;
 import org.apache.ambari.server.orm.entities.ViewInstanceDataEntity;
 import org.apache.ambari.server.orm.entities.ViewInstanceEntity;
 import org.apache.ambari.server.orm.entities.ViewInstancePropertyEntity;
 import org.apache.ambari.server.orm.entities.ViewParameterEntity;
-import org.easymock.EasyMock;
+import org.apache.ambari.server.view.ViewRegistry;
+import org.easymock.Capture;
+import org.junit.Assert;
+import org.junit.Before;
 import org.junit.Test;
 
-import java.util.ArrayList;
 import java.util.Arrays;
-import java.util.Collection;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
@@ -42,6 +44,17 @@ import static org.easymock.EasyMock.*;
 
 public class ViewInstanceResourceProviderTest {
 
+  private static final ViewRegistry singleton = createMock(ViewRegistry.class);
+
+  static {
+    ViewRegistry.initInstance(singleton);
+  }
+
+  @Before
+  public void before() {
+    reset(singleton);
+  }
+
   @Test
   public void testToResource() throws Exception {
     ViewInstanceResourceProvider provider = new ViewInstanceResourceProvider();
@@ -79,4 +92,70 @@ public class ViewInstanceResourceProviderTest {
     assertEquals("val3", props.get("par3"));
     assertNull(props.get("par2"));
   }
+
+  @Test
+  public void testCreateResources() throws Exception {
+    ViewInstanceResourceProvider provider = new ViewInstanceResourceProvider();
+
+    Set<Map<String, Object>> properties = new HashSet<Map<String, Object>>();
+
+    Map<String, Object> propertyMap = new HashMap<String, Object>();
+
+    propertyMap.put(ViewInstanceResourceProvider.VIEW_NAME_PROPERTY_ID, "V1");
+    propertyMap.put(ViewInstanceResourceProvider.VIEW_VERSION_PROPERTY_ID, "1.0.0");
+    propertyMap.put(ViewInstanceResourceProvider.INSTANCE_NAME_PROPERTY_ID, "I1");
+
+    properties.add(propertyMap);
+
+    ViewInstanceEntity viewInstanceEntity = new ViewInstanceEntity();
+    viewInstanceEntity.setViewName("V1{1.0.0}");
+    viewInstanceEntity.setName("I1");
+
+    expect(singleton.instanceExists(viewInstanceEntity)).andReturn(false);
+    expect(singleton.getInstanceDefinition("V1", "1.0.0", "I1")).andReturn(viewInstanceEntity);
+
+    Capture<ViewInstanceEntity> instanceEntityCapture = new Capture<ViewInstanceEntity>();
+    singleton.installViewInstance(capture(instanceEntityCapture));
+
+    replay(singleton);
+
+    provider.createResources(PropertyHelper.getCreateRequest(properties, null));
+
+    Assert.assertEquals(viewInstanceEntity, instanceEntityCapture.getValue());
+
+    verify(singleton);
+  }
+
+  @Test
+  public void testCreateResources_existingInstance() throws Exception {
+    ViewInstanceResourceProvider provider = new ViewInstanceResourceProvider();
+
+    Set<Map<String, Object>> properties = new HashSet<Map<String, Object>>();
+
+    Map<String, Object> propertyMap = new HashMap<String, Object>();
+
+    propertyMap.put(ViewInstanceResourceProvider.VIEW_NAME_PROPERTY_ID, "V1");
+    propertyMap.put(ViewInstanceResourceProvider.VIEW_VERSION_PROPERTY_ID, "1.0.0");
+    propertyMap.put(ViewInstanceResourceProvider.INSTANCE_NAME_PROPERTY_ID, "I1");
+
+    properties.add(propertyMap);
+
+    ViewInstanceEntity viewInstanceEntity = new ViewInstanceEntity();
+    viewInstanceEntity.setViewName("V1{1.0.0}");
+    viewInstanceEntity.setName("I1");
+
+    expect(singleton.instanceExists(viewInstanceEntity)).andReturn(true);
+    expect(singleton.getInstanceDefinition("V1", "1.0.0", "I1")).andReturn(viewInstanceEntity);
+
+    replay(singleton);
+
+    try {
+      provider.createResources(PropertyHelper.getCreateRequest(properties, null));
+      fail("Expected ResourceAlreadyExistsException.");
+    } catch (ResourceAlreadyExistsException e) {
+      // expected
+    }
+
+    verify(singleton);
+  }
 }
\ No newline at end of file