ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nc...@apache.org
Subject [30/39] ambari git commit: AMBARI-14835: Return 409 instead of 500 for duplicate setting creation (Ajit Kumar via smnaha)
Date Mon, 01 Feb 2016 15:42:07 GMT
AMBARI-14835: Return 409 instead of 500 for duplicate setting creation (Ajit Kumar via smnaha)


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

Branch: refs/heads/branch-dev-patch-upgrade
Commit: 528e8ea5e05d98866e14dcfbe2d2becf6b7ceb7e
Parents: 1be4a2d
Author: Nahappan Somasundaram <nsomasundaram@hortonworks.com>
Authored: Fri Jan 29 15:50:00 2016 -0800
Committer: Nahappan Somasundaram <nsomasundaram@hortonworks.com>
Committed: Fri Jan 29 17:16:42 2016 -0800

----------------------------------------------------------------------
 .../internal/AdminSettingResourceProvider.java       |  5 +++++
 .../internal/AdminSettingResourceProviderTest.java   | 15 +++++++++++++--
 .../ambari/server/orm/dao/AdminSettingDAOTest.java   |  4 ++++
 3 files changed, 22 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/528e8ea5/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/AdminSettingResourceProvider.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/AdminSettingResourceProvider.java
b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/AdminSettingResourceProvider.java
index f62958f..e8d5b5e 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/AdminSettingResourceProvider.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/AdminSettingResourceProvider.java
@@ -19,6 +19,7 @@ package org.apache.ambari.server.controller.internal;
 
 import com.google.inject.Inject;
 import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.DuplicateResourceException;
 import org.apache.ambari.server.StaticallyInject;
 import org.apache.ambari.server.controller.spi.NoSuchParentResourceException;
 import org.apache.ambari.server.controller.spi.NoSuchResourceException;
@@ -171,6 +172,10 @@ public class AdminSettingResourceProvider extends AbstractAuthorizedResourceProv
       @Override
       public AdminSettingEntity invoke() throws AmbariException, AuthorizationException {
         AdminSettingEntity entity = toEntity(properties);
+        if (dao.findByName(entity.getName()) != null) {
+          throw new DuplicateResourceException(
+                  String.format("Setting already exists. setting name :%s ", entity.getName()));
+        }
         dao.create(entity);
         notifyCreate(Resource.Type.AdminSetting, request);
         return entity;

http://git-wip-us.apache.org/repos/asf/ambari/blob/528e8ea5/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/AdminSettingResourceProviderTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/AdminSettingResourceProviderTest.java
b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/AdminSettingResourceProviderTest.java
index 68eaf28..e95449c 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/AdminSettingResourceProviderTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/AdminSettingResourceProviderTest.java
@@ -18,10 +18,12 @@
 package org.apache.ambari.server.controller.internal;
 
 import com.google.common.collect.Lists;
+import org.apache.ambari.server.DuplicateResourceException;
 import org.apache.ambari.server.controller.spi.Predicate;
 import org.apache.ambari.server.controller.spi.Request;
 import org.apache.ambari.server.controller.spi.RequestStatus;
 import org.apache.ambari.server.controller.spi.Resource;
+import org.apache.ambari.server.controller.spi.ResourceAlreadyExistsException;
 import org.apache.ambari.server.controller.utilities.PredicateBuilder;
 import org.apache.ambari.server.controller.utilities.PropertyHelper;
 import org.apache.ambari.server.orm.dao.AdminSettingDAO;
@@ -190,6 +192,7 @@ public class AdminSettingResourceProviderTest {
     Capture<AdminSettingEntity> entityCapture = Capture.newInstance();
     Request request = createRequest(entity);
 
+    expect(dao.findByName(entity.getName())).andReturn(null);
     dao.create(capture(entityCapture));
     mockControl.replay();
 
@@ -204,6 +207,16 @@ public class AdminSettingResourceProviderTest {
     assertEquals(AuthorizationHelper.getAuthenticatedName(), capturedEntity.getUpdatedBy());
   }
 
+  @Test(expected = ResourceAlreadyExistsException.class)
+  public void testCreateDuplicateResource() throws Exception {
+    setupAuthenticationForAdmin();
+    AdminSettingEntity entity = newEntity("motd");
+    Request request = createRequest(entity);
+
+    expect(dao.findByName(entity.getName())).andReturn(entity);
+    mockControl.replay();
+    resourceProvider.createResources(request);
+  }
 
   @Test(expected = AuthorizationException.class)
   public void testUpdateResources_noAuth() throws Exception {
@@ -211,7 +224,6 @@ public class AdminSettingResourceProviderTest {
     resourceProvider.updateResources(updateRequest(newEntity("motd")), null);
   }
 
-
   @Test(expected = AuthorizationException.class)
   public void testUpdateResources_clusterUser() throws Exception {
     setupAuthenticationForClusterUser();
@@ -219,7 +231,6 @@ public class AdminSettingResourceProviderTest {
     resourceProvider.updateResources(updateRequest(newEntity("motd")), null);
   }
 
-
   @Test
   public void testUpdateResources_admin() throws Exception {
     setupAuthenticationForAdmin();

http://git-wip-us.apache.org/repos/asf/ambari/blob/528e8ea5/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AdminSettingDAOTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AdminSettingDAOTest.java
b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AdminSettingDAOTest.java
index eabbb77..8258974 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AdminSettingDAOTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AdminSettingDAOTest.java
@@ -31,6 +31,7 @@ import java.util.Map;
 import java.util.Objects;
 
 import static junit.framework.Assert.assertEquals;
+import static junit.framework.Assert.assertNull;
 
 public class AdminSettingDAOTest {
   private  Injector injector;
@@ -63,6 +64,9 @@ public class AdminSettingDAOTest {
     retrieveAndValidateSame(entities);
     assertEquals(entities.size(), dao.findAll().size());
 
+    //Should return null if doesn't exist.
+    assertNull(dao.findByName("does-not-exist"));
+
 
     //Update
     for(Map.Entry<String, AdminSettingEntity> entry : entities.entrySet()) {


Mime
View raw message