ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nc...@apache.org
Subject [27/32] ambari git commit: AMBARI-13296. Credential Resource API should return an empty list rather than a 404 error (rlevas)
Date Fri, 02 Oct 2015 21:12:31 GMT
AMBARI-13296. Credential Resource API should return an empty list rather than a 404 error (rlevas)


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

Branch: refs/heads/branch-dev-patch-upgrade
Commit: 4b437ea4f1461d6bec197b7d748b7aa86f7e89f6
Parents: 3fbdd9d
Author: Robert Levas <rlevas@hortonworks.com>
Authored: Fri Oct 2 11:38:11 2015 -0400
Committer: Robert Levas <rlevas@hortonworks.com>
Committed: Fri Oct 2 11:38:11 2015 -0400

----------------------------------------------------------------------
 .../internal/CredentialResourceProvider.java    | 11 ++++++-
 .../CredentialResourceProviderTest.java         | 32 ++++++++++++++++++++
 2 files changed, 42 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/4b437ea4/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/CredentialResourceProvider.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/CredentialResourceProvider.java
b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/CredentialResourceProvider.java
index 55f26a0..52783c9 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/CredentialResourceProvider.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/CredentialResourceProvider.java
@@ -119,6 +119,7 @@ public class CredentialResourceProvider extends AbstractControllerResourceProvid
 
     Set<String> requestedIds = getRequestPropertyIds(request, predicate);
     Set<Resource> resources = new HashSet<Resource>();
+    boolean sendNotFoundErrorIfEmpty = false;
 
     for (Map<String, Object> propertyMap : getPropertyMaps(predicate)) {
       String clusterName = (String) propertyMap.get(CREDENTIAL_CLUSTER_NAME_PROPERTY_ID);
@@ -133,6 +134,14 @@ public class CredentialResourceProvider extends AbstractControllerResourceProvid
           if (credentialStoreService.containsCredential(clusterName, alias)) {
             resources.add(toResource(clusterName, alias, credentialStoreService.getCredentialStoreType(clusterName,
alias), requestedIds));
           }
+          else {
+            // Only throw a NoSuchResourceException if a specific item is being requested
and it
+            // wasn't found. If multiple resources are queried, one or may not exist and
this
+            // sendNotFoundErrorIfEmpty will be set to true.  However if at least one resource
is
+            // found, the resources Set will not be empty and NoSuchResourceException will
not be
+            // thrown
+            sendNotFoundErrorIfEmpty = true;
+          }
         } catch (AmbariException e) {
           throw new SystemException(e.getLocalizedMessage(), e);
         }
@@ -150,7 +159,7 @@ public class CredentialResourceProvider extends AbstractControllerResourceProvid
       }
     }
 
-    if ((predicate != null) && resources.isEmpty()) {
+    if (sendNotFoundErrorIfEmpty && resources.isEmpty()) {
       throw new NoSuchResourceException("The requested resource doesn't exist: Credential
not found, " + predicate);
     }
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/4b437ea4/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/CredentialResourceProviderTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/CredentialResourceProviderTest.java
b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/CredentialResourceProviderTest.java
index 04ce22a..5f32d39 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/CredentialResourceProviderTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/CredentialResourceProviderTest.java
@@ -451,6 +451,38 @@ public class CredentialResourceProviderTest {
 
     verify(request, factory, managementController);
   }
+  @Test
+  public void testGetResources_WithOutPredicateNoResults() throws Exception {
+
+    AmbariManagementController managementController = createMock(AmbariManagementController.class);
+    Request request = createMock(Request.class);
+
+    CredentialResourceProvider credentialResourceProvider = new CredentialResourceProvider(managementController);
+    injector.injectMembers(credentialResourceProvider);
+
+    // Get resources request
+    expect(request.getPropertyIds()).andReturn(null).once();
+
+    ResourceProviderFactory factory = createMock(ResourceProviderFactory.class);
+    expect(factory.getCredentialResourceProvider(anyObject(AmbariManagementController.class))).andReturn(credentialResourceProvider);
+
+    replay(request, factory, managementController);
+    // end expectations
+
+    AbstractControllerResourceProvider.init(factory);
+
+    ResourceProvider provider = AbstractControllerResourceProvider.getResourceProvider(
+        Resource.Type.Credential,
+        PropertyHelper.getPropertyIds(Resource.Type.Credential),
+        PropertyHelper.getKeyPropertyIds(Resource.Type.Credential),
+        managementController);
+
+
+    Set<Resource> results = provider.getResources(request, null);
+    Assert.assertTrue(results.isEmpty());
+
+    verify(request, factory, managementController);
+  }
 
   @Test
   public void testUpdateResources() throws Exception {


Mime
View raw message