ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nc...@apache.org
Subject ambari git commit: AMBARI-21828. The BE should be providing repository versions in the order of version string (ncole)
Date Tue, 29 Aug 2017 13:18:08 GMT
Repository: ambari
Updated Branches:
  refs/heads/branch-2.6 61d223a53 -> 7ca3a4d63


AMBARI-21828. The BE should be providing repository versions in the order of version string
(ncole)


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

Branch: refs/heads/branch-2.6
Commit: 7ca3a4d630eca9a2f06896b27c4ab4595e49af5e
Parents: 61d223a
Author: Nate Cole <ncole@hortonworks.com>
Authored: Mon Aug 28 10:01:46 2017 -0400
Committer: Nate Cole <ncole@hortonworks.com>
Committed: Tue Aug 29 09:10:19 2017 -0400

----------------------------------------------------------------------
 .../ClusterStackVersionResourceProvider.java    |  22 +++-
 ...ClusterStackVersionResourceProviderTest.java | 114 +++++++++++++++++++
 2 files changed, 131 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/7ca3a4d6/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ClusterStackVersionResourceProvider.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ClusterStackVersionResourceProvider.java
b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ClusterStackVersionResourceProvider.java
index 7a53e91..2083365 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ClusterStackVersionResourceProvider.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ClusterStackVersionResourceProvider.java
@@ -21,10 +21,12 @@ import static org.apache.ambari.server.agent.ExecutionCommand.KeyNames.JDK_LOCAT
 
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.Comparator;
 import java.util.EnumSet;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
+import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -199,7 +201,7 @@ public class ClusterStackVersionResourceProvider extends AbstractControllerResou
     comment = "this is a fake response until the UI no longer uses the endpoint")
   public Set<Resource> getResourcesAuthorized(Request request, Predicate predicate)
throws
       SystemException, UnsupportedPropertyException, NoSuchResourceException, NoSuchParentResourceException
{
-    final Set<Resource> resources = new HashSet<>();
+    final Set<Resource> resources = new LinkedHashSet<>();
 
     final Set<String> requestedIds = getRequestPropertyIds(request, predicate);
     final Set<Map<String, Object>> propertyMaps = getPropertyMaps(predicate);
@@ -218,7 +220,7 @@ public class ClusterStackVersionResourceProvider extends AbstractControllerResou
       throw new SystemException(e.getMessage(), e);
     }
 
-    Set<Long> requestedEntities = new HashSet<>();
+    Set<Long> requestedEntities = new LinkedHashSet<>();
 
     if (propertyMap.containsKey(CLUSTER_STACK_VERSION_ID_PROPERTY_ID)) {
       Long id = Long.parseLong(propertyMap.get(CLUSTER_STACK_VERSION_ID_PROPERTY_ID).toString());
@@ -226,6 +228,13 @@ public class ClusterStackVersionResourceProvider extends AbstractControllerResou
     } else {
       List<RepositoryVersionEntity> entities = repositoryVersionDAO.findAll();
 
+      Collections.sort(entities, new Comparator<RepositoryVersionEntity>() {
+        @Override
+        public int compare(RepositoryVersionEntity o1, RepositoryVersionEntity o2) {
+          return compareVersions(o1.getVersion(), o2.getVersion());
+        }
+      });
+
       for (RepositoryVersionEntity entity : entities) {
         requestedEntities.add(entity.getId());
       }
@@ -235,7 +244,6 @@ public class ClusterStackVersionResourceProvider extends AbstractControllerResou
       throw new SystemException("Could not find any repositories to show");
     }
 
