ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jonathanhur...@apache.org
Subject [3/9] ambari git commit: AMBARI-20894 - Setting Desired Stack Ids to Correct Values During Service and Patch Upgrades (jonathanhurley)
Date Wed, 03 May 2017 15:13:50 GMT
http://git-wip-us.apache.org/repos/asf/ambari/blob/dcbd826c/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/UpgradeResourceProviderTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/UpgradeResourceProviderTest.java b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/UpgradeResourceProviderTest.java
index e587f28..a814ba8 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/UpgradeResourceProviderTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/UpgradeResourceProviderTest.java
@@ -95,6 +95,8 @@ import org.apache.ambari.server.state.Service;
 import org.apache.ambari.server.state.ServiceComponent;
 import org.apache.ambari.server.state.ServiceComponentHost;
 import org.apache.ambari.server.state.StackId;
+import org.apache.ambari.server.state.UpgradeContext;
+import org.apache.ambari.server.state.UpgradeContextFactory;
 import org.apache.ambari.server.state.UpgradeHelper;
 import org.apache.ambari.server.state.UpgradeState;
 import org.apache.ambari.server.state.stack.UpgradePack;
@@ -140,6 +142,11 @@ public class UpgradeResourceProviderTest {
   private TopologyManager topologyManager;
   private ConfigFactory configFactory;
   private HostRoleCommandDAO hrcDAO;
+  private UpgradeContextFactory upgradeContextFactory;
+
+  RepositoryVersionEntity repoVersionEntity2110;
+  RepositoryVersionEntity repoVersionEntity2111;
+  RepositoryVersionEntity repoVersionEntity2200;
 
   @Before
   public void before() throws Exception {
@@ -176,6 +183,7 @@ public class UpgradeResourceProviderTest {
     amc = injector.getInstance(AmbariManagementController.class);
     ambariMetaInfo = injector.getInstance(AmbariMetaInfo.class);
     configFactory = injector.getInstance(ConfigFactory.class);
+    upgradeContextFactory = injector.getInstance(UpgradeContextFactory.class);
 
     Field field = AmbariServer.class.getDeclaredField("clusterController");
     field.setAccessible(true);
@@ -199,26 +207,26 @@ public class UpgradeResourceProviderTest {
     StackId stack211 = new StackId("HDP-2.1.1");
     StackId stack220 = new StackId("HDP-2.2.0");
 
-    RepositoryVersionEntity repoVersionEntity = new RepositoryVersionEntity();
-    repoVersionEntity.setDisplayName("My New Version 1");
-    repoVersionEntity.setOperatingSystems("");
-    repoVersionEntity.setStack(stackEntity211);
-    repoVersionEntity.setVersion("2.1.1.0");
-    repoVersionDao.create(repoVersionEntity);
-
-    repoVersionEntity = new RepositoryVersionEntity();
-    repoVersionEntity.setDisplayName("My New Version 2 for patch upgrade");
-    repoVersionEntity.setOperatingSystems("");
-    repoVersionEntity.setStack(stackEntity211);
-    repoVersionEntity.setVersion("2.1.1.1");
-    repoVersionDao.create(repoVersionEntity);
-
-    repoVersionEntity = new RepositoryVersionEntity();
-    repoVersionEntity.setDisplayName("My New Version 3 for major upgrade");
-    repoVersionEntity.setOperatingSystems("");
-    repoVersionEntity.setStack(stackEntity220);
-    repoVersionEntity.setVersion("2.2.0.0");
-    repoVersionDao.create(repoVersionEntity);
+    repoVersionEntity2110 = new RepositoryVersionEntity();
+    repoVersionEntity2110.setDisplayName("My New Version 1");
+    repoVersionEntity2110.setOperatingSystems("");
+    repoVersionEntity2110.setStack(stackEntity211);
+    repoVersionEntity2110.setVersion("2.1.1.0");
+    repoVersionDao.create(repoVersionEntity2110);
+
+    repoVersionEntity2111 = new RepositoryVersionEntity();
+    repoVersionEntity2111.setDisplayName("My New Version 2 for patch upgrade");
+    repoVersionEntity2111.setOperatingSystems("");
+    repoVersionEntity2111.setStack(stackEntity211);
+    repoVersionEntity2111.setVersion("2.1.1.1");
+    repoVersionDao.create(repoVersionEntity2111);
+
+    repoVersionEntity2200 = new RepositoryVersionEntity();
+    repoVersionEntity2200.setDisplayName("My New Version 3 for major upgrade");
+    repoVersionEntity2200.setOperatingSystems("");
+    repoVersionEntity2200.setStack(stackEntity220);
+    repoVersionEntity2200.setVersion("2.2.0.0");
+    repoVersionDao.create(repoVersionEntity2200);
 
     clusters = injector.getInstance(Clusters.class);
 
@@ -242,8 +250,7 @@ public class UpgradeResourceProviderTest {
     clusters.mapHostToCluster("h1", "c1");
 
     // add a single ZK server
-    Service service = cluster.addService("ZOOKEEPER");
-    service.setDesiredStackVersion(cluster.getDesiredStackVersion());
+    Service service = cluster.addService("ZOOKEEPER", repoVersionEntity2110);
 
     ServiceComponent component = service.addServiceComponent("ZOOKEEPER_SERVER");
     ServiceComponentHost sch = component.addServiceComponentHost("h1");
@@ -745,8 +752,7 @@ public class UpgradeResourceProviderTest {
     Cluster cluster = clusters.getCluster("c1");
 
     // add additional service for the test
-    Service service = cluster.addService("HIVE");
-    service.setDesiredStackVersion(cluster.getDesiredStackVersion());
+    Service service = cluster.addService("HIVE", repoVersionEntity2110);
 
     ServiceComponent component = service.addServiceComponent("HIVE_SERVER");
     ServiceComponentHost sch = component.addServiceComponentHost("h1");
@@ -796,7 +802,7 @@ public class UpgradeResourceProviderTest {
 
     // create downgrade with one upgraded service
     StackId stackId = new StackId("HDP", "2.2.0");
-    cluster.setDesiredStackVersion(stackId, true);
+    service.setDesiredRepositoryVersion(repoVersionEntity2200);
 
     requestProps.put(UpgradeResourceProvider.UPGRADE_CLUSTER_NAME, "c1");
     requestProps.put(UpgradeResourceProvider.UPGRADE_VERSION, "2.1.1.0");
@@ -1057,7 +1063,7 @@ public class UpgradeResourceProviderTest {
         assertEquals(oldStack, sc.getDesiredStackVersion());
 
         for (ServiceComponentHost sch : sc.getServiceComponentHosts().values()) {
-          assertEquals(oldStack, sch.getDesiredStackVersion());
+          assertEquals(oldStack.getStackVersion(), sch.getVersion());
         }
       }
     }
@@ -1103,7 +1109,7 @@ public class UpgradeResourceProviderTest {
         assertEquals(newStack, sc.getDesiredStackVersion());
 
         for (ServiceComponentHost sch : sc.getServiceComponentHosts().values()) {
-          assertEquals(newStack, sch.getDesiredStackVersion());
+          assertEquals(newStack.getStackVersion(), sch.getVersion());
         }
       }
     }
@@ -1200,7 +1206,11 @@ public class UpgradeResourceProviderTest {
 
     Map<String, UpgradePack> upgradePacks = ambariMetaInfo.getUpgradePacks("HDP", "2.1.1");
     UpgradePack upgrade = upgradePacks.get("upgrade_to_new_stack");
-    upgradeResourceProvider.applyStackAndProcessConfigurations(stack211.getStackName(), cluster, "2.2.0.0", Direction.UPGRADE, upgrade, "admin");
+
+    UpgradeContext upgradeContext = upgradeContextFactory.create(cluster, upgrade.getType(),
+        Direction.UPGRADE, "2.2.0.0", new HashMap<String, Object>());
+
+    upgradeResourceProvider.applyStackAndProcessConfigurations(upgradeContext);
 
     Map<String, Map<String, String>> expectedConfigurations = expectedConfigurationsCapture.getValue();
     Map<String, String> expectedFooType = expectedConfigurations.get("foo-site");
@@ -1511,8 +1521,7 @@ public class UpgradeResourceProviderTest {
   @Test
   public void testCreateUpgradeDowngradeCycleAdvertisingVersion() throws Exception {
     Cluster cluster = clusters.getCluster("c1");
-    Service service = cluster.addService("STORM");
-    service.setDesiredStackVersion(cluster.getDesiredStackVersion());
+    Service service = cluster.addService("STORM", repoVersionEntity2110);
 
     ServiceComponent component = service.addServiceComponent("DRPC_SERVER");
     ServiceComponentHost sch = component.addServiceComponentHost("h1");

http://git-wip-us.apache.org/repos/asf/ambari/blob/dcbd826c/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/UpgradeSummaryResourceProviderTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/UpgradeSummaryResourceProviderTest.java b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/UpgradeSummaryResourceProviderTest.java
index bee8983..094706e 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/UpgradeSummaryResourceProviderTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/UpgradeSummaryResourceProviderTest.java
@@ -178,8 +178,7 @@ public class UpgradeSummaryResourceProviderTest {
     clusters.mapHostToCluster("h1", "c1");
 
     // add a single ZOOKEEPER server
-    Service service = cluster.addService("ZOOKEEPER");
-    service.setDesiredStackVersion(cluster.getDesiredStackVersion());
+    Service service = cluster.addService("ZOOKEEPER", repoVersionEntity);
 
     ServiceComponent component = service.addServiceComponent("ZOOKEEPER_SERVER");
     ServiceComponentHost sch = component.addServiceComponentHost("h1");

http://git-wip-us.apache.org/repos/asf/ambari/blob/dcbd826c/ambari-server/src/test/java/org/apache/ambari/server/controller/utilities/state/GeneralServiceCalculatedStateTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/utilities/state/GeneralServiceCalculatedStateTest.java b/ambari-server/src/test/java/org/apache/ambari/server/controller/utilities/state/GeneralServiceCalculatedStateTest.java
index 09d5569..eb41ba6 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/controller/utilities/state/GeneralServiceCalculatedStateTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/utilities/state/GeneralServiceCalculatedStateTest.java
@@ -26,6 +26,8 @@ import org.apache.ambari.server.H2DatabaseCleaner;
 import org.apache.ambari.server.actionmanager.ActionManager;
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
 import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
+import org.apache.ambari.server.orm.OrmTestHelper;
+import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
 import org.apache.ambari.server.state.Cluster;
 import org.apache.ambari.server.state.Clusters;
 import org.apache.ambari.server.state.Service;
@@ -59,10 +61,13 @@ public abstract class GeneralServiceCalculatedStateTest {
   @Inject
   protected Clusters clusters;
 
+  @Inject
+  private OrmTestHelper ormTestHelper;
 
   @Before
   public void setup() throws Exception {
     final StackId stack211 = new StackId("HDP-2.1.1");
+    final String version = "2.1.1-1234";
 
     injector = Guice.createInjector(Modules.override(
       new InMemoryDefaultTestModule()).with(new Module() {
@@ -75,11 +80,13 @@ public abstract class GeneralServiceCalculatedStateTest {
     injector.getInstance(GuiceJpaInitializer.class);
     injector.injectMembers(this);
 
+    RepositoryVersionEntity repositoryVersion = ormTestHelper.getOrCreateRepositoryVersion(stack211,
+        version);
+
     clusters.addCluster(clusterName, stack211);
     cluster = clusters.getCluster(clusterName);
 
-    service = cluster.addService(getServiceName());
-    service.setDesiredStackVersion(cluster.getDesiredStackVersion());
+    service = cluster.addService(getServiceName(), repositoryVersion);
 
     createComponentsAndHosts();
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/dcbd826c/ambari-server/src/test/java/org/apache/ambari/server/events/EventsTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/events/EventsTest.java b/ambari-server/src/test/java/org/apache/ambari/server/events/EventsTest.java
index 6fb0028..9907153 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/events/EventsTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/events/EventsTest.java
@@ -33,6 +33,7 @@ import org.apache.ambari.server.orm.dao.AlertDefinitionDAO;
 import org.apache.ambari.server.orm.dao.AlertDispatchDAO;
 import org.apache.ambari.server.orm.entities.AlertDefinitionEntity;
 import org.apache.ambari.server.orm.entities.AlertGroupEntity;
+import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
 import org.apache.ambari.server.state.Cluster;
 import org.apache.ambari.server.state.Clusters;
 import org.apache.ambari.server.state.Host;
@@ -78,6 +79,10 @@ public class EventsTest {
   private AlertDefinitionDAO m_definitionDao;
   private AlertDispatchDAO m_alertDispatchDao;
 
+  private final String STACK_VERSION = "2.0.6";
+  private final String REPO_VERSION = "2.0.6-1234";
+  private RepositoryVersionEntity m_repositoryVersion;
+
   /**
    *
    */
@@ -101,7 +106,7 @@ public class EventsTest {
     m_alertDispatchDao = m_injector.getInstance(AlertDispatchDAO.class);
 
     m_clusterName = "foo";
-    StackId stackId = new StackId("HDP", "2.0.6");
+    StackId stackId = new StackId("HDP", STACK_VERSION);
 
     m_clusters.addCluster(m_clusterName, stackId);
     m_clusters.addHost(HOSTNAME);
@@ -117,8 +122,8 @@ public class EventsTest {
     Assert.assertNotNull(m_cluster);
 
     m_cluster.setDesiredStackVersion(stackId);
-    m_helper.getOrCreateRepositoryVersion(stackId, stackId.getStackVersion());
-    m_cluster.createClusterVersion(stackId, stackId.getStackVersion(), "admin",
+    m_repositoryVersion = m_helper.getOrCreateRepositoryVersion(stackId, REPO_VERSION);
+    m_cluster.createClusterVersion(stackId, REPO_VERSION, "admin",
         RepositoryVersionState.INSTALLING);
 
     m_clusters.mapHostToCluster(HOSTNAME, m_clusterName);
@@ -282,8 +287,6 @@ public class EventsTest {
     // make sure there are at least 1
     Assert.assertTrue(hdfsDefinitions.size() > 0);
 
-    AlertDefinitionEntity definition = hdfsDefinitions.get(0);
-
     // delete the default alert group
     m_alertDispatchDao.remove(group);
 
@@ -378,7 +381,7 @@ public class EventsTest {
 
   private void installHdfsService() throws Exception {
     String serviceName = "HDFS";
-    Service service = m_serviceFactory.createNew(m_cluster, serviceName);
+    Service service = m_serviceFactory.createNew(m_cluster, serviceName, m_repositoryVersion);
     service = m_cluster.getService(serviceName);
     Assert.assertNotNull(service);
 
@@ -391,7 +394,5 @@ public class EventsTest {
     component.addServiceComponentHost(sch);
     sch.setDesiredState(State.INSTALLED);
     sch.setState(State.INSTALLED);
-    sch.setDesiredStackVersion(new StackId("HDP-2.0.6"));
-    sch.setStackVersion(new StackId("HDP-2.0.6"));
   }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/dcbd826c/ambari-server/src/test/java/org/apache/ambari/server/events/listeners/upgrade/HostVersionOutOfSyncListenerTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/events/listeners/upgrade/HostVersionOutOfSyncListenerTest.java b/ambari-server/src/test/java/org/apache/ambari/server/events/listeners/upgrade/HostVersionOutOfSyncListenerTest.java
index fef9276..6184d6d 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/events/listeners/upgrade/HostVersionOutOfSyncListenerTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/events/listeners/upgrade/HostVersionOutOfSyncListenerTest.java
@@ -123,7 +123,8 @@ public class HostVersionOutOfSyncListenerTest {
    * @param stackId Stack Id to use
    * @throws AmbariException
    */
-  private void createClusterAndHosts(String INSTALLED_VERSION, StackId stackId) throws AmbariException {
+  private RepositoryVersionEntity createClusterAndHosts(String INSTALLED_VERSION, StackId stackId)
+      throws AmbariException {
     Host h1 = clusters.getHost("h1");
     h1.setState(HostState.HEALTHY);
 
@@ -152,7 +153,7 @@ public class HostVersionOutOfSyncListenerTest {
     Map<String, List<Integer>> zkTopology = new HashMap<>();
     List<Integer> zkServerHosts = Arrays.asList(0, 1, 2);
     zkTopology.put("ZOOKEEPER_SERVER", new ArrayList<>(zkServerHosts));
-    addService(c1, hostList, zkTopology, "ZOOKEEPER");
+    addService(c1, hostList, zkTopology, "ZOOKEEPER", repositoryVersionEntity);
 
     // install new version
     helper.createHostVersion("h1", repositoryVersionEntity, RepositoryVersionState.INSTALLED);
@@ -173,6 +174,8 @@ public class HostVersionOutOfSyncListenerTest {
         assertEquals(hostVersionEntity.getState(), RepositoryVersionState.INSTALLED);
       }
     }
+
+    return repositoryVersionEntity;
   }
 
   /***
@@ -182,7 +185,7 @@ public class HostVersionOutOfSyncListenerTest {
    * @param stackId Stack Id to use
    * @throws AmbariException
    */
-  private void addRepoVersion(String INSTALLED_VERSION, StackId stackId) throws AmbariException {
+  private RepositoryVersionEntity addRepoVersion(String INSTALLED_VERSION, StackId stackId) throws AmbariException {
     // Register and install new version
     RepositoryVersionEntity repositoryVersionEntity = helper.getOrCreateRepositoryVersion(stackId,
             INSTALLED_VERSION);
@@ -200,6 +203,8 @@ public class HostVersionOutOfSyncListenerTest {
         assertEquals(hostVersionEntity.getState(), RepositoryVersionState.INSTALLED);
       }
     }
+
+    return repositoryVersionEntity;
   }
 
   /**
@@ -215,7 +220,7 @@ public class HostVersionOutOfSyncListenerTest {
     StackId yaStackId = new StackId(yetAnotherStackId);
 
     // get new hosts installed with the first repo
-    createClusterAndHosts(INSTALLED_VERSION, stackId);
+    RepositoryVersionEntity repositoryVersion = createClusterAndHosts(INSTALLED_VERSION, stackId);
 
     // register the new repo
     addRepoVersion(INSTALLED_VERSION_2, yaStackId);
@@ -234,7 +239,7 @@ public class HostVersionOutOfSyncListenerTest {
     hdfsTopology.put("SECONDARY_NAMENODE", Collections.singletonList(1));
     List<Integer> datanodeHosts = Arrays.asList(0, 1);
     hdfsTopology.put("DATANODE", new ArrayList<>(datanodeHosts));
-    addService(c1, hostList, hdfsTopology, "HDFS");
+    addService(c1, hostList, hdfsTopology, "HDFS", repositoryVersion);
 
     // Check result
     Set<String> changedHosts = new HashSet<>();
@@ -272,7 +277,7 @@ public class HostVersionOutOfSyncListenerTest {
     String INSTALLED_VERSION = "2.2.0-1000";
     StackId stackId = new StackId(this.stackId);
 
-    createClusterAndHosts(INSTALLED_VERSION, stackId);
+    RepositoryVersionEntity repositoryVersion = createClusterAndHosts(INSTALLED_VERSION, stackId);
 
     //Add Ganglia service
     List<String> hostList = new ArrayList<>();
@@ -283,7 +288,7 @@ public class HostVersionOutOfSyncListenerTest {
     hdfsTopology.put("GANGLIA_SERVER", Collections.singletonList(0));
     List<Integer> monitorHosts = Arrays.asList(0, 1);
     hdfsTopology.put("GANGLIA_MONITOR", new ArrayList<>(monitorHosts));
-    addService(c1, hostList, hdfsTopology, "GANGLIA");
+    addService(c1, hostList, hdfsTopology, "GANGLIA", repositoryVersion);
 
     // Check result
     Set<String> changedHosts = new HashSet<>();
@@ -319,7 +324,7 @@ public class HostVersionOutOfSyncListenerTest {
 
     assertRepoVersionState(stackId.getStackId(), INSTALLED_VERSION, RepositoryVersionState.INSTALLED);
     assertRepoVersionState(stackId.getStackId(), INSTALLED_VERSION_2, RepositoryVersionState.INSTALLED);
-    
+
     //Add ZOOKEEPER_CLIENT component
     List<String> hostList = new ArrayList<>();
     hostList.add("h1");
@@ -334,7 +339,7 @@ public class HostVersionOutOfSyncListenerTest {
     changedHosts.add("h3");
 
     assertRepoVersionState(stackId.getStackId(), INSTALLED_VERSION,RepositoryVersionState.OUT_OF_SYNC);
-    
+
     List<HostVersionEntity> hostVersions = hostVersionDAO.findAll();
 
     for (HostVersionEntity hostVersionEntity : hostVersions) {
@@ -481,13 +486,13 @@ public class HostVersionOutOfSyncListenerTest {
         .put("NAMENODE", Lists.newArrayList(0))
         .put("DATANODE", Lists.newArrayList(1))
         .build();
-    addService(c1, allHosts, topology, "HDFS");
+    addService(c1, allHosts, topology, "HDFS", repo);
 
     topology = new ImmutableMap.Builder<String, List<Integer>>()
         .put("GANGLIA_SERVER", Lists.newArrayList(0))
         .put("GANGLIA_MONITOR", Lists.newArrayList(2))
         .build();
-    addService(c1, allHosts, topology, "GANGLIA");
+    addService(c1, allHosts, topology, "GANGLIA", repo);
 
     List<HostVersionEntity> hostVersions = hostVersionDAO.findAll();
     assertEquals(3, hostVersions.size());
@@ -552,12 +557,11 @@ public class HostVersionOutOfSyncListenerTest {
     host1.setHostAttributes(hostAttributes);
   }
 
-  private void addService(Cluster cl, List<String> hostList,
-                                Map<String, List<Integer>> topology, String serviceName
-                          ) throws AmbariException {
+  private void addService(Cluster cl, List<String> hostList, Map<String, List<Integer>> topology,
+      String serviceName, RepositoryVersionEntity repositoryVersionEntity) throws AmbariException {
     StackId stackIdObj = new StackId(stackId);
     cl.setDesiredStackVersion(stackIdObj);
-    cl.addService(serviceName);
+    cl.addService(serviceName, repositoryVersionEntity);
 
     for (Map.Entry<String, List<Integer>> component : topology.entrySet()) {
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/dcbd826c/ambari-server/src/test/java/org/apache/ambari/server/events/listeners/upgrade/StackVersionListenerTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/events/listeners/upgrade/StackVersionListenerTest.java b/ambari-server/src/test/java/org/apache/ambari/server/events/listeners/upgrade/StackVersionListenerTest.java
index 7b8b68a..d5b2d46 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/events/listeners/upgrade/StackVersionListenerTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/events/listeners/upgrade/StackVersionListenerTest.java
@@ -167,8 +167,6 @@ public class StackVersionListenerTest extends EasyMockSupport {
   public void testRecalculateHostVersionStateWhenComponentDesiredVersionIsUnknownAndNewVersionIsNotValid() throws AmbariException {
     expect(serviceComponent.getDesiredVersion()).andReturn(UNKNOWN_VERSION);
     expect(serviceComponent.isVersionAdvertised()).andReturn(Boolean.TRUE);
-    serviceComponent.setDesiredVersion(INVALID_NEW_VERSION);
-    expectLastCall().once();
     sch.setUpgradeState(UpgradeState.NONE);
     expectLastCall().once();
     sch.setVersion(INVALID_NEW_VERSION);
@@ -184,8 +182,6 @@ public class StackVersionListenerTest extends EasyMockSupport {
   public void testRecalculateClusterVersionStateWhenComponentDesiredVersionIsUnknownAndNewVersionIsValid() throws AmbariException {
     expect(serviceComponent.getDesiredVersion()).andReturn(UNKNOWN_VERSION);
     expect(serviceComponent.isVersionAdvertised()).andReturn(Boolean.TRUE);
-    serviceComponent.setDesiredVersion(VALID_NEW_VERSION);
-    expectLastCall().once();
     sch.setUpgradeState(UpgradeState.NONE);
     expectLastCall().once();
     sch.setVersion(VALID_NEW_VERSION);

http://git-wip-us.apache.org/repos/asf/ambari/blob/dcbd826c/ambari-server/src/test/java/org/apache/ambari/server/orm/OrmTestHelper.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/orm/OrmTestHelper.java b/ambari-server/src/test/java/org/apache/ambari/server/orm/OrmTestHelper.java
index 574ffa4..e84e0f6 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/orm/OrmTestHelper.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/orm/OrmTestHelper.java
@@ -435,8 +435,11 @@ public class OrmTestHelper {
   public void installHdfsService(Cluster cluster,
       ServiceFactory serviceFactory, ServiceComponentFactory componentFactory,
       ServiceComponentHostFactory schFactory, String hostName) throws Exception {
+
+    RepositoryVersionEntity repositoryVersion = cluster.getCurrentClusterVersion().getRepositoryVersion();
+
     String serviceName = "HDFS";
-    Service service = serviceFactory.createNew(cluster, serviceName);
+    Service service = serviceFactory.createNew(cluster, serviceName, repositoryVersion);
     service = cluster.getService(serviceName);
     assertNotNull(service);
 
@@ -450,8 +453,6 @@ public class OrmTestHelper {
     datanode.addServiceComponentHost(sch);
     sch.setDesiredState(State.INSTALLED);
     sch.setState(State.INSTALLED);
-    sch.setDesiredStackVersion(new StackId("HDP-2.0.6"));
-    sch.setStackVersion(new StackId("HDP-2.0.6"));
 
     ServiceComponent namenode = componentFactory.createNew(service, "NAMENODE");
 
@@ -462,15 +463,16 @@ public class OrmTestHelper {
     namenode.addServiceComponentHost(sch);
     sch.setDesiredState(State.INSTALLED);
     sch.setState(State.INSTALLED);
-    sch.setDesiredStackVersion(new StackId("HDP-2.0.6"));
-    sch.setStackVersion(new StackId("HDP-2.0.6"));
   }
 
   public void installYarnService(Cluster cluster,
       ServiceFactory serviceFactory, ServiceComponentFactory componentFactory,
       ServiceComponentHostFactory schFactory, String hostName) throws Exception {
+
+    RepositoryVersionEntity repositoryVersion = cluster.getCurrentClusterVersion().getRepositoryVersion();
+
     String serviceName = "YARN";
-    Service service = serviceFactory.createNew(cluster, serviceName);
+    Service service = serviceFactory.createNew(cluster, serviceName, repositoryVersion);
     service = cluster.getService(serviceName);
     assertNotNull(service);
 
@@ -485,8 +487,6 @@ public class OrmTestHelper {
     resourceManager.addServiceComponentHost(sch);
     sch.setDesiredState(State.INSTALLED);
     sch.setState(State.INSTALLED);
-    sch.setDesiredStackVersion(new StackId("HDP-2.0.6"));
-    sch.setStackVersion(new StackId("HDP-2.0.6"));
   }
 
   /**

http://git-wip-us.apache.org/repos/asf/ambari/blob/dcbd826c/ambari-server/src/test/java/org/apache/ambari/server/serveraction/upgrades/ComponentVersionCheckActionTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/serveraction/upgrades/ComponentVersionCheckActionTest.java b/ambari-server/src/test/java/org/apache/ambari/server/serveraction/upgrades/ComponentVersionCheckActionTest.java
index 73ab5e6..91d5f04 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/serveraction/upgrades/ComponentVersionCheckActionTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/serveraction/upgrades/ComponentVersionCheckActionTest.java
@@ -21,7 +21,6 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
-import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -46,10 +45,15 @@ import org.apache.ambari.server.orm.dao.ClusterVersionDAO;
 import org.apache.ambari.server.orm.dao.HostDAO;
 import org.apache.ambari.server.orm.dao.HostVersionDAO;
 import org.apache.ambari.server.orm.dao.RepositoryVersionDAO;
+import org.apache.ambari.server.orm.dao.RequestDAO;
 import org.apache.ambari.server.orm.dao.StackDAO;
+import org.apache.ambari.server.orm.dao.UpgradeDAO;
 import org.apache.ambari.server.orm.entities.ClusterVersionEntity;
 import org.apache.ambari.server.orm.entities.HostVersionEntity;
+import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
+import org.apache.ambari.server.orm.entities.RequestEntity;
 import org.apache.ambari.server.orm.entities.StackEntity;
+import org.apache.ambari.server.orm.entities.UpgradeEntity;
 import org.apache.ambari.server.state.Cluster;
 import org.apache.ambari.server.state.Clusters;
 import org.apache.ambari.server.state.ConfigFactory;
@@ -64,6 +68,7 @@ import org.apache.ambari.server.state.ServiceComponentHostFactory;
 import org.apache.ambari.server.state.ServiceFactory;
 import org.apache.ambari.server.state.StackId;
 import org.apache.ambari.server.state.State;
+import org.apache.ambari.server.state.stack.upgrade.UpgradeType;
 import org.apache.ambari.server.utils.EventBusSynchronizer;
 import org.junit.After;
 import org.junit.Assert;
@@ -146,6 +151,9 @@ public class ComponentVersionCheckActionTest {
     clusters.addCluster(clusterName, sourceStack);
 
     StackDAO stackDAO = m_injector.getInstance(StackDAO.class);
+    RequestDAO requestDAO = m_injector.getInstance(RequestDAO.class);
+    UpgradeDAO upgradeDAO = m_injector.getInstance(UpgradeDAO.class);
+
     StackEntity stackEntitySource = stackDAO.find(sourceStack.getStackName(), sourceStack.getStackVersion());
     StackEntity stackEntityTarget = stackDAO.find(targetStack.getStackName(), targetStack.getStackVersion());
     assertNotNull(stackEntitySource);
@@ -180,9 +188,6 @@ public class ComponentVersionCheckActionTest {
     c.transitionClusterVersion(targetStack, targetRepo, RepositoryVersionState.INSTALLED);
     c.setCurrentStackVersion(targetStack);
 
-    c.mapHostVersions(Collections.singleton(hostName), c.getCurrentClusterVersion(),
-        RepositoryVersionState.CURRENT);
-
     HostDAO hostDAO = m_injector.getInstance(HostDAO.class);
 
     HostVersionEntity entity = new HostVersionEntity();
@@ -190,6 +195,25 @@ public class ComponentVersionCheckActionTest {
     entity.setRepositoryVersion(repoVersionDAO.findByStackAndVersion(targetStack, targetRepo));
     entity.setState(RepositoryVersionState.INSTALLED);
     hostVersionDAO.create(entity);
+
+    RequestEntity requestEntity = new RequestEntity();
+    requestEntity.setClusterId(c.getClusterId());
+    requestEntity.setRequestId(1L);
+    requestEntity.setStartTime(System.currentTimeMillis());
+    requestEntity.setCreateTime(System.currentTimeMillis());
+    requestDAO.create(requestEntity);
+
+    UpgradeEntity upgradeEntity = new UpgradeEntity();
+    upgradeEntity.setId(1L);
+    upgradeEntity.setClusterId(c.getClusterId());
+    upgradeEntity.setRequestEntity(requestEntity);
+    upgradeEntity.setUpgradePackage("");
+    upgradeEntity.setFromVersion(sourceRepo);
+    upgradeEntity.setToVersion(targetRepo);
+    upgradeEntity.setUpgradeType(UpgradeType.NON_ROLLING);
+    upgradeDAO.create(upgradeEntity);
+
+    c.setUpgradeEntity(upgradeEntity);
   }
 
   private void makeCrossStackUpgradeCluster(StackId sourceStack, String sourceRepo, StackId targetStack,
@@ -199,6 +223,9 @@ public class ComponentVersionCheckActionTest {
     clusters.addCluster(clusterName, sourceStack);
 
     StackDAO stackDAO = m_injector.getInstance(StackDAO.class);
+    RequestDAO requestDAO = m_injector.getInstance(RequestDAO.class);
+    UpgradeDAO upgradeDAO = m_injector.getInstance(UpgradeDAO.class);
+
     StackEntity stackEntitySource = stackDAO.find(sourceStack.getStackName(), sourceStack.getStackVersion());
     StackEntity stackEntityTarget = stackDAO.find(targetStack.getStackName(), targetStack.getStackVersion());
 
@@ -225,6 +252,24 @@ public class ComponentVersionCheckActionTest {
     c.createClusterVersion(sourceStack, sourceRepo, "admin", RepositoryVersionState.INSTALLING);
     c.transitionClusterVersion(sourceStack, sourceRepo, RepositoryVersionState.CURRENT);
 
+    RequestEntity requestEntity = new RequestEntity();
+    requestEntity.setClusterId(c.getClusterId());
+    requestEntity.setRequestId(1L);
+    requestEntity.setStartTime(System.currentTimeMillis());
+    requestEntity.setCreateTime(System.currentTimeMillis());
+    requestDAO.create(requestEntity);
+
+    UpgradeEntity upgradeEntity = new UpgradeEntity();
+    upgradeEntity.setId(1L);
+    upgradeEntity.setClusterId(c.getClusterId());
+    upgradeEntity.setRequestEntity(requestEntity);
+    upgradeEntity.setUpgradePackage("");
+    upgradeEntity.setFromVersion(sourceRepo);
+    upgradeEntity.setToVersion(targetRepo);
+    upgradeEntity.setUpgradeType(UpgradeType.NON_ROLLING);
+    upgradeDAO.create(upgradeEntity);
+
+    c.setUpgradeEntity(upgradeEntity);
   }
 
   private void createNewRepoVersion(StackId targetStack, String targetRepo, String clusterName,
@@ -245,9 +290,6 @@ public class ComponentVersionCheckActionTest {
     c.createClusterVersion(targetStack, targetRepo, "admin", RepositoryVersionState.INSTALLING);
     c.transitionClusterVersion(targetStack, targetRepo, RepositoryVersionState.INSTALLED);
 
-    c.mapHostVersions(Collections.singleton(hostName), c.getCurrentClusterVersion(),
-        RepositoryVersionState.CURRENT);
-
     HostDAO hostDAO = m_injector.getInstance(HostDAO.class);
 
     HostVersionEntity entity = new HostVersionEntity();
@@ -389,12 +431,16 @@ public class ComponentVersionCheckActionTest {
     Cluster cluster = clusters.getCluster("c1");
     clusters.mapHostToCluster("h1", "c1");
 
+    RepositoryVersionEntity repositoryVersion2110 = m_helper.getOrCreateRepositoryVersion(
+        HDP_21_STACK, HDP_2_1_1_0);
+
+    RepositoryVersionEntity repositoryVersion2111 = m_helper.getOrCreateRepositoryVersion(
+        HDP_21_STACK, HDP_2_1_1_1);
+
     Service service = installService(cluster, "HDFS");
+    service.setDesiredRepositoryVersion(repositoryVersion2110);
     ServiceComponent sc = addServiceComponent(cluster, service, "NAMENODE");
-    sc.setDesiredVersion(HDP_2_1_1_0);
-
     sc = addServiceComponent(cluster, service, "DATANODE");
-    sc.setDesiredVersion(HDP_2_1_1_0);
 
     ServiceComponentHost sch = createNewServiceComponentHost(cluster, "HDFS", "NAMENODE", "h1");
     sch.setVersion(HDP_2_1_1_0);
@@ -402,8 +448,8 @@ public class ComponentVersionCheckActionTest {
     sch.setVersion(HDP_2_1_1_0);
 
     service = installService(cluster, "ZOOKEEPER");
+    service.setDesiredRepositoryVersion(repositoryVersion2111);
     sc = addServiceComponent(cluster, service, "ZOOKEEPER_SERVER");
-    sc.setDesiredVersion(HDP_2_1_1_1);
 
     sch = createNewServiceComponentHost(cluster, "ZOOKEEPER", "ZOOKEEPER_SERVER", "h1");
     sch.setVersion(HDP_2_1_1_1);
@@ -415,7 +461,7 @@ public class ComponentVersionCheckActionTest {
     assertEquals(HDP_211_CENTOS6_REPO_URL, repo.getBaseUrl());
 
     // Finalize the upgrade
-    Map<String, String> commandParams = new HashMap<String, String>();
+    Map<String, String> commandParams = new HashMap<>();
     commandParams.put(FinalizeUpgradeAction.UPGRADE_DIRECTION_KEY, "upgrade");
     commandParams.put(FinalizeUpgradeAction.VERSION_KEY, targetRepo);
     commandParams.put(FinalizeUpgradeAction.SUPPORTED_SERVICES_KEY, "ZOOKEEPER");
@@ -450,8 +496,6 @@ public class ComponentVersionCheckActionTest {
     sc.addServiceComponentHost(sch);
     sch.setDesiredState(State.INSTALLED);
     sch.setState(State.INSTALLED);
-    sch.setDesiredStackVersion(cluster.getDesiredStackVersion());
-    sch.setStackVersion(cluster.getCurrentStackVersion());
 
     return sch;
   }
@@ -462,7 +506,8 @@ public class ComponentVersionCheckActionTest {
     try {
       service = cluster.getService(serviceName);
     } catch (ServiceNotFoundException e) {
-      service = serviceFactory.createNew(cluster, serviceName);
+      RepositoryVersionEntity repositoryVersion = cluster.getCurrentClusterVersion().getRepositoryVersion();
+      service = serviceFactory.createNew(cluster, serviceName, repositoryVersion);
       cluster.addService(service);
     }
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/dcbd826c/ambari-server/src/test/java/org/apache/ambari/server/serveraction/upgrades/ConfigureActionTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/serveraction/upgrades/ConfigureActionTest.java b/ambari-server/src/test/java/org/apache/ambari/server/serveraction/upgrades/ConfigureActionTest.java
index 976dea4..860369b 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/serveraction/upgrades/ConfigureActionTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/serveraction/upgrades/ConfigureActionTest.java
@@ -46,6 +46,7 @@ import org.apache.ambari.server.orm.dao.HostVersionDAO;
 import org.apache.ambari.server.orm.dao.RepositoryVersionDAO;
 import org.apache.ambari.server.orm.dao.StackDAO;
 import org.apache.ambari.server.orm.entities.HostVersionEntity;
+import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
 import org.apache.ambari.server.orm.entities.StackEntity;
 import org.apache.ambari.server.serveraction.ServerAction;
 import org.apache.ambari.server.state.Cluster;
@@ -1695,7 +1696,6 @@ public class ConfigureActionTest {
 
     clusters.addCluster(clusterName, HDP_220_STACK);
 
-
     StackEntity stackEntity = stackDAO.find(HDP_220_STACK.getStackName(),
         HDP_220_STACK.getStackVersion());
 
@@ -1704,9 +1704,13 @@ public class ConfigureActionTest {
     Cluster c = clusters.getCluster(clusterName);
     c.setDesiredStackVersion(HDP_220_STACK);
 
+    // Creating starting repo
+    RepositoryVersionEntity repositoryVersionEntity = m_helper.getOrCreateRepositoryVersion(
+        HDP_220_STACK, HDP_2_2_0_0);
+
     // !!! very important, otherwise the loops that walk the list of installed
     // service properties will not run!
-    installService(c, "ZOOKEEPER");
+    installService(c, "ZOOKEEPER", repositoryVersionEntity);
 
     Config config = cf.createNew(c, "zoo.cfg", "version1", new HashMap<String, String>() {
       {
@@ -1726,8 +1730,6 @@ public class ConfigureActionTest {
     hostAttributes.put("os_release_version", "6");
     host.setHostAttributes(hostAttributes);
 
-    // Creating starting repo
-    m_helper.getOrCreateRepositoryVersion(HDP_220_STACK, HDP_2_2_0_0);
     c.createClusterVersion(HDP_220_STACK, HDP_2_2_0_0, "admin", RepositoryVersionState.INSTALLING);
     c.transitionClusterVersion(HDP_220_STACK, HDP_2_2_0_0, RepositoryVersionState.CURRENT);
 
@@ -1741,10 +1743,6 @@ public class ConfigureActionTest {
     c.transitionClusterVersion(HDP_220_STACK, HDP_2_2_0_1, RepositoryVersionState.INSTALLED);
     c.setCurrentStackVersion(HDP_220_STACK);
 
-    c.mapHostVersions(Collections.singleton(hostName), c.getCurrentClusterVersion(),
-        RepositoryVersionState.CURRENT);
-
-
     HostVersionEntity entity = new HostVersionEntity();
     entity.setHostEntity(hostDAO.findByName(hostName));
     entity.setRepositoryVersion(repoVersionDAO.findByStackAndVersion(HDP_220_STACK, HDP_2_2_0_1));
@@ -1764,13 +1762,14 @@ public class ConfigureActionTest {
    * @return
    * @throws AmbariException
    */
-  private Service installService(Cluster cluster, String serviceName) throws AmbariException {
+  private Service installService(Cluster cluster, String serviceName,
+      RepositoryVersionEntity repositoryVersion) throws AmbariException {
     Service service = null;
 
     try {
       service = cluster.getService(serviceName);
     } catch (ServiceNotFoundException e) {
-      service = serviceFactory.createNew(cluster, serviceName);
+      service = serviceFactory.createNew(cluster, serviceName, repositoryVersion);
       cluster.addService(service);
     }
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/dcbd826c/ambari-server/src/test/java/org/apache/ambari/server/serveraction/upgrades/UpgradeActionTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/serveraction/upgrades/UpgradeActionTest.java b/ambari-server/src/test/java/org/apache/ambari/server/serveraction/upgrades/UpgradeActionTest.java
index 5513271..0a583ae 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/serveraction/upgrades/UpgradeActionTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/serveraction/upgrades/UpgradeActionTest.java
@@ -24,7 +24,6 @@ import static org.junit.Assert.assertTrue;
 
 import java.lang.reflect.Field;
 import java.util.Collection;
-import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -51,8 +50,6 @@ import org.apache.ambari.server.orm.GuiceJpaInitializer;
 import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
 import org.apache.ambari.server.orm.OrmTestHelper;
 import org.apache.ambari.server.orm.dao.ClusterVersionDAO;
-import org.apache.ambari.server.orm.dao.HostComponentDesiredStateDAO;
-import org.apache.ambari.server.orm.dao.HostComponentStateDAO;
 import org.apache.ambari.server.orm.dao.HostDAO;
 import org.apache.ambari.server.orm.dao.HostVersionDAO;
 import org.apache.ambari.server.orm.dao.RepositoryVersionDAO;
@@ -118,6 +115,8 @@ public class UpgradeActionTest {
 
   private static final String HDP_211_CENTOS6_REPO_URL = "http://s3.amazonaws.com/dev.hortonworks.com/HDP/centos6/2.x/BUILDS/2.1.1.0-118";
 
+  private RepositoryVersionEntity sourceRepositoryVersion;
+
   private Injector m_injector;
 
   private AmbariManagementController amc;
@@ -148,10 +147,6 @@ public class UpgradeActionTest {
   @Inject
   private ServiceComponentDesiredStateDAO serviceComponentDesiredStateDAO;
   @Inject
-  private HostComponentDesiredStateDAO hostComponentDesiredStateDAO;
-  @Inject
-  private HostComponentStateDAO hostComponentStateDAO;
-  @Inject
   private StackDAO stackDAO;
   @Inject
   private AmbariMetaInfo ambariMetaInfo;
@@ -209,9 +204,6 @@ public class UpgradeActionTest {
     c.createClusterVersion(targetStack, targetRepo, "admin", RepositoryVersionState.INSTALLING);
     c.transitionClusterVersion(targetStack, targetRepo, RepositoryVersionState.INSTALLED);
 
-    c.mapHostVersions(Collections.singleton(hostName), c.getCurrentClusterVersion(),
-            RepositoryVersionState.CURRENT);
-
     HostVersionEntity entity = new HostVersionEntity();
     entity.setHostEntity(hostDAO.findByName(hostName));
     entity.setRepositoryVersion(repoVersionDAO.findByStackAndVersion(targetStack, targetRepo));
@@ -266,10 +258,6 @@ public class UpgradeActionTest {
     entitySource.setState(RepositoryVersionState.INSTALL_FAILED);
     hostVersionDAO.create(entitySource);
 
-    // Create a host version for the mid repo in CURRENT
-    c.mapHostVersions(Collections.singleton(hostName), c.getCurrentClusterVersion(),
-            RepositoryVersionState.CURRENT);
-
     // Create a host version for the target repo in UPGRADED
     HostVersionEntity entityTarget = new HostVersionEntity();
     entityTarget.setHostEntity(hostDAO.findByName(hostName));
@@ -278,7 +266,8 @@ public class UpgradeActionTest {
     hostVersionDAO.create(entityTarget);
   }
 
-  private void createUpgradeClusterAndSourceRepo(StackId sourceStack, String sourceRepo,
+  private RepositoryVersionEntity createUpgradeClusterAndSourceRepo(StackId sourceStack,
+      String sourceRepo,
                                                  String hostName) throws Exception {
 
     clusters.addCluster(clusterName, sourceStack);
@@ -303,8 +292,8 @@ public class UpgradeActionTest {
     clusters.mapHostToCluster(hostName, clusterName);
 
     // Create the starting repo version
-    RepositoryVersionEntity repoEntity = m_helper.getOrCreateRepositoryVersion(sourceStack, sourceRepo);
-    repoEntity.setOperatingSystems("[\n" +
+    sourceRepositoryVersion = m_helper.getOrCreateRepositoryVersion(sourceStack, sourceRepo);
+    sourceRepositoryVersion.setOperatingSystems("[\n" +
             "   {\n" +
             "      \"repositories\":[\n" +
             "         {\n" +
@@ -316,14 +305,14 @@ public class UpgradeActionTest {
             "      \"OperatingSystems/os_type\":\"redhat6\"\n" +
             "   }\n" +
             "]");
-    repoVersionDAO.merge(repoEntity);
+    repoVersionDAO.merge(sourceRepositoryVersion);
 
     c.createClusterVersion(sourceStack, sourceRepo, "admin", RepositoryVersionState.INSTALLING);
     c.transitionClusterVersion(sourceStack, sourceRepo, RepositoryVersionState.CURRENT);
-
+    return sourceRepositoryVersion;
   }
 
-  private void createUpgradeClusterTargetRepo(StackId targetStack, String targetRepo,
+  private RepositoryVersionEntity createUpgradeClusterTargetRepo(StackId targetStack, String targetRepo,
                                               String hostName) throws AmbariException {
     Cluster c = clusters.getCluster(clusterName);
     StackEntity stackEntityTarget = stackDAO.find(targetStack.getStackName(), targetStack.getStackVersion());
@@ -341,9 +330,6 @@ public class UpgradeActionTest {
     c.transitionClusterVersion(targetStack, targetRepo, RepositoryVersionState.INSTALLED);
     c.setCurrentStackVersion(targetStack);
 
-    c.mapHostVersions(Collections.singleton(hostName), c.getCurrentClusterVersion(),
-            RepositoryVersionState.CURRENT);
-
     // create a single host with the UPGRADED HostVersionEntity
     HostDAO hostDAO = m_injector.getInstance(HostDAO.class);
 
@@ -361,6 +347,8 @@ public class UpgradeActionTest {
 
     assertEquals(1, hostVersions.size());
     assertEquals(RepositoryVersionState.INSTALLED, hostVersions.get(0).getState());
+
+    return repositoryVersionEntity;
   }
 
   private void makeCrossStackUpgradeClusterAndSourceRepo(StackId sourceStack, String sourceRepo,
@@ -388,7 +376,7 @@ public class UpgradeActionTest {
     clusters.mapHostToCluster(hostName, clusterName);
 
     // Create the starting repo version
-    m_helper.getOrCreateRepositoryVersion(sourceStack, sourceRepo);
+    sourceRepositoryVersion = m_helper.getOrCreateRepositoryVersion(sourceStack, sourceRepo);
     c.createClusterVersion(sourceStack, sourceRepo, "admin", RepositoryVersionState.INSTALLING);
     c.transitionClusterVersion(sourceStack, sourceRepo, RepositoryVersionState.CURRENT);
   }
@@ -409,9 +397,6 @@ public class UpgradeActionTest {
     c.createClusterVersion(targetStack, targetRepo, "admin", RepositoryVersionState.INSTALLING);
     c.transitionClusterVersion(targetStack, targetRepo, RepositoryVersionState.INSTALLED);
 
-    c.mapHostVersions(Collections.singleton(hostName), c.getCurrentClusterVersion(),
-        RepositoryVersionState.CURRENT);
-
     HostDAO hostDAO = m_injector.getInstance(HostDAO.class);
 
     HostVersionEntity entity = new HostVersionEntity();
@@ -442,10 +427,11 @@ public class UpgradeActionTest {
     Assert.assertTrue(packs.containsKey(upgradePackName));
 
     makeCrossStackUpgradeClusterAndSourceRepo(sourceStack, sourceRepo, hostName);
-//    makeCrossStackUpgradeCluster(sourceStack, sourceRepo, targetStack, targetRepo);
 
     Cluster cluster = clusters.getCluster(clusterName);
 
+    createUpgrade(cluster, sourceStack, sourceRepo, targetRepo);
+
     // Install ZK and HDFS with some components
     Service zk = installService(cluster, "ZOOKEEPER");
     addServiceComponent(cluster, zk, "ZOOKEEPER_SERVER");
@@ -515,6 +501,10 @@ public class UpgradeActionTest {
 
     makeDowngradeCluster(sourceStack, sourceRepo, targetStack, targetRepo);
 
+    Cluster cluster = clusters.getCluster(clusterName);
+
+    createUpgrade(cluster, sourceStack, sourceRepo, targetRepo);
+
     Map<String, String> commandParams = new HashMap<>();
     commandParams.put(FinalizeUpgradeAction.UPGRADE_DIRECTION_KEY, "downgrade");
     commandParams.put(FinalizeUpgradeAction.VERSION_KEY, sourceRepo);
@@ -573,6 +563,10 @@ public class UpgradeActionTest {
 
     makeTwoUpgradesWhereLastDidNotComplete(sourceStack, sourceRepo, midStack, midRepo, targetStack, targetRepo);
 
+    Cluster cluster = clusters.getCluster(clusterName);
+
+    createUpgrade(cluster, sourceStack, sourceRepo, targetRepo);
+
     Map<String, String> commandParams = new HashMap<>();
     commandParams.put(FinalizeUpgradeAction.UPGRADE_DIRECTION_KEY, "downgrade");
     commandParams.put(FinalizeUpgradeAction.VERSION_KEY, midRepo);
@@ -606,10 +600,13 @@ public class UpgradeActionTest {
     createUpgradeClusterAndSourceRepo(sourceStack, sourceRepo, hostName);
     createUpgradeClusterTargetRepo(targetStack, targetRepo, hostName);
 
+    Cluster cluster = clusters.getCluster(clusterName);
+
+    createUpgrade(cluster, sourceStack, sourceRepo, targetRepo);
+
     // Verify the repo before calling Finalize
     AmbariCustomCommandExecutionHelper helper = m_injector.getInstance(AmbariCustomCommandExecutionHelper.class);
     Host host = clusters.getHost("h1");
-    Cluster cluster = clusters.getCluster(clusterName);
 
     RepositoryInfo repo = ambariMetaInfo.getRepository(sourceStack.getStackName(), sourceStack.getStackVersion(), "redhat6", sourceStack.getStackId());
     assertEquals(HDP_211_CENTOS6_REPO_URL, repo.getBaseUrl());
@@ -671,6 +668,8 @@ public class UpgradeActionTest {
     Host host = clusters.getHost("h1");
     Cluster cluster = clusters.getCluster(clusterName);
 
+    createUpgrade(cluster, sourceStack, sourceRepo, targetRepo);
+
     RepositoryInfo repo = ambariMetaInfo.getRepository(sourceStack.getStackName(),
             sourceStack.getStackVersion(), "redhat6", sourceStack.getStackId());
     assertEquals(HDP_211_CENTOS6_REPO_URL, repo.getBaseUrl());
@@ -734,6 +733,8 @@ public class UpgradeActionTest {
     cluster.setCurrentStackVersion(sourceStack);
     cluster.setDesiredStackVersion(targetStack);
 
+    createUpgrade(cluster, sourceStack, sourceRepo, targetRepo);
+
     Map<String, String> commandParams = new HashMap<>();
     commandParams.put(FinalizeUpgradeAction.UPGRADE_DIRECTION_KEY, "upgrade");
     commandParams.put(FinalizeUpgradeAction.VERSION_KEY, targetRepo);
@@ -781,7 +782,6 @@ public class UpgradeActionTest {
     makeCrossStackUpgradeClusterAndSourceRepo(sourceStack, sourceRepo, hostName);
     Cluster cluster = clusters.getCluster(clusterName);
 
-
     // install HDFS with some components
     Service service = installService(cluster, "HDFS");
     addServiceComponent(cluster, service, "NAMENODE");
@@ -791,6 +791,8 @@ public class UpgradeActionTest {
 
     makeCrossStackUpgradeTargetRepo(targetStack, targetRepo, hostName);
 
+    createUpgrade(cluster, sourceStack, sourceRepo, targetRepo);
+
     // create some configs
     createConfigs(cluster);
 
@@ -889,6 +891,8 @@ public class UpgradeActionTest {
     cluster.setCurrentStackVersion(sourceStack);
     cluster.setDesiredStackVersion(targetStack);
 
+    createUpgrade(cluster, sourceStack, sourceRepo, targetRepo);
+
     // set the SCH versions to the new stack so that the finalize action is
     // happy
     cluster.getServiceComponentHosts("HDFS", "NAMENODE").get(0).setVersion(targetRepo);
@@ -966,32 +970,15 @@ public class UpgradeActionTest {
     ServiceComponentHost nnSCH = createNewServiceComponentHost(cluster, "HDFS", "NAMENODE", "h1");
     ServiceComponentHost dnSCH = createNewServiceComponentHost(cluster, "HDFS", "DATANODE", "h1");
 
-    createUpgradeClusterTargetRepo(targetStack, targetRepo, hostName);
+    RepositoryVersionEntity targetRepositoryVersion = createUpgradeClusterTargetRepo(targetStack,
+        targetRepo, hostName);
 
     // fake their upgrade
-    nnSCH.setStackVersion(nnSCH.getDesiredStackVersion());
+    service.setDesiredRepositoryVersion(targetRepositoryVersion);
     nnSCH.setVersion(targetRepo);
-    dnSCH.setStackVersion(nnSCH.getDesiredStackVersion());
     dnSCH.setVersion(targetRepo);
 
-    // create some entities for the finalize action to work with for patch
-    // history
-    RequestEntity requestEntity = new RequestEntity();
-    requestEntity.setClusterId(cluster.getClusterId());
-    requestEntity.setRequestId(1L);
-    requestEntity.setStartTime(System.currentTimeMillis());
-    requestEntity.setCreateTime(System.currentTimeMillis());
-    requestDAO.create(requestEntity);
-
-    UpgradeEntity upgradeEntity = new UpgradeEntity();
-    upgradeEntity.setId(1L);
-    upgradeEntity.setClusterId(cluster.getClusterId());
-    upgradeEntity.setRequestEntity(requestEntity);
-    upgradeEntity.setUpgradePackage("");
-    upgradeEntity.setFromVersion(sourceRepo);
-    upgradeEntity.setToVersion(targetRepo);
-    upgradeEntity.setUpgradeType(UpgradeType.NON_ROLLING);
-    upgradeDAO.create(upgradeEntity);
+    UpgradeEntity upgrade = createUpgrade(cluster, sourceStack, sourceRepo, targetRepo);
 
     // verify that no history exist exists yet
     List<ServiceComponentHistoryEntity> historyEntites = serviceComponentDesiredStateDAO.findHistory(
@@ -1007,7 +994,7 @@ public class UpgradeActionTest {
     // Finalize the upgrade, passing in the request ID so that history is
     // created
     Map<String, String> commandParams = new HashMap<>();
-    commandParams.put(FinalizeUpgradeAction.REQUEST_ID, String.valueOf(requestEntity.getRequestId()));
+    commandParams.put(FinalizeUpgradeAction.REQUEST_ID, String.valueOf(upgrade.getRequestId()));
     commandParams.put(FinalizeUpgradeAction.UPGRADE_DIRECTION_KEY, "upgrade");
     commandParams.put(FinalizeUpgradeAction.VERSION_KEY, targetRepo);
 
@@ -1044,8 +1031,6 @@ public class UpgradeActionTest {
     sc.addServiceComponentHost(sch);
     sch.setDesiredState(State.INSTALLED);
     sch.setState(State.INSTALLED);
-    sch.setDesiredStackVersion(cluster.getDesiredStackVersion());
-    sch.setStackVersion(cluster.getCurrentStackVersion());
     return sch;
   }
 
@@ -1055,7 +1040,7 @@ public class UpgradeActionTest {
     try {
       service = cluster.getService(serviceName);
     } catch (ServiceNotFoundException e) {
-      service = serviceFactory.createNew(cluster, serviceName);
+      service = serviceFactory.createNew(cluster, serviceName, sourceRepositoryVersion);
       cluster.addService(service);
     }
 
@@ -1100,4 +1085,40 @@ public class UpgradeActionTest {
     configFactory.createNew(cluster, "foo-site", "version-" + System.currentTimeMillis(),
         properties, propertiesAttributes);
   }
+
+  /**
+   * Creates an upgrade an associates it with the cluster.
+   *
+   * @param cluster
+   * @param sourceRepo
+   * @param targetRepo
+   * @throws Exception
+   */
+  private UpgradeEntity createUpgrade(Cluster cluster, StackId sourceStack, String sourceRepo,
+      String targetRepo) throws Exception {
+
+    // create some entities for the finalize action to work with for patch
+    // history
+    RequestEntity requestEntity = new RequestEntity();
+    requestEntity.setClusterId(cluster.getClusterId());
+    requestEntity.setRequestId(1L);
+    requestEntity.setStartTime(System.currentTimeMillis());
+    requestEntity.setCreateTime(System.currentTimeMillis());
+    requestDAO.create(requestEntity);
+
+    UpgradeEntity upgradeEntity = new UpgradeEntity();
+    upgradeEntity.setId(1L);
+    upgradeEntity.setClusterId(cluster.getClusterId());
+    upgradeEntity.setRequestEntity(requestEntity);
+    upgradeEntity.setUpgradePackage("");
+    upgradeEntity.setFromVersion(sourceRepo);
+    upgradeEntity.setToVersion(targetRepo);
+    upgradeEntity.setUpgradeType(UpgradeType.NON_ROLLING);
+
+    upgradeDAO.create(upgradeEntity);
+
+    cluster.setUpgradeEntity(upgradeEntity);
+
+    return upgradeEntity;
+  }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/dcbd826c/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceComponentTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceComponentTest.java b/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceComponentTest.java
index 2d589ed..d72f018 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceComponentTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceComponentTest.java
@@ -31,9 +31,7 @@ import java.util.Map;
 import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.H2DatabaseCleaner;
 import org.apache.ambari.server.actionmanager.HostRoleStatus;
-import org.apache.ambari.server.api.services.AmbariMetaInfo;
 import org.apache.ambari.server.controller.ServiceComponentResponse;
-import org.apache.ambari.server.events.listeners.upgrade.StackVersionListener;
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
 import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
 import org.apache.ambari.server.orm.OrmTestHelper;
@@ -43,6 +41,7 @@ import org.apache.ambari.server.orm.dao.HostDAO;
 import org.apache.ambari.server.orm.dao.RepositoryVersionDAO;
 import org.apache.ambari.server.orm.dao.RequestDAO;
 import org.apache.ambari.server.orm.dao.ServiceComponentDesiredStateDAO;
+import org.apache.ambari.server.orm.dao.StackDAO;
 import org.apache.ambari.server.orm.dao.UpgradeDAO;
 import org.apache.ambari.server.orm.entities.HostComponentDesiredStateEntity;
 import org.apache.ambari.server.orm.entities.HostComponentStateEntity;
@@ -52,6 +51,7 @@ import org.apache.ambari.server.orm.entities.RequestEntity;
 import org.apache.ambari.server.orm.entities.ServiceComponentDesiredStateEntity;
 import org.apache.ambari.server.orm.entities.ServiceComponentHistoryEntity;
 import org.apache.ambari.server.orm.entities.ServiceComponentVersionEntity;
+import org.apache.ambari.server.orm.entities.StackEntity;
 import org.apache.ambari.server.orm.entities.StageEntity;
 import org.apache.ambari.server.orm.entities.UpgradeEntity;
 import org.apache.ambari.server.state.stack.upgrade.Direction;
@@ -76,7 +76,6 @@ public class ServiceComponentTest {
   private ServiceFactory serviceFactory;
   private ServiceComponentFactory serviceComponentFactory;
   private ServiceComponentHostFactory serviceComponentHostFactory;
-  private AmbariMetaInfo metaInfo;
   private OrmTestHelper helper;
   private HostDAO hostDAO;
 
@@ -92,7 +91,6 @@ public class ServiceComponentTest {
         ServiceComponentHostFactory.class);
     helper = injector.getInstance(OrmTestHelper.class);
     hostDAO = injector.getInstance(HostDAO.class);
-    metaInfo = injector.getInstance(AmbariMetaInfo.class);
 
     clusterName = "foo";
     serviceName = "HDFS";
@@ -103,11 +101,14 @@ public class ServiceComponentTest {
 
     cluster.setDesiredStackVersion(stackId);
     Assert.assertNotNull(cluster);
-    helper.getOrCreateRepositoryVersion(stackId, stackId.getStackVersion());
+
+    RepositoryVersionEntity repositoryVersion = helper.getOrCreateRepositoryVersion(stackId,
+        stackId.getStackVersion());
+
     cluster.createClusterVersion(stackId, stackId.getStackVersion(), "admin",
         RepositoryVersionState.INSTALLING);
 
-    Service s = serviceFactory.createNew(cluster, serviceName);
+    Service s = serviceFactory.createNew(cluster, serviceName, repositoryVersion);
     cluster.addService(s);
     service = cluster.getService(serviceName);
     Assert.assertNotNull(service);
@@ -154,8 +155,12 @@ public class ServiceComponentTest {
     sc.setDesiredState(State.INSTALLED);
     Assert.assertEquals(State.INSTALLED, sc.getDesiredState());
 
-    sc.setDesiredStackVersion(new StackId("HDP-1.2.0"));
-    Assert.assertEquals("HDP-1.2.0", sc.getDesiredStackVersion().getStackId());
+    StackId newStackId = new StackId("HDP-1.2.0");
+    RepositoryVersionEntity repositoryVersion = helper.getOrCreateRepositoryVersion(newStackId,
+        newStackId.getStackVersion());
+
+    sc.setDesiredRepositoryVersion(repositoryVersion);
+    Assert.assertEquals(newStackId.toString(), sc.getDesiredStackVersion().getStackId());
 
     ServiceComponentDesiredStateDAO serviceComponentDesiredStateDAO =
         injector.getInstance(ServiceComponentDesiredStateDAO.class);
@@ -199,8 +204,7 @@ public class ServiceComponentTest {
   @Test
   public void testAddAndGetServiceComponentHosts() throws AmbariException {
     String componentName = "NAMENODE";
-    ServiceComponent component = serviceComponentFactory.createNew(service,
-        componentName);
+    ServiceComponent component = serviceComponentFactory.createNew(service, componentName);
     service.addServiceComponent(component);
 
     ServiceComponent sc = service.getServiceComponent(componentName);
@@ -224,6 +228,8 @@ public class ServiceComponentTest {
 
     ServiceComponentHost sch1 = sc.addServiceComponentHost("h1");
     ServiceComponentHost sch2 = sc.addServiceComponentHost("h2");
+    assertNotNull(sch1);
+    assertNotNull(sch2);
 
     try {
       sc.addServiceComponentHost("h2");
@@ -241,9 +247,7 @@ public class ServiceComponentTest {
     sc.addServiceComponentHost("h3");
     Assert.assertNotNull(sc.getServiceComponentHost("h3"));
 
-    sch1.setDesiredStackVersion(new StackId("HDP-1.2.0"));
     sch1.setState(State.STARTING);
-    sch1.setStackVersion(new StackId("HDP-1.2.0"));
     sch1.setDesiredState(State.STARTED);
 
     HostComponentDesiredStateDAO desiredStateDAO = injector.getInstance(
@@ -268,17 +272,14 @@ public class ServiceComponentTest {
     Assert.assertNotNull(sch);
     Assert.assertEquals(State.STARTING, sch.getState());
     Assert.assertEquals(State.STARTED, sch.getDesiredState());
-    Assert.assertEquals("HDP-1.2.0",
-        sch.getStackVersion().getStackId());
-    Assert.assertEquals("HDP-1.2.0",
-        sch.getDesiredStackVersion().getStackId());
+    Assert.assertEquals(service.getDesiredRepositoryVersion().getVersion(),
+        sch.getServiceComponent().getDesiredVersion());
   }
 
   @Test
   public void testConvertToResponse() throws AmbariException {
     String componentName = "NAMENODE";
-    ServiceComponent component = serviceComponentFactory.createNew(service,
-        componentName);
+    ServiceComponent component = serviceComponentFactory.createNew(service, componentName);
     service.addServiceComponent(component);
 
     addHostToCluster("h1", service.getCluster().getClusterName());
@@ -295,7 +296,6 @@ public class ServiceComponentTest {
     ServiceComponent sc = service.getServiceComponent(componentName);
     Assert.assertNotNull(sc);
     sc.setDesiredState(State.INSTALLED);
-    sc.setDesiredStackVersion(new StackId("HDP-1.2.0"));
 
     ServiceComponentResponse r = sc.convertToResponse();
     Assert.assertEquals(sc.getClusterName(), r.getClusterName());
@@ -362,7 +362,12 @@ public class ServiceComponentTest {
     sc.setDesiredState(State.INSTALLED);
     Assert.assertEquals(State.INSTALLED, sc.getDesiredState());
 
-    sc.setDesiredStackVersion(new StackId("HDP-2.2.0"));
+    StackId newStackId = new StackId("HDP-2.2.0");
+    RepositoryVersionEntity repositoryVersion = helper.getOrCreateRepositoryVersion(newStackId,
+        newStackId.getStackVersion());
+
+    sc.setDesiredRepositoryVersion(repositoryVersion);
+
     StackId stackId = sc.getDesiredStackVersion();
     Assert.assertEquals(new StackId("HDP", "2.2.0"), stackId);
 
@@ -479,7 +484,12 @@ public class ServiceComponentTest {
     sc.setDesiredState(State.INSTALLED);
     Assert.assertEquals(State.INSTALLED, sc.getDesiredState());
 
-    sc.setDesiredStackVersion(new StackId("HDP-2.2.0"));
+    StackId newStackId = new StackId("HDP-2.2.0");
+    RepositoryVersionEntity repositoryVersion = helper.getOrCreateRepositoryVersion(newStackId,
+        newStackId.getStackVersion());
+
+    sc.setDesiredRepositoryVersion(repositoryVersion);
+
     StackId stackId = sc.getDesiredStackVersion();
     Assert.assertEquals(new StackId("HDP", "2.2.0"), stackId);
 
@@ -545,23 +555,28 @@ public class ServiceComponentTest {
     sc.setDesiredState(State.INSTALLED);
     Assert.assertEquals(State.INSTALLED, sc.getDesiredState());
 
-    sc.setDesiredStackVersion(new StackId("HDP-2.2.0"));
-    StackId stackId = sc.getDesiredStackVersion();
-    Assert.assertEquals(new StackId("HDP", "2.2.0"), stackId);
-
-    Assert.assertEquals("HDP-2.2.0", sc.getDesiredStackVersion().getStackId());
-
     ServiceComponentDesiredStateEntity serviceComponentDesiredStateEntity = serviceComponentDesiredStateDAO.findByName(
         cluster.getClusterId(), serviceName, componentName);
 
-    Assert.assertNotNull(serviceComponentDesiredStateEntity);
+    StackDAO stackDAO = injector.getInstance(StackDAO.class);
+    StackEntity stackEntity = stackDAO.find("HDP", "2.2.0");
 
-    RepositoryVersionEntity rve = new RepositoryVersionEntity(
-        serviceComponentDesiredStateEntity.getDesiredStack(), "HDP-2.2.0", "2.2.0.1-1111", "[]");
+    RepositoryVersionEntity rve = new RepositoryVersionEntity(stackEntity, "HDP-2.2.0",
+        "2.2.0.1-1111", "[]");
 
     RepositoryVersionDAO repositoryDAO = injector.getInstance(RepositoryVersionDAO.class);
     repositoryDAO.create(rve);
 
+    sc.setDesiredRepositoryVersion(rve);
+
+    Assert.assertEquals(rve, sc.getDesiredRepositoryVersion());
+
+    Assert.assertEquals(new StackId("HDP", "2.2.0"), sc.getDesiredStackVersion());
+
+    Assert.assertEquals("HDP-2.2.0", sc.getDesiredStackVersion().getStackId());
+
+    Assert.assertNotNull(serviceComponentDesiredStateEntity);
+
     ServiceComponentVersionEntity version = new ServiceComponentVersionEntity();
     version.setState(RepositoryVersionState.CURRENT);
     version.setRepositoryVersion(rve);
@@ -595,23 +610,27 @@ public class ServiceComponentTest {
     sc.setDesiredState(State.INSTALLED);
     Assert.assertEquals(State.INSTALLED, sc.getDesiredState());
 
-    sc.setDesiredStackVersion(new StackId("HDP-2.2.0"));
-    StackId stackId = sc.getDesiredStackVersion();
-    Assert.assertEquals(new StackId("HDP", "2.2.0"), stackId);
-
-    Assert.assertEquals("HDP-2.2.0", sc.getDesiredStackVersion().getStackId());
-
     ServiceComponentDesiredStateEntity serviceComponentDesiredStateEntity = serviceComponentDesiredStateDAO.findByName(
         cluster.getClusterId(), serviceName, componentName);
 
-    Assert.assertNotNull(serviceComponentDesiredStateEntity);
+    StackDAO stackDAO = injector.getInstance(StackDAO.class);
+    StackEntity stackEntity = stackDAO.find("HDP", "2.2.0");
 
-    RepositoryVersionEntity rve = new RepositoryVersionEntity(
-        serviceComponentDesiredStateEntity.getDesiredStack(), "HDP-2.2.0", "2.2.0.1-1111", "[]");
+    RepositoryVersionEntity rve = new RepositoryVersionEntity(stackEntity, "HDP-2.2.0",
+        "2.2.0.1-1111", "[]");
 
     RepositoryVersionDAO repositoryDAO = injector.getInstance(RepositoryVersionDAO.class);
     repositoryDAO.create(rve);
 
+    sc.setDesiredRepositoryVersion(rve);
+
+    StackId stackId = sc.getDesiredStackVersion();
+    Assert.assertEquals(new StackId("HDP", "2.2.0"), stackId);
+
+    Assert.assertEquals("HDP-2.2.0", sc.getDesiredStackVersion().getStackId());
+
+    Assert.assertNotNull(serviceComponentDesiredStateEntity);
+
     ServiceComponentVersionEntity version = new ServiceComponentVersionEntity();
     version.setState(RepositoryVersionState.CURRENT);
     version.setRepositoryVersion(rve);
@@ -650,7 +669,13 @@ public class ServiceComponentTest {
     String componentName = "NAMENODE";
 
     ServiceComponent component = serviceComponentFactory.createNew(service, componentName);
-    component.setDesiredStackVersion(new StackId("HDP-2.2.0"));
+
+    StackId newStackId = new StackId("HDP-2.2.0");
+    RepositoryVersionEntity repositoryVersion = helper.getOrCreateRepositoryVersion(newStackId,
+        newStackId.getStackVersion());
+
+    component.setDesiredRepositoryVersion(repositoryVersion);
+
     service.addServiceComponent(component);
 
     ServiceComponent sc = service.getServiceComponent(componentName);
@@ -658,8 +683,11 @@ public class ServiceComponentTest {
 
     ServiceComponentDesiredStateEntity entity = serviceComponentDesiredStateDAO.findByName(cluster.getClusterId(), serviceName, componentName);
 
-    helper.getOrCreateRepositoryVersion(component.getDesiredStackVersion(), "2.2.0.1");
-    helper.getOrCreateRepositoryVersion(component.getDesiredStackVersion(), "2.2.0.2");
+    RepositoryVersionEntity repoVersion2201 = helper.getOrCreateRepositoryVersion(
+        component.getDesiredStackVersion(), "2.2.0.1");
+
+    RepositoryVersionEntity repoVersion2202 = helper.getOrCreateRepositoryVersion(
+        component.getDesiredStackVersion(), "2.2.0.2");
 
     addHostToCluster("h1", clusterName);
     addHostToCluster("h2", clusterName);
@@ -671,7 +699,7 @@ public class ServiceComponentTest {
     ServiceComponentHost sch2 = sc.addServiceComponentHost("h2");
 
     // !!! case 1: component desired is UNKNOWN, mix of h-c versions
-    sc.setDesiredVersion(StackVersionListener.UNKNOWN_VERSION);
+    sc.setDesiredRepositoryVersion(repositoryVersion);
     sch1.setVersion("2.2.0.1");
     sch2.setVersion("2.2.0.2");
     sc.updateRepositoryState("2.2.0.2");
@@ -679,15 +707,15 @@ public class ServiceComponentTest {
     assertEquals(RepositoryVersionState.OUT_OF_SYNC, entity.getRepositoryState());
 
     // !!! case 2: component desired is UNKNOWN, all h-c same version
-    sc.setDesiredVersion(StackVersionListener.UNKNOWN_VERSION);
+    sc.setDesiredRepositoryVersion(repositoryVersion);
     sch1.setVersion("2.2.0.1");
     sch2.setVersion("2.2.0.1");
     sc.updateRepositoryState("2.2.0.1");
     entity = serviceComponentDesiredStateDAO.findByName(cluster.getClusterId(), serviceName, componentName);
-    assertEquals(RepositoryVersionState.CURRENT, entity.getRepositoryState());
+    assertEquals(RepositoryVersionState.OUT_OF_SYNC, entity.getRepositoryState());
 
     // !!! case 3: component desired is known, any component reports different version
-    sc.setDesiredVersion("2.2.0.1");
+    sc.setDesiredRepositoryVersion(repoVersion2201);
     sch1.setVersion("2.2.0.1");
     sch2.setVersion("2.2.0.2");
     sc.updateRepositoryState("2.2.0.2");
@@ -695,7 +723,7 @@ public class ServiceComponentTest {
     assertEquals(RepositoryVersionState.OUT_OF_SYNC, entity.getRepositoryState());
 
     // !!! case 4: component desired is known, component reports same as desired, mix of h-c versions
-    sc.setDesiredVersion("2.2.0.1");
+    sc.setDesiredRepositoryVersion(repoVersion2201);
     sch1.setVersion("2.2.0.1");
     sch2.setVersion("2.2.0.2");
     sc.updateRepositoryState("2.2.0.1");
@@ -703,7 +731,7 @@ public class ServiceComponentTest {
     assertEquals(RepositoryVersionState.OUT_OF_SYNC, entity.getRepositoryState());
 
     // !!! case 5: component desired is known, component reports same as desired, all h-c the same
-    sc.setDesiredVersion("2.2.0.1");
+    sc.setDesiredRepositoryVersion(repoVersion2201);
     sch1.setVersion("2.2.0.1");
     sch2.setVersion("2.2.0.1");
     sc.updateRepositoryState("2.2.0.1");

http://git-wip-us.apache.org/repos/asf/ambari/blob/dcbd826c/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceTest.java b/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceTest.java
index fc12e44..dfe8f59 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceTest.java
@@ -27,12 +27,13 @@ import java.util.Map;
 
 import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.H2DatabaseCleaner;
-import org.apache.ambari.server.api.services.AmbariMetaInfo;
 import org.apache.ambari.server.controller.ServiceResponse;
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
 import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
+import org.apache.ambari.server.orm.OrmTestHelper;
 import org.apache.ambari.server.orm.dao.ClusterServiceDAO;
 import org.apache.ambari.server.orm.entities.ClusterServiceEntity;
+import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
@@ -51,7 +52,12 @@ public class ServiceTest {
   private ServiceFactory serviceFactory;
   private ServiceComponentFactory serviceComponentFactory;
   private ServiceComponentHostFactory serviceComponentHostFactory;
-  private AmbariMetaInfo metaInfo;
+  private OrmTestHelper ormTestHelper;
+
+  private final String STACK_VERSION = "0.1";
+  private final String REPO_VERSION = "0.1-1234";
+  private final StackId STACK_ID = new StackId("HDP", STACK_VERSION);
+  private RepositoryVersionEntity repositoryVersion;
 
   @Before
   public void setup() throws Exception {
@@ -59,13 +65,14 @@ public class ServiceTest {
     injector.getInstance(GuiceJpaInitializer.class);
     clusters = injector.getInstance(Clusters.class);
     serviceFactory = injector.getInstance(ServiceFactory.class);
-    serviceComponentFactory = injector.getInstance(
-            ServiceComponentFactory.class);
-    serviceComponentHostFactory = injector.getInstance(
-            ServiceComponentHostFactory.class);
-    metaInfo = injector.getInstance(AmbariMetaInfo.class);
+    serviceComponentFactory = injector.getInstance(ServiceComponentFactory.class);
+    serviceComponentHostFactory = injector.getInstance(ServiceComponentHostFactory.class);
+
+    ormTestHelper = injector.getInstance(OrmTestHelper.class);
+    repositoryVersion = ormTestHelper.getOrCreateRepositoryVersion(STACK_ID, REPO_VERSION);
+
     clusterName = "foo";
-    clusters.addCluster(clusterName, new StackId("HDP-0.1"));
+    clusters.addCluster(clusterName, STACK_ID);
     cluster = clusters.getCluster(clusterName);
     Assert.assertNotNull(cluster);
   }
@@ -77,7 +84,7 @@ public class ServiceTest {
 
   @Test
   public void testCanBeRemoved() throws Exception{
-    Service service = cluster.addService("HDFS");
+    Service service = cluster.addService("HDFS", repositoryVersion);
 
     for (State state : State.values()) {
       service.setDesiredState(state);
@@ -119,15 +126,20 @@ public class ServiceTest {
   @Test
   public void testGetAndSetServiceInfo() throws AmbariException {
     String serviceName = "HDFS";
-    Service s = serviceFactory.createNew(cluster, serviceName);
+    Service s = serviceFactory.createNew(cluster, serviceName, repositoryVersion);
     cluster.addService(s);
 
     Service service = cluster.getService(serviceName);
     Assert.assertNotNull(service);
 
-    service.setDesiredStackVersion(new StackId("HDP-1.2.0"));
-    Assert.assertEquals("HDP-1.2.0",
-        service.getDesiredStackVersion().getStackId());
+    StackId desiredStackId = new StackId("HDP-1.2.0");
+    String desiredVersion = "1.2.0-1234";
+
+    RepositoryVersionEntity desiredRepositoryVersion = ormTestHelper.getOrCreateRepositoryVersion(
+        desiredStackId, desiredVersion);
+
+    service.setDesiredRepositoryVersion(desiredRepositoryVersion);
+    Assert.assertEquals(desiredStackId, service.getDesiredStackVersion());
 
     service.setDesiredState(State.INSTALLING);
     Assert.assertEquals(State.INSTALLING, service.getDesiredState());
@@ -140,7 +152,7 @@ public class ServiceTest {
   @Test
   public void testAddGetDeleteServiceComponents() throws AmbariException {
     String serviceName = "HDFS";
-    Service s = serviceFactory.createNew(cluster, serviceName);
+    Service s = serviceFactory.createNew(cluster, serviceName, repositoryVersion);
     cluster.addService(s);
 
     Service service = cluster.getService(serviceName);
@@ -223,30 +235,30 @@ public class ServiceTest {
   @Test
   public void testConvertToResponse() throws AmbariException {
     String serviceName = "HDFS";
-    Service s = serviceFactory.createNew(cluster, serviceName);
+    Service s = serviceFactory.createNew(cluster, serviceName, repositoryVersion);
     cluster.addService(s);
     Service service = cluster.getService(serviceName);
     Assert.assertNotNull(service);
 
     ServiceResponse r = s.convertToResponse();
     Assert.assertEquals(s.getName(), r.getServiceName());
-    Assert.assertEquals(s.getCluster().getClusterName(),
-        r.getClusterName());
-    Assert.assertEquals(s.getDesiredStackVersion().getStackId(),
-        r.getDesiredStackVersion());
-    Assert.assertEquals(s.getDesiredState().toString(),
-        r.getDesiredState());
-
-    service.setDesiredStackVersion(new StackId("HDP-1.2.0"));
+    Assert.assertEquals(s.getCluster().getClusterName(), r.getClusterName());
+    Assert.assertEquals(s.getDesiredStackVersion().getStackId(), r.getDesiredStackVersion());
+    Assert.assertEquals(s.getDesiredState().toString(), r.getDesiredState());
+
+    StackId desiredStackId = new StackId("HDP-1.2.0");
+    String desiredVersion = "1.2.0-1234";
+
+    RepositoryVersionEntity desiredRepositoryVersion = ormTestHelper.getOrCreateRepositoryVersion(
+        desiredStackId, desiredVersion);
+
+    service.setDesiredRepositoryVersion(desiredRepositoryVersion);
     service.setDesiredState(State.INSTALLING);
     r = s.convertToResponse();
     Assert.assertEquals(s.getName(), r.getServiceName());
-    Assert.assertEquals(s.getCluster().getClusterName(),
-        r.getClusterName());
-    Assert.assertEquals(s.getDesiredStackVersion().getStackId(),
-        r.getDesiredStackVersion());
-    Assert.assertEquals(s.getDesiredState().toString(),
-        r.getDesiredState());
+    Assert.assertEquals(s.getCluster().getClusterName(), r.getClusterName());
+    Assert.assertEquals(s.getDesiredStackVersion().getStackId(), r.getDesiredStackVersion());
+    Assert.assertEquals(s.getDesiredState().toString(), r.getDesiredState());
     // FIXME add checks for configs
 
     StringBuilder sb = new StringBuilder();
@@ -259,7 +271,7 @@ public class ServiceTest {
   @Test
   public void testServiceMaintenance() throws Exception {
     String serviceName = "HDFS";
-    Service s = serviceFactory.createNew(cluster, serviceName);
+    Service s = serviceFactory.createNew(cluster, serviceName, repositoryVersion);
     cluster.addService(s);
 
     Service service = cluster.getService(serviceName);
@@ -282,7 +294,7 @@ public class ServiceTest {
   @Test
   public void testSecurityState() throws Exception {
     String serviceName = "HDFS";
-    Service s = serviceFactory.createNew(cluster, serviceName);
+    Service s = serviceFactory.createNew(cluster, serviceName, repositoryVersion);
     cluster.addService(s);
 
     Service service = cluster.getService(serviceName);


Mime
View raw message