ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nc...@apache.org
Subject [13/37] ambari git commit: AMBARI-21450. Initial cherry-picking for feature branch (ncole)
Date Thu, 13 Jul 2017 19:14:31 GMT
http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/PreUpgradeCheckResourceProviderTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/PreUpgradeCheckResourceProviderTest.java
b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/PreUpgradeCheckResourceProviderTest.java
index a41fb0f..2a48fa6 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/PreUpgradeCheckResourceProviderTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/PreUpgradeCheckResourceProviderTest.java
@@ -1,4 +1,4 @@
-/**
+/*
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
  * distributed with this work for additional information
@@ -101,10 +101,9 @@ public class PreUpgradeCheckResourceProviderTest {
     ServiceFactory serviceFactory = createNiceMock(ServiceFactory.class);
     AmbariMetaInfo ambariMetaInfo = createNiceMock(AmbariMetaInfo.class);
 
-    String stackName = "Stack100";
-    Map<String, Service> allServiceMap = new HashMap<String, Service>();
+    Map<String, Service> allServiceMap = new HashMap<>();
     allServiceMap.put("Service100", service);
-    Map<String, ServiceInfo> allServiceInfoMap = new HashMap<String, ServiceInfo>();
+    Map<String, ServiceInfo> allServiceInfoMap = new HashMap<>();
     allServiceInfoMap.put("Service100", serviceInfo);
 
     expect(configuration.isUpgradePrecheckBypass()).andReturn(false).anyTimes();
@@ -117,23 +116,23 @@ public class PreUpgradeCheckResourceProviderTest {
     expect(cluster.getService("Service100")).andReturn(service).anyTimes();
     expect(cluster.getCurrentStackVersion()).andReturn(currentStackId).anyTimes();
 
-    expect(currentStackId.getStackName()).andReturn(stackName).anyTimes();
+    expect(currentStackId.getStackName()).andReturn("Stack100").anyTimes();
     expect(currentStackId.getStackVersion()).andReturn("1.0").anyTimes();
-    expect(targetStackId.getStackName()).andReturn(stackName).anyTimes();
+    expect(targetStackId.getStackName()).andReturn("Stack100").anyTimes();
     expect(targetStackId.getStackVersion()).andReturn("1.1").anyTimes();
 
-    expect(repoDao.findByStackNameAndVersion(stackName, "Repo100")).andReturn(repo).anyTimes();
+    expect(repoDao.findByStackNameAndVersion("Stack100", "Repo100")).andReturn(repo).anyTimes();
     expect(repo.getStackId()).andReturn(targetStackId).atLeastOnce();
-    expect(upgradeHelper.suggestUpgradePack("Cluster100", "1.0", "Repo100", Direction.UPGRADE,
UpgradeType.NON_ROLLING, stackName, "upgrade_pack11")).andReturn(upgradePack);
+    expect(upgradeHelper.suggestUpgradePack("Cluster100", "1.0", "Repo100", Direction.UPGRADE,
UpgradeType.NON_ROLLING, "upgrade_pack11")).andReturn(upgradePack);
 
-    List<AbstractCheckDescriptor> upgradeChecksToRun = new LinkedList<AbstractCheckDescriptor>();
-    List<String> prerequisiteChecks = new LinkedList<String>();
+    List<AbstractCheckDescriptor> upgradeChecksToRun = new LinkedList<>();
+    List<String> prerequisiteChecks = new LinkedList<>();
     prerequisiteChecks.add("org.apache.ambari.server.sample.checks.SampleServiceCheck");
     expect(upgradePack.getPrerequisiteCheckConfig()).andReturn(config);
     expect(upgradePack.getPrerequisiteChecks()).andReturn(prerequisiteChecks).anyTimes();
     expect(upgradePack.getTarget()).andReturn("1.1.*.*").anyTimes();
 
-    expect(ambariMetaInfo.getServices(stackName, "1.0")).andReturn(allServiceInfoMap).anyTimes();
+    expect(ambariMetaInfo.getServices("Stack100", "1.0")).andReturn(allServiceInfoMap).anyTimes();
     String checks = ClassLoader.getSystemClassLoader().getResource("checks").getPath();
     expect(serviceInfo.getChecksFolder()).andReturn(new File(checks));
 
@@ -148,7 +147,6 @@ public class PreUpgradeCheckResourceProviderTest {
     Predicate predicate = builder.property(PreUpgradeCheckResourceProvider.UPGRADE_CHECK_CLUSTER_NAME_PROPERTY_ID).equals("Cluster100").and()
         .property(PreUpgradeCheckResourceProvider.UPGRADE_CHECK_UPGRADE_PACK_PROPERTY_ID).equals("upgrade_pack11").and()
         .property(PreUpgradeCheckResourceProvider.UPGRADE_CHECK_UPGRADE_TYPE_PROPERTY_ID).equals(UpgradeType.NON_ROLLING).and()
-        .property(PreUpgradeCheckResourceProvider.UPGRADE_CHECK_TARGET_STACK_ID).equals(stackName
+ "-100").and()
         .property(PreUpgradeCheckResourceProvider.UPGRADE_CHECK_REPOSITORY_VERSION_PROPERTY_ID).equals("Repo100").toPredicate();
 
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/RepositoryVersionResourceProviderTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/RepositoryVersionResourceProviderTest.java
b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/RepositoryVersionResourceProviderTest.java
index 0230a97..6bc8b95 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/RepositoryVersionResourceProviderTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/RepositoryVersionResourceProviderTest.java
@@ -1,4 +1,4 @@
-/**
+/*
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
  * distributed with this work for additional information
@@ -19,7 +19,6 @@
 package org.apache.ambari.server.controller.internal;
 
 import java.sql.SQLException;
-import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.LinkedHashMap;
@@ -40,11 +39,8 @@ 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;
-import org.apache.ambari.server.orm.dao.ClusterVersionDAO;
 import org.apache.ambari.server.orm.dao.RepositoryVersionDAO;
 import org.apache.ambari.server.orm.dao.StackDAO;
-import org.apache.ambari.server.orm.entities.ClusterEntity;
-import org.apache.ambari.server.orm.entities.ClusterVersionEntity;
 import org.apache.ambari.server.orm.entities.OperatingSystemEntity;
 import org.apache.ambari.server.orm.entities.RepositoryEntity;
 import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
@@ -54,13 +50,13 @@ import org.apache.ambari.server.security.authorization.AuthorizationException;
 import org.apache.ambari.server.state.Clusters;
 import org.apache.ambari.server.state.OperatingSystemInfo;
 import org.apache.ambari.server.state.RepositoryInfo;
-import org.apache.ambari.server.state.RepositoryVersionState;
 import org.apache.ambari.server.state.ServiceInfo;
 import org.apache.ambari.server.state.StackId;
 import org.apache.ambari.server.state.StackInfo;
 import org.apache.ambari.server.state.stack.UpgradePack;
 import org.apache.ambari.server.state.stack.upgrade.RepositoryVersionHelper;
 import org.junit.After;
+import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
 import org.mockito.Mockito;
@@ -73,57 +69,28 @@ import com.google.common.collect.Sets;
 import com.google.gson.Gson;
 import com.google.inject.Guice;
 import com.google.inject.Injector;
-
-import junit.framework.Assert;
-
 /**
  * RepositoryVersionResourceProvider tests.
  */
 public class RepositoryVersionResourceProviderTest {
 
-  private ClusterVersionDAO clusterVersionDAO;
-
   private static Injector injector;
 
   private static String jsonStringRedhat6 = "[{\"OperatingSystems\":{\"os_type\":\"redhat6\"},\"repositories\":[]}]";
   private static String jsonStringRedhat7 = "[{\"OperatingSystems\":{\"os_type\":\"redhat7\"},\"repositories\":[]}]";
 
-  private List<ClusterVersionEntity> getNoClusterVersions() {
-    final List<ClusterVersionEntity> emptyList = new ArrayList<ClusterVersionEntity>();
-    return emptyList;
-  }
-
-  private List<ClusterVersionEntity> getInstallFailedClusterVersions() {
-    ClusterEntity cluster = new ClusterEntity();
-    cluster.setClusterName("c1");
-    cluster.setClusterId(1L);
-
-    final List<ClusterVersionEntity> clusterVersions = new ArrayList<ClusterVersionEntity>();
-    final RepositoryVersionEntity repositoryVersion = new RepositoryVersionEntity();
-    repositoryVersion.setId(1L);
-    final ClusterVersionEntity installFailedVersion = new ClusterVersionEntity();
-    installFailedVersion.setState(RepositoryVersionState.INSTALL_FAILED);
-    installFailedVersion.setRepositoryVersion(repositoryVersion);
-    installFailedVersion.setClusterEntity(cluster);
-    clusterVersions.add(installFailedVersion);
-    cluster.setClusterVersionEntities(clusterVersions);
-    return clusterVersions;
-  }
-
   @Before
   public void before() throws Exception {
     final Set<String> validVersions = Sets.newHashSet("1.1", "1.1-17", "1.1.1.1", "1.1.343432.2",
"1.1.343432.2-234234324");
-    final Set<StackInfo> stacks = new HashSet<StackInfo>();
+    final Set<StackInfo> stacks = new HashSet<>();
 
     final AmbariMetaInfo ambariMetaInfo = Mockito.mock(AmbariMetaInfo.class);
-    clusterVersionDAO = Mockito.mock(ClusterVersionDAO.class);
 
     final InMemoryDefaultTestModule injectorModule = new InMemoryDefaultTestModule() {
       @Override
       protected void configure() {
         super.configure();
         bind(AmbariMetaInfo.class).toInstance(ambariMetaInfo);
-        bind(ClusterVersionDAO.class).toInstance(clusterVersionDAO);
       };
     };
     injector = Guice.createInjector(injectorModule);
@@ -131,7 +98,7 @@ public class RepositoryVersionResourceProviderTest {
     final StackInfo stackInfo = new StackInfo() {
       @Override
       public Map<String, UpgradePack> getUpgradePacks() {
-        final Map<String, UpgradePack> map = new HashMap<String, UpgradePack>();
+        final Map<String, UpgradePack> map = new HashMap<>();
         final UpgradePack pack1 = new UpgradePack() {
           @Override
           public String getName() {
@@ -194,7 +161,7 @@ public class RepositoryVersionResourceProviderTest {
 
     });
 
-    final HashSet<OperatingSystemInfo> osInfos = new HashSet<OperatingSystemInfo>();
+    final HashSet<OperatingSystemInfo> osInfos = new HashSet<>();
     osInfos.add(new OperatingSystemInfo("redhat6"));
     osInfos.add(new OperatingSystemInfo("redhat7"));
     Mockito.when(ambariMetaInfo.getOperatingSystems(Mockito.anyString(), Mockito.anyString())).thenAnswer(new
Answer<Set<OperatingSystemInfo>>() {
@@ -207,22 +174,7 @@ public class RepositoryVersionResourceProviderTest {
         if (stack.equals("HDP") && validVersions.contains(version)) {
           return osInfos;
         } else {
-          return new HashSet<OperatingSystemInfo>();
-        }
-      }
-    });
-
-    Mockito.when(clusterVersionDAO.findByStackAndVersion(Mockito.anyString(), Mockito.anyString(),
Mockito.anyString())).thenAnswer(
-        new Answer<List<ClusterVersionEntity>>() {
-      @Override
-      public List<ClusterVersionEntity> answer(InvocationOnMock invocation) throws
Throwable {
-        final String stack = invocation.getArguments()[0].toString();
-        final String version = invocation.getArguments()[1].toString();
-
-        if (stack.equals("HDP-1.1") && version.equals("1.1.1.1")) {
-          return getNoClusterVersions();
-        } else {
-          return getInstallFailedClusterVersions();
+          return new HashSet<>();
         }
       }
     });
@@ -257,8 +209,8 @@ public class RepositoryVersionResourceProviderTest {
 
     final ResourceProvider provider = injector.getInstance(ResourceProviderFactory.class).getRepositoryVersionResourceProvider();
 
-    final Set<Map<String, Object>> propertySet = new LinkedHashSet<Map<String,
Object>>();
-    final Map<String, Object> properties = new LinkedHashMap<String, Object>();
+    final Set<Map<String, Object>> propertySet = new LinkedHashSet<>();
+    final Map<String, Object> properties = new LinkedHashMap<>();
     properties.put(RepositoryVersionResourceProvider.REPOSITORY_VERSION_DISPLAY_NAME_PROPERTY_ID,
"name");
     properties.put(RepositoryVersionResourceProvider.SUBRESOURCE_OPERATING_SYSTEMS_PROPERTY_ID,
new Gson().fromJson("[{\"OperatingSystems/os_type\":\"redhat6\",\"repositories\":[{\"Repositories/repo_id\":\"1\",\"Repositories/repo_name\":\"1\",\"Repositories/base_url\":\"1\",\"Repositories/unique\":\"true\"}]}]",
Object.class));
     properties.put(RepositoryVersionResourceProvider.REPOSITORY_VERSION_STACK_NAME_PROPERTY_ID,
"HDP");
@@ -421,8 +373,8 @@ public class RepositoryVersionResourceProviderTest {
 
     final ResourceProvider provider = injector.getInstance(ResourceProviderFactory.class).getRepositoryVersionResourceProvider();
 
-    final Set<Map<String, Object>> propertySet = new LinkedHashSet<Map<String,
Object>>();
-    final Map<String, Object> properties = new LinkedHashMap<String, Object>();
+    final Set<Map<String, Object>> propertySet = new LinkedHashSet<>();
+    final Map<String, Object> properties = new LinkedHashMap<>();
     properties.put(RepositoryVersionResourceProvider.REPOSITORY_VERSION_DISPLAY_NAME_PROPERTY_ID,
"name");
     properties.put(RepositoryVersionResourceProvider.SUBRESOURCE_OPERATING_SYSTEMS_PROPERTY_ID,
new Gson().fromJson("[{\"OperatingSystems/os_type\":\"redhat6\",\"repositories\":[{\"Repositories/repo_id\":\"1\",\"Repositories/repo_name\":\"1\",\"Repositories/base_url\":\"1\",\"Repositories/unique\":\"true\"}]}]",
Object.class));
     properties.put(RepositoryVersionResourceProvider.REPOSITORY_VERSION_STACK_NAME_PROPERTY_ID,
"HDP");
@@ -461,16 +413,8 @@ public class RepositoryVersionResourceProviderTest {
 
     final ResourceProvider provider = injector.getInstance(ResourceProviderFactory.class).getRepositoryVersionResourceProvider();
 
-    Mockito.when(clusterVersionDAO.findByStackAndVersion(Mockito.anyString(), Mockito.anyString(),
Mockito.anyString())).thenAnswer(
-        new Answer<List<ClusterVersionEntity>>() {
-          @Override
-          public List<ClusterVersionEntity> answer(InvocationOnMock invocation) throws
Throwable {
-            return getNoClusterVersions();
-          }
-        });
-
-    final Set<Map<String, Object>> propertySet = new LinkedHashSet<Map<String,
Object>>();
-    final Map<String, Object> properties = new LinkedHashMap<String, Object>();
+    final Set<Map<String, Object>> propertySet = new LinkedHashSet<>();
+    final Map<String, Object> properties = new LinkedHashMap<>();
     properties.put(RepositoryVersionResourceProvider.REPOSITORY_VERSION_DISPLAY_NAME_PROPERTY_ID,
"name");
     properties.put(RepositoryVersionResourceProvider.SUBRESOURCE_OPERATING_SYSTEMS_PROPERTY_ID,
new Gson().fromJson("[{\"OperatingSystems/os_type\":\"redhat6\",\"repositories\":[{\"Repositories/repo_id\":\"1\",\"Repositories/repo_name\":\"1\",\"Repositories/base_url\":\"http://example.com/repo1\",\"Repositories/unique\":\"true\"}]}]",
Object.class));
     properties.put(RepositoryVersionResourceProvider.REPOSITORY_VERSION_STACK_NAME_PROPERTY_ID,
"HDP");
@@ -521,14 +465,6 @@ public class RepositoryVersionResourceProviderTest {
 
     properties.put(RepositoryVersionResourceProvider.SUBRESOURCE_OPERATING_SYSTEMS_PROPERTY_ID,
new Gson().fromJson("[{\"OperatingSystems/os_type\":\"redhat6\",\"repositories\":[{\"Repositories/repo_id\":\"2\",\"Repositories/repo_name\":\"2\",\"Repositories/base_url\":\"2\",\"Repositories/unique\":\"true\"}]}]",
Object.class));
     provider.updateResources(updateRequest, new AndPredicate(predicateStackName, predicateStackVersion));
-    // Now, insert a cluster version whose state is INSTALL_FAILED, so the operation will
not be permitted.
-    Mockito.when(clusterVersionDAO.findByStackAndVersion(Mockito.anyString(), Mockito.anyString(),
Mockito.anyString())).thenAnswer(
-      new Answer<List<ClusterVersionEntity>>() {
-        @Override
-        public List<ClusterVersionEntity> answer(InvocationOnMock invocation) throws
Throwable {
-          return getInstallFailedClusterVersions();
-        }
-      });
 
     try {
       provider.updateResources(updateRequest, new AndPredicate(predicateStackName, predicateStackVersion));
@@ -545,16 +481,8 @@ public class RepositoryVersionResourceProviderTest {
 
     final ResourceProvider provider = injector.getInstance(ResourceProviderFactory.class).getRepositoryVersionResourceProvider();
 
-    Mockito.when(clusterVersionDAO.findByStackAndVersion(Mockito.anyString(), Mockito.anyString(),
Mockito.anyString())).thenAnswer(
-        new Answer<List<ClusterVersionEntity>>() {
-          @Override
-          public List<ClusterVersionEntity> answer(InvocationOnMock invocation) throws
Throwable {
-            return getNoClusterVersions();
-          }
-        });
-
-    final Set<Map<String, Object>> propertySet = new LinkedHashSet<Map<String,
Object>>();
-    final Map<String, Object> properties = new LinkedHashMap<String, Object>();
+    final Set<Map<String, Object>> propertySet = new LinkedHashSet<>();
+    final Map<String, Object> properties = new LinkedHashMap<>();
     properties.put(RepositoryVersionResourceProvider.REPOSITORY_VERSION_DISPLAY_NAME_PROPERTY_ID,
"name");
     properties.put(RepositoryVersionResourceProvider.SUBRESOURCE_OPERATING_SYSTEMS_PROPERTY_ID,
new Gson().fromJson("[{\"OperatingSystems/os_type\":\"redhat6\",\"repositories\":[{\"Repositories/repo_id\":\"1\",\"Repositories/repo_name\":\"1\",\"Repositories/base_url\":\"http://example.com/repo1\",\"Repositories/unique\":\"true\"}]}]",
Object.class));
     properties.put(RepositoryVersionResourceProvider.REPOSITORY_VERSION_STACK_NAME_PROPERTY_ID,
"HDP");

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ServiceResourceProviderTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ServiceResourceProviderTest.java
b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ServiceResourceProviderTest.java
index 716be62..a0c58ea 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ServiceResourceProviderTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ServiceResourceProviderTest.java
@@ -1,4 +1,4 @@
-/**
+/*
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
  * distributed with this work for additional information
@@ -62,6 +62,8 @@ import org.apache.ambari.server.controller.spi.SystemException;
 import org.apache.ambari.server.controller.utilities.PredicateBuilder;
 import org.apache.ambari.server.controller.utilities.PropertyHelper;
 import org.apache.ambari.server.metadata.RoleCommandOrder;
+import org.apache.ambari.server.orm.dao.RepositoryVersionDAO;
+import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
 import org.apache.ambari.server.security.TestAuthenticationFactory;
 import org.apache.ambari.server.security.authorization.AuthorizationException;
 import org.apache.ambari.server.serveraction.kerberos.KerberosAdminAuthenticationException;
@@ -112,7 +114,7 @@ public class ServiceResourceProviderTest {
     Clusters clusters = createNiceMock(Clusters.class);
     Cluster cluster = createNiceMock(Cluster.class);
     Service service = createNiceMock(Service.class);
-    StackId stackId = createNiceMock(StackId.class);
+    StackId stackId = new StackId("HDP-2.5");
     ServiceFactory serviceFactory = createNiceMock(ServiceFactory.class);
     AmbariMetaInfo ambariMetaInfo = createNiceMock(AmbariMetaInfo.class);
     ServiceInfo serviceInfo = createNiceMock(ServiceInfo.class);
@@ -120,7 +122,8 @@ public class ServiceResourceProviderTest {
     expect(managementController.getClusters()).andReturn(clusters).anyTimes();
     expect(managementController.getAmbariMetaInfo()).andReturn(ambariMetaInfo).anyTimes();
 
-    expect(cluster.addService("Service100")).andReturn(service);
+    expect(cluster.addService(eq("Service100"),
+        EasyMock.anyObject(RepositoryVersionEntity.class))).andReturn(service);
 
     expect(clusters.getCluster("Cluster100")).andReturn(cluster).anyTimes();
 
@@ -128,29 +131,29 @@ public class ServiceResourceProviderTest {
     expect(cluster.getDesiredStackVersion()).andReturn(stackId).anyTimes();
     expect(cluster.getClusterId()).andReturn(2L).anyTimes();
 
-    expect(stackId.getStackName()).andReturn("HDP").anyTimes();
-    expect(stackId.getStackVersion()).andReturn("2.5").anyTimes();
-
     expect(ambariMetaInfo.isValidService( (String) anyObject(), (String) anyObject(), (String)
anyObject())).andReturn(true);
     expect(ambariMetaInfo.getService((String)anyObject(), (String)anyObject(), (String)anyObject())).andReturn(serviceInfo).anyTimes();
 
     // replay
-    replay(managementController, clusters, cluster, service, ambariMetaInfo, stackId, serviceFactory,
serviceInfo);
+    replay(managementController, clusters, cluster, service,
+        ambariMetaInfo, serviceFactory, serviceInfo);
 
     SecurityContextHolder.getContext().setAuthentication(authentication);
 
-    ResourceProvider provider = getServiceProvider(managementController);
+    ResourceProvider provider = getServiceProvider(managementController, true);
 
     // add the property map to a set for the request.  add more maps for multiple creates
-    Set<Map<String, Object>> propertySet = new LinkedHashSet<Map<String,
Object>>();
+    Set<Map<String, Object>> propertySet = new LinkedHashSet<>();
 
     // Service 1: create a map of properties for the request
-    Map<String, Object> properties = new LinkedHashMap<String, Object>();
+    Map<String, Object> properties = new LinkedHashMap<>();
 
     // add properties to the request map
     properties.put(ServiceResourceProvider.SERVICE_CLUSTER_NAME_PROPERTY_ID, "Cluster100");
     properties.put(ServiceResourceProvider.SERVICE_SERVICE_NAME_PROPERTY_ID, "Service100");
     properties.put(ServiceResourceProvider.SERVICE_SERVICE_STATE_PROPERTY_ID, "INIT");
+    properties.put(ServiceResourceProvider.SERVICE_DESIRED_STACK_PROPERTY_ID, "HDP-1.1");
+    properties.put(ServiceResourceProvider.SERVICE_DESIRED_REPO_VERSION_ID_PROPERTY_ID, "1");
 
     propertySet.add(properties);
 
@@ -160,7 +163,8 @@ public class ServiceResourceProviderTest {
     provider.createResources(request);
 
     // verify
-    verify(managementController, clusters, cluster, service, ambariMetaInfo, stackId, serviceFactory,
serviceInfo);
+    verify(managementController, clusters, cluster, service,
+        ambariMetaInfo, serviceFactory, serviceInfo);
   }
 
   @Test
@@ -197,7 +201,7 @@ public class ServiceResourceProviderTest {
     ServiceFactory serviceFactory = createNiceMock(ServiceFactory.class);
     AmbariMetaInfo ambariMetaInfo = createNiceMock(AmbariMetaInfo.class);
 
-    Map<String, Service> allResponseMap = new HashMap<String, Service>();
+    Map<String, Service> allResponseMap = new HashMap<>();
     allResponseMap.put("Service100", service0);
     allResponseMap.put("Service101", service1);
     allResponseMap.put("Service102", service2);
@@ -254,7 +258,7 @@ public class ServiceResourceProviderTest {
 
     ResourceProvider provider = getServiceProvider(managementController);
 
-    Set<String> propertyIds = new HashSet<String>();
+    Set<String> propertyIds = new HashSet<>();
 
     propertyIds.add(ServiceResourceProvider.SERVICE_CLUSTER_NAME_PROPERTY_ID);
     propertyIds.add(ServiceResourceProvider.SERVICE_SERVICE_NAME_PROPERTY_ID);
@@ -265,7 +269,7 @@ public class ServiceResourceProviderTest {
     Set<Resource> resources = provider.getResources(request, predicate);
 
     Assert.assertEquals(5, resources.size());
-    Set<String> names = new HashSet<String>();
+    Set<String> names = new HashSet<>();
     for (Resource resource : resources) {
       String clusterName = (String) resource.getPropertyValue(ServiceResourceProvider.SERVICE_CLUSTER_NAME_PROPERTY_ID);
       Assert.assertEquals("Cluster100", clusterName);
@@ -292,7 +296,7 @@ public class ServiceResourceProviderTest {
     resources = provider.getResources(request, predicate);
 
     Assert.assertEquals(3, resources.size());
-    names = new HashSet<String>();
+    names = new HashSet<>();
     for (Resource resource : resources) {
       String clusterName = (String) resource.getPropertyValue(ServiceResourceProvider.SERVICE_CLUSTER_NAME_PROPERTY_ID);
       Assert.assertEquals("Cluster100", clusterName);
@@ -319,7 +323,7 @@ public class ServiceResourceProviderTest {
     AmbariMetaInfo ambariMetaInfo = createNiceMock(AmbariMetaInfo.class);
     KerberosHelper kerberosHeper = createStrictMock(KerberosHelper.class);
 
-    Map<String, Service> allResponseMap = new HashMap<String, Service>();
+    Map<String, Service> allResponseMap = new HashMap<>();
     allResponseMap.put("KERBEROS", service0);
 
     // set expectations
@@ -387,7 +391,7 @@ public class ServiceResourceProviderTest {
     AmbariMetaInfo ambariMetaInfo = createNiceMock(AmbariMetaInfo.class);
     KerberosHelper kerberosHelper = createStrictMock(KerberosHelper.class);
 
-    Map<String, Service> allResponseMap = new HashMap<String, Service>();
+    Map<String, Service> allResponseMap = new HashMap<>();
     allResponseMap.put("KERBEROS", service0);
 
     // set expectations
@@ -454,7 +458,7 @@ public class ServiceResourceProviderTest {
     AmbariMetaInfo ambariMetaInfo = createNiceMock(AmbariMetaInfo.class);
     KerberosHelper kerberosHeper = createStrictMock(KerberosHelper.class);
 
-    Map<String, Service> allResponseMap = new HashMap<String, Service>();
+    Map<String, Service> allResponseMap = new HashMap<>();
     allResponseMap.put("KERBEROS", service0);
 
     // set expectations
@@ -523,13 +527,13 @@ public class ServiceResourceProviderTest {
     AmbariMetaInfo ambariMetaInfo = createNiceMock(AmbariMetaInfo.class);
     KerberosHelper kerberosHeper = createStrictMock(KerberosHelper.class);
 
-    Map<String, Service> allResponseMap = new HashMap<String, Service>();
+    Map<String, Service> allResponseMap = new HashMap<>();
     allResponseMap.put("KERBEROS", service0);
 
     // set expectations
     expect(managementController.getClusters()).andReturn(clusters).anyTimes();
     expect(managementController.getAmbariMetaInfo()).andReturn(ambariMetaInfo).anyTimes();
-    expect(managementController.getHostComponents((Set<ServiceComponentHostRequest>)
anyObject())).
+    expect(managementController.getHostComponents(EasyMock.<Set<ServiceComponentHostRequest>>anyObject())).
         andReturn(Collections.<ServiceComponentHostResponse>emptySet()).anyTimes();
 
     expect(clusters.getCluster("Cluster100")).andReturn(cluster).anyTimes();
@@ -596,6 +600,7 @@ public class ServiceResourceProviderTest {
 
   private void testUpdateResources(Authentication authentication) throws Exception{
     MaintenanceStateHelper maintenanceStateHelper = createNiceMock(MaintenanceStateHelper.class);
+    RepositoryVersionDAO repositoryVersionDAO = createNiceMock(RepositoryVersionDAO.class);
     AmbariManagementController managementController = createMock(AmbariManagementController.class);
     Clusters clusters = createNiceMock(Clusters.class);
     Cluster cluster = createNiceMock(Cluster.class);
@@ -608,7 +613,7 @@ public class ServiceResourceProviderTest {
     StackId stackId = createNiceMock(StackId.class);
     ServiceInfo serviceInfo = createNiceMock(ServiceInfo.class);
 
-    Map<String, String> mapRequestProps = new HashMap<String, String>();
+    Map<String, String> mapRequestProps = new HashMap<>();
     mapRequestProps.put("context", "Called from a test");
 
     // set expectations
@@ -626,7 +631,7 @@ public class ServiceResourceProviderTest {
     expect(stackId.getStackId()).andReturn("HDP-2.5").anyTimes();
     expect(stackId.getStackName()).andReturn("HDP").anyTimes();
     expect(stackId.getStackVersion()).andReturn("2.5").anyTimes();
-    expect(service0.getDesiredStackVersion()).andReturn(stackId).anyTimes();
+    expect(service0.getDesiredStackId()).andReturn(stackId).anyTimes();
     expect(service0.getName()).andReturn("Service102").anyTimes();
     expect(serviceInfo.isCredentialStoreSupported()).andReturn(true).anyTimes();
     expect(serviceInfo.isCredentialStoreEnabled()).andReturn(false).anyTimes();
@@ -656,14 +661,16 @@ public class ServiceResourceProviderTest {
 
     // replay
     replay(managementController, clusters, cluster, rco, maintenanceStateHelper,
-        service0, serviceFactory, ambariMetaInfo, requestStages, requestStatusResponse, stackId,
serviceInfo);
+        repositoryVersionDAO, service0, serviceFactory, ambariMetaInfo, requestStages,
+        requestStatusResponse, stackId, serviceInfo);
 
     SecurityContextHolder.getContext().setAuthentication(authentication);
 
-    ServiceResourceProvider provider = getServiceProvider(managementController, maintenanceStateHelper);
+    ServiceResourceProvider provider = getServiceProvider(managementController,
+        maintenanceStateHelper, repositoryVersionDAO);
 
     // add the property map to a set for the request.
-    Map<String, Object> properties = new LinkedHashMap<String, Object>();
+    Map<String, Object> properties = new LinkedHashMap<>();
 
     properties.put(ServiceResourceProvider.SERVICE_SERVICE_STATE_PROPERTY_ID, "STARTED");
 
@@ -697,6 +704,7 @@ public class ServiceResourceProviderTest {
 
   private void testReconfigureClientsFlag(Authentication authentication) throws Exception
{
     MaintenanceStateHelper maintenanceStateHelper = createNiceMock(MaintenanceStateHelper.class);
+    RepositoryVersionDAO repositoryVersionDAO = createNiceMock(RepositoryVersionDAO.class);
     AmbariManagementController managementController1 = createMock(AmbariManagementController.class);
     AmbariManagementController managementController2 = createMock
         (AmbariManagementController.class);
@@ -716,7 +724,7 @@ public class ServiceResourceProviderTest {
     StackId stackId = createNiceMock(StackId.class);
     ServiceInfo serviceInfo = createNiceMock(ServiceInfo.class);
 
-    Map<String, String> mapRequestProps = new HashMap<String, String>();
+    Map<String, String> mapRequestProps = new HashMap<>();
     mapRequestProps.put("context", "Called from a test");
 
     // set expectations
@@ -739,7 +747,7 @@ public class ServiceResourceProviderTest {
     expect(stackId.getStackId()).andReturn("HDP-2.5").anyTimes();
     expect(stackId.getStackName()).andReturn("HDP").anyTimes();
     expect(stackId.getStackVersion()).andReturn("2.5").anyTimes();
-    expect(service0.getDesiredStackVersion()).andReturn(stackId).anyTimes();
+    expect(service0.getDesiredStackId()).andReturn(stackId).anyTimes();
     expect(service0.getName()).andReturn("Service102").anyTimes();
     expect(serviceInfo.isCredentialStoreSupported()).andReturn(true).anyTimes();
     expect(serviceInfo.isCredentialStoreEnabled()).andReturn(false).anyTimes();
@@ -787,17 +795,20 @@ public class ServiceResourceProviderTest {
     andReturn(Collections.<Service>emptySet()).anyTimes();
 
     // replay
-    replay(managementController1, response1, managementController2, requestStages1, requestStages2,
response2,
-        clusters, cluster, service0, serviceResponse0, ambariMetaInfo, rco, maintenanceStateHelper,
stackId, serviceInfo);
+    replay(managementController1, response1, managementController2, requestStages1, requestStages2,
+        response2, clusters, cluster, service0, serviceResponse0, ambariMetaInfo, rco,
+        maintenanceStateHelper, repositoryVersionDAO, stackId, serviceInfo);
 
     SecurityContextHolder.getContext().setAuthentication(authentication);
 
-    ServiceResourceProvider provider1 = getServiceProvider(managementController1, maintenanceStateHelper);
+    ServiceResourceProvider provider1 = getServiceProvider(managementController1,
+        maintenanceStateHelper, repositoryVersionDAO);
 
-    ServiceResourceProvider provider2 = getServiceProvider(managementController2, maintenanceStateHelper);
+    ServiceResourceProvider provider2 = getServiceProvider(managementController2,
+        maintenanceStateHelper, repositoryVersionDAO);
 
     // add the property map to a set for the request.
-    Map<String, Object> properties = new LinkedHashMap<String, Object>();
+    Map<String, Object> properties = new LinkedHashMap<>();
 
     properties.put(ServiceResourceProvider.SERVICE_SERVICE_STATE_PROPERTY_ID,
       "STARTED");
@@ -943,7 +954,7 @@ public class ServiceResourceProviderTest {
     Cluster cluster = createNiceMock(Cluster.class);
     Service service = createNiceMock(Service.class);
     ServiceComponent sc = createNiceMock(ServiceComponent.class);
-    Map<String, ServiceComponent> scMap = new HashMap<String, ServiceComponent>();
+    Map<String, ServiceComponent> scMap = new HashMap<>();
     scMap.put("Component100", sc);
     State componentState = State.STARTED;
     ServiceComponentHost sch = createNiceMock(ServiceComponentHost.class);
@@ -1022,7 +1033,7 @@ public class ServiceResourceProviderTest {
     TestComponent component2 = new TestComponent("Component101", createNiceMock(ServiceComponent.class),
State.STARTED);
     TestComponent component3 = new TestComponent("Component102", createNiceMock(ServiceComponent.class),
State.STARTED);
 
-    Map<String, ServiceComponent> scMap = new HashMap<String, ServiceComponent>();
+    Map<String, ServiceComponent> scMap = new HashMap<>();
     scMap.put(component1.Name, component1.Component);
     scMap.put(component2.Name, component2.Component);
     scMap.put(component3.Name, component3.Component);
@@ -1108,46 +1119,53 @@ public class ServiceResourceProviderTest {
 
   @Test
   public void testCheckPropertyIds() throws Exception {
-    Set<String> propertyIds = new HashSet<String>();
-    propertyIds.add("foo");
-    propertyIds.add("cat1/foo");
-    propertyIds.add("cat2/bar");
-    propertyIds.add("cat2/baz");
-    propertyIds.add("cat3/sub1/bam");
-    propertyIds.add("cat4/sub2/sub3/bat");
-    propertyIds.add("cat5/subcat5/map");
-
-    Map<Resource.Type, String> keyPropertyIds = new HashMap<Resource.Type, String>();
-
     AmbariManagementController managementController = createMock(AmbariManagementController.class);
 
     MaintenanceStateHelper maintenanceStateHelperMock = createNiceMock(MaintenanceStateHelper.class);
-    AbstractResourceProvider provider = new ServiceResourceProvider(propertyIds,
-        keyPropertyIds,
-        managementController, maintenanceStateHelperMock);
+    RepositoryVersionDAO repositoryVersionDAO = createNiceMock(RepositoryVersionDAO.class);
+    replay(maintenanceStateHelperMock, repositoryVersionDAO);
+
+    AbstractResourceProvider provider = new ServiceResourceProvider(managementController,
+        maintenanceStateHelperMock, repositoryVersionDAO);
+
+    Set<String> unsupported = provider.checkPropertyIds(
+        Collections.singleton(ServiceResourceProvider.SERVICE_CLUSTER_NAME_PROPERTY_ID));
 
-    Set<String> unsupported = provider.checkPropertyIds(Collections.singleton("foo"));
     Assert.assertTrue(unsupported.isEmpty());
 
     // note that key is not in the set of known property ids.  We allow it if its parent
is a known property.
     // this allows for Map type properties where we want to treat the entries as individual
properties
-    Assert.assertTrue(provider.checkPropertyIds(Collections.singleton("cat5/subcat5/map/key")).isEmpty());
+    String subKey = PropertyHelper.getPropertyId(ServiceResourceProvider.SERVICE_CLUSTER_NAME_PROPERTY_ID,
"key");
+    unsupported = provider.checkPropertyIds(Collections.singleton(subKey));
+    Assert.assertTrue(unsupported.isEmpty());
 
     unsupported = provider.checkPropertyIds(Collections.singleton("bar"));
     Assert.assertEquals(1, unsupported.size());
     Assert.assertTrue(unsupported.contains("bar"));
 
-    unsupported = provider.checkPropertyIds(Collections.singleton("cat1/foo"));
-    Assert.assertTrue(unsupported.isEmpty());
+    for (String propertyId : provider.getPKPropertyIds()) {
+      unsupported = provider.checkPropertyIds(Collections.singleton(propertyId));
+      Assert.assertTrue(unsupported.isEmpty());
+    }
+  }
 
-    unsupported = provider.checkPropertyIds(Collections.singleton("cat1"));
-    Assert.assertTrue(unsupported.isEmpty());
+  private static ServiceResourceProvider getServiceProvider(AmbariManagementController managementController,
boolean mockFindByStack) throws  AmbariException {
+    MaintenanceStateHelper maintenanceStateHelperMock = createNiceMock(MaintenanceStateHelper.class);
+    RepositoryVersionDAO repositoryVersionDAO = createNiceMock(RepositoryVersionDAO.class);
+    expect(maintenanceStateHelperMock.isOperationAllowed(anyObject(Resource.Type.class),
anyObject(Service.class))).andReturn(true).anyTimes();
+    expect(maintenanceStateHelperMock.isOperationAllowed(anyObject(Resource.Type.class),
anyObject(ServiceComponentHost.class))).andReturn(true).anyTimes();
 
-    unsupported = provider.checkPropertyIds(Collections.singleton("config"));
-    Assert.assertTrue(unsupported.isEmpty());
+    if (mockFindByStack) {
+      RepositoryVersionEntity repositoryVersion = createNiceMock(RepositoryVersionEntity.class);
 
-    unsupported = provider.checkPropertyIds(Collections.singleton("config/unknown_property"));
-    Assert.assertTrue(unsupported.isEmpty());
+      expect(repositoryVersionDAO.findByPK(EasyMock.anyLong())).andReturn(repositoryVersion).atLeastOnce();
+
+      expect(repositoryVersion.getStackId()).andReturn(new StackId("HDP-2.2")).anyTimes();
+      replay(repositoryVersion);
+    }
+
+    replay(maintenanceStateHelperMock, repositoryVersionDAO);
+    return getServiceProvider(managementController, maintenanceStateHelperMock, repositoryVersionDAO);
   }
 
   /**
@@ -1155,27 +1173,24 @@ public class ServiceResourceProviderTest {
    * It's useful in most cases (when we don't care about Maintenance State)
    */
   public static ServiceResourceProvider getServiceProvider(AmbariManagementController managementController)
throws  AmbariException {
-    MaintenanceStateHelper maintenanceStateHelperMock = createNiceMock(MaintenanceStateHelper.class);
-    expect(maintenanceStateHelperMock.isOperationAllowed(anyObject(Resource.Type.class),
anyObject(Service.class))).andReturn(true).anyTimes();
-    expect(maintenanceStateHelperMock.isOperationAllowed(anyObject(Resource.Type.class),
anyObject(ServiceComponentHost.class))).andReturn(true).anyTimes();
-    replay(maintenanceStateHelperMock);
-    return getServiceProvider(managementController, maintenanceStateHelperMock);
+    return getServiceProvider(managementController, false);
   }
 
   /**
    * This factory method allows to define custom MaintenanceStateHelper mock.
    */
-  public static ServiceResourceProvider getServiceProvider(AmbariManagementController managementController,
-                                                           MaintenanceStateHelper maintenanceStateHelper)
{
+  public static ServiceResourceProvider getServiceProvider(
+      AmbariManagementController managementController,
+      MaintenanceStateHelper maintenanceStateHelper, RepositoryVersionDAO repositoryVersionDAO)
{
     Resource.Type type = Resource.Type.Service;
-    return new ServiceResourceProvider(PropertyHelper.getPropertyIds(type),
-            PropertyHelper.getKeyPropertyIds(type),
-            managementController, maintenanceStateHelper);
+    return new ServiceResourceProvider(managementController, maintenanceStateHelper, repositoryVersionDAO);
   }
 
-  public static void createServices(AmbariManagementController controller, Set<ServiceRequest>
requests)
+  public static void createServices(AmbariManagementController controller,
+      RepositoryVersionDAO repositoryVersionDAO, Set<ServiceRequest> requests)
       throws AmbariException, AuthorizationException {
-    ServiceResourceProvider provider = getServiceProvider(controller);
+    MaintenanceStateHelper maintenanceStateHelperMock = createNiceMock(MaintenanceStateHelper.class);
+    ServiceResourceProvider provider = getServiceProvider(controller, maintenanceStateHelperMock,
repositoryVersionDAO);
     provider.createServices(requests);
   }
 
@@ -1205,7 +1220,7 @@ public class ServiceResourceProviderTest {
       throws AmbariException, AuthorizationException {
     ServiceResourceProvider provider;
     if (maintenanceStateHelper != null) {
-      provider = getServiceProvider(controller, maintenanceStateHelper);
+      provider = getServiceProvider(controller, maintenanceStateHelper, null);
     } else {
       provider = getServiceProvider(controller);
     }


Mime
View raw message