-
     for (Long repositoryVersionId : requestedEntities) {
       final Resource resource = new ResourceImpl(Resource.Type.ClusterStackVersion);
 
@@ -262,8 +270,9 @@ public class ClusterStackVersionResourceProvider extends AbstractControllerResou
       ClusterVersionSummary versionSummary = null;
       try {
         VersionDefinitionXml vdf = repositoryVersion.getRepositoryXml();
-
-        versionSummary = vdf.getClusterSummary(cluster);
+        if (null != vdf) {
+          versionSummary = vdf.getClusterSummary(cluster);
+        }
       } catch (Exception e) {
         throw new IllegalArgumentException(
             String.format("Version %s is backed by a version definition, but it could not
be parsed", repositoryVersion.getVersion()), e);
@@ -721,4 +730,7 @@ public class ClusterStackVersionResourceProvider extends AbstractControllerResou
 
     return (compare == 0) ? 0 : (compare < 0) ? -1 : 1;
   }
+
+
+
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/7ca3a4d6/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ClusterStackVersionResourceProviderTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ClusterStackVersionResourceProviderTest.java
b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ClusterStackVersionResourceProviderTest.java
index 282f159..bd6de5e 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ClusterStackVersionResourceProviderTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ClusterStackVersionResourceProviderTest.java
@@ -31,6 +31,7 @@ import static org.easymock.EasyMock.verify;
 
 import java.io.File;
 import java.io.FileInputStream;
+import java.lang.reflect.Field;
 import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -61,10 +62,12 @@ import org.apache.ambari.server.controller.AmbariManagementController;
 import org.apache.ambari.server.controller.ExecuteActionRequest;
 import org.apache.ambari.server.controller.RequestStatusResponse;
 import org.apache.ambari.server.controller.ResourceProviderFactory;
+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.ResourceProvider;
+import org.apache.ambari.server.controller.utilities.PredicateBuilder;
 import org.apache.ambari.server.controller.utilities.PropertyHelper;
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
 import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
@@ -110,12 +113,14 @@ import org.springframework.security.core.Authentication;
 import org.springframework.security.core.context.SecurityContextHolder;
 
 import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Sets;
 import com.google.gson.JsonArray;
 import com.google.gson.JsonObject;
 import com.google.gson.JsonParser;
 import com.google.inject.AbstractModule;
 import com.google.inject.Guice;
 import com.google.inject.Injector;
+import com.google.inject.Provider;
 import com.google.inject.util.Modules;
 
 
@@ -1694,6 +1699,115 @@ public class ClusterStackVersionResourceProviderTest {
     Assert.assertEquals(Float.valueOf(0.85f), successFactor);
   }
 
+  @Test
+  public void testGetSorted() throws Exception {
+
+    Resource.Type type = Resource.Type.ClusterStackVersion;
+
+    final Clusters clusters = createNiceMock(Clusters.class);
+    Cluster cluster = createNiceMock(Cluster.class);
+    StackId stackId = new StackId("HDP", "2.2.0");
+
+    StackEntity stackEntity = new StackEntity();
+    stackEntity.setStackName(stackId.getStackName());
+    stackEntity.setStackVersion(stackId.getStackVersion());
+
+    RequestStatusResponse response = createNiceMock(RequestStatusResponse.class);
+    ResourceProviderFactory resourceProviderFactory = createNiceMock(ResourceProviderFactory.class);
+    ResourceProvider csvResourceProvider = createNiceMock(
+        ClusterStackVersionResourceProvider.class);
+
+    AbstractControllerResourceProvider.init(resourceProviderFactory);
+
+    expect(clusters.getCluster(anyObject(String.class))).andReturn(cluster);
+
+    expect(cluster.getClusterId()).andReturn(1L).anyTimes();
+
+    String[] versionStrings = new String[] {
+      "2.1.0.0-15",  // idx 0, sorted to 0
+      "2.1.0.5-17",  // idx 1, sorted to 2
+      "2.1.1.5-19",  // idx 2, sorted to 3
+      "2.1.0.3-14",  // idx 3, sorted to 1
+      "2.1.1.5-74"   // idx 4, sorted to 4
+    };
+
+    List<RepositoryVersionEntity> repoVersionList = new ArrayList<>();
+    for (int i = 0; i < versionStrings.length; i++) {
+      Long id = new Long(i);
+
+      RepositoryVersionEntity repoVersion = createNiceMock(RepositoryVersionEntity.class);
+      expect(repoVersion.getVersion()).andReturn(versionStrings[i]).anyTimes();
+      expect(repoVersion.getStack()).andReturn(stackEntity).anyTimes();
+      expect(repoVersion.getId()).andReturn(id).anyTimes();
+      expect(repositoryVersionDAOMock.findByPK(id)).andReturn(repoVersion).anyTimes();
+
+      repoVersionList.add(repoVersion);
+
+      replay(repoVersion);
+    }
+
+    expect(repositoryVersionDAOMock.findAll()).andReturn(repoVersionList).atLeastOnce();
+
+    expect(hostVersionDAO.findHostVersionByClusterAndRepository(
+        anyLong(), anyObject(RepositoryVersionEntity.class))).andReturn(Collections.<HostVersionEntity>emptyList()).anyTimes();
+
+    // replay
+    replay(response, clusters, resourceProviderFactory,
+        csvResourceProvider, cluster, repositoryVersionDAOMock, configHelper,
+        stageFactory, hostVersionDAO);
+
+    ResourceProvider provider = AbstractControllerResourceProvider.getResourceProvider(type,
+        PropertyHelper.getPropertyIds(type), PropertyHelper.getKeyPropertyIds(type),
+        /*managementController*/null);
+    injector.injectMembers(provider);
+
+    Field field = ClusterStackVersionResourceProvider.class.getDeclaredField("clusters");
+    field.setAccessible(true);
+    field.set(null, new Provider<Clusters>() {
+      @Override
+      public Clusters get() {
+        return clusters;
+      }
+    });
+
+    // set the security auth
+    SecurityContextHolder.getContext().setAuthentication(
+        TestAuthenticationFactory.createAdministrator());
+
+    Set<String> ids = Sets.newHashSet(
+        ClusterStackVersionResourceProvider.CLUSTER_STACK_VERSION_STATE_PROPERTY_ID,
+        ClusterStackVersionResourceProvider.CLUSTER_STACK_VERSION_VERSION_PROPERTY_ID,
+        ClusterStackVersionResourceProvider.CLUSTER_STACK_VERSION_REPOSITORY_VERSION_PROPERTY_ID);
+
+    // get cluster named Cluster100
+    Predicate predicate = new PredicateBuilder()
+        .property(ClusterStackVersionResourceProvider.CLUSTER_STACK_VERSION_CLUSTER_NAME_PROPERTY_ID).equals("Cluster100")
+        .toPredicate();
+
+    // create the request
+    Request request = PropertyHelper.getReadRequest(ids);
+
+    Set<Resource> responses = provider.getResources(request, predicate);
+    Assert.assertNotNull(responses);
+
+    // verify
+    verify(response, clusters, cluster, hostVersionDAO);
+
+    Assert.assertEquals(5, responses.size());
+
+    int i = 0;
+    // see the string array above.  this is the order matching the sorted strings
+    long[] orders = new long[] { 0, 3, 1, 2, 4 };
+    for (Resource res : responses) {
+      Assert.assertEquals(orders[i], res.getPropertyValue(
+          ClusterStackVersionResourceProvider.CLUSTER_STACK_VERSION_REPOSITORY_VERSION_PROPERTY_ID));
+
+      i++;
+    }
+
+
+  }
+
 
    private void testCreateResourcesExistingUpgrade(Authentication authentication) throws
Exception {
     Resource.Type type = Resource.Type.ClusterStackVersion;


Mime
View raw message