Repository: ambari Updated Branches: refs/heads/trunk 41656481b -> e16da8300
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/e16da830 Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/e16da830 Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/e16da830 Branch: refs/heads/trunk Commit: e16da830042e1fea011733da45b24fe6648ee7c2 Parents: 4165648 Author: Nate Cole <nc...@hortonworks.com> Authored: Tue Aug 29 09:25:05 2017 -0400 Committer: Nate Cole <nc...@hortonworks.com> Committed: Tue Aug 29 09:25:05 2017 -0400 ---------------------------------------------------------------------- .../ClusterStackVersionResourceProvider.java | 20 +++- ...ClusterStackVersionResourceProviderTest.java | 114 +++++++++++++++++++ 2 files changed, 129 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/e16da830/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 02cfd24..19f5895 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; @@ -208,7 +210,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); @@ -227,7 +229,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()); @@ -235,6 +237,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()); } @@ -244,7 +253,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); @@ -271,8 +279,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); @@ -819,4 +828,5 @@ public class ClusterStackVersionResourceProvider extends AbstractControllerResou null, amc.getAuthName(), serviceNote); } } + } http://git-wip-us.apache.org/repos/asf/ambari/blob/e16da830/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 5912ae6..f729de1 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; @@ -109,12 +112,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; @@ -1692,6 +1697,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;