You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by dm...@apache.org on 2014/12/17 17:31:24 UTC

[2/2] ambari git commit: AMBARI-8718. Repository Version Management > Create relation between repo_version and stack_version (yshylov via dlysnichenko)

AMBARI-8718. Repository Version Management > Create relation between repo_version and stack_version (yshylov via dlysnichenko)


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

Branch: refs/heads/trunk
Commit: 92815dd588597842a02d3143657554a8085d880e
Parents: 97115a7
Author: Lisnichenko Dmitro <dl...@hortonworks.com>
Authored: Wed Dec 17 18:30:19 2014 +0200
Committer: Lisnichenko Dmitro <dl...@hortonworks.com>
Committed: Wed Dec 17 18:30:19 2014 +0200

----------------------------------------------------------------------
 .../server/api/services/StacksService.java      |  50 ---------
 .../ClusterStackVersionResourceProvider.java    |   7 +-
 .../HostStackVersionResourceProvider.java       |   4 +-
 .../DistributeRepositoriesActionListener.java   |   6 +-
 .../orm/entities/ClusterVersionEntity.java      |  87 ++++++++--------
 .../server/orm/entities/HostVersionEntity.java  | 101 +++++++++----------
 .../server/state/cluster/ClusterImpl.java       |  22 ++--
 .../ambari/server/state/stack/UpgradePack.java  |   2 +-
 .../server/upgrade/UpgradeCatalog200.java       |  31 ++----
 .../main/resources/Ambari-DDL-MySQL-CREATE.sql  |   8 +-
 .../main/resources/Ambari-DDL-Oracle-CREATE.sql |   8 +-
 .../resources/Ambari-DDL-Postgres-CREATE.sql    |   8 +-
 .../Ambari-DDL-Postgres-EMBEDDED-CREATE.sql     |   8 +-
 .../resources/Ambari-DDL-SQLServer-CREATE.sql   |   6 +-
 .../server/agent/TestHeartbeatHandler.java      |   4 +
 .../server/agent/TestHeartbeatMonitor.java      |   9 +-
 .../server/api/services/StacksServiceTest.java  |  13 ---
 .../AmbariManagementControllerTest.java         |  12 +++
 .../StackDefinedPropertyProviderTest.java       |   9 +-
 .../internal/UpgradeResourceProviderTest.java   |   7 +-
 .../apache/ambari/server/events/EventsTest.java |   5 +
 .../apache/ambari/server/orm/OrmTestHelper.java |  24 +++++
 .../server/orm/dao/ClusterVersionDAOTest.java   |  16 +--
 .../server/orm/dao/HostVersionDAOTest.java      |  61 ++++++-----
 .../server/state/ServiceComponentTest.java      |   8 +-
 .../ambari/server/state/UpgradeHelperTest.java  |   7 +-
 .../server/state/cluster/ClusterTest.java       |  64 ++++++------
 .../server/state/cluster/ClustersTest.java      |  40 ++++----
 .../ambari/server/state/host/HostTest.java      |  49 +++++----
 .../svccomphost/ServiceComponentHostTest.java   |   8 ++
 .../server/upgrade/UpgradeCatalog200Test.java   |   4 +-
 31 files changed, 358 insertions(+), 330 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/92815dd5/ambari-server/src/main/java/org/apache/ambari/server/api/services/StacksService.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/api/services/StacksService.java b/ambari-server/src/main/java/org/apache/ambari/server/api/services/StacksService.java
index 4d0fb31..cdadfd9 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/api/services/StacksService.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/api/services/StacksService.java
@@ -250,46 +250,6 @@ public class StacksService extends BaseService {
     return new RepositoryVersionService(stackProperties);
   }
 
-  @GET
-  @Path("{stackName}/versions/{stackVersion}/repository_versions/{repositoryVersionId}/operating_systems/{osType}/repositories")
-  @Produces("text/plain")
-  public Response getRepositories(String body, @Context HttpHeaders headers, @Context UriInfo ui, @PathParam("stackName") String stackName,
-      @PathParam("stackVersion") String stackVersion, @PathParam("repositoryVersionId") String repositoryVersionId, @PathParam("osType") String osType) {
-
-    return handleRequest(headers, body, new StackUriInfo(ui), Request.Type.GET, createRepositoryResource(stackName, stackVersion, repositoryVersionId, osType, null));
-  }
-
-  @GET
-  @Path("{stackName}/versions/{stackVersion}/repository_versions/{repositoryVersionId}/operating_systems/{osType}/repositories/{repoId}")
-  @Produces("text/plain")
-  public Response getRepository(String body, @Context HttpHeaders headers, @Context UriInfo ui, @PathParam("stackName") String stackName,
-      @PathParam("stackVersion") String stackVersion, @PathParam("repositoryVersionId") String repositoryVersionId, @PathParam("osType") String osType, @PathParam("repoId") String repoId) {
-
-    return handleRequest(headers, body, new StackUriInfo(ui), Request.Type.GET, createRepositoryResource(stackName, stackVersion, repositoryVersionId, osType, repoId));
-  }
-
-  ResourceInstance createRepositoryVersionResource(String stackName, String stackVersion,
-                                                   String repositoryVersionId) {
-    final Map<Resource.Type, String> mapIds = new HashMap<Resource.Type, String>();
-
-    mapIds.put(Resource.Type.Stack, stackName);
-    mapIds.put(Resource.Type.StackVersion, stackVersion);
-    mapIds.put(Resource.Type.RepositoryVersion, repositoryVersionId);
-
-    return createResource(Resource.Type.RepositoryVersion, mapIds);
-  }
-
-  ResourceInstance createOperatingSystemResource(String stackName, String stackVersion, String repositoryVersion, String osType) {
-    final Map<Resource.Type, String> mapIds = new HashMap<Resource.Type, String>();
-
-    mapIds.put(Resource.Type.Stack, stackName);
-    mapIds.put(Resource.Type.StackVersion, stackVersion);
-    mapIds.put(Resource.Type.OperatingSystem, osType);
-    mapIds.put(Resource.Type.RepositoryVersion, repositoryVersion);
-
-    return createResource(Resource.Type.OperatingSystem, mapIds);
-  }
-
   ResourceInstance createStackServiceComponentResource(
       String stackName, String stackVersion, String serviceName, String componentName) {
 
@@ -337,16 +297,6 @@ public class StacksService extends BaseService {
     return createResource(Resource.Type.StackService, mapIds);
   }
 
-  ResourceInstance createRepositoryResource(String stackName, String stackVersion, String repositoryVersion, String osType, String repoId) {
-    final Map<Resource.Type, String> mapIds = new HashMap<Resource.Type, String>();
-    mapIds.put(Resource.Type.Stack, stackName);
-    mapIds.put(Resource.Type.StackVersion, stackVersion);
-    mapIds.put(Resource.Type.OperatingSystem, osType);
-    mapIds.put(Resource.Type.Repository, repoId);
-    mapIds.put(Resource.Type.RepositoryVersion, repositoryVersion);
-    return createResource(Resource.Type.Repository, mapIds);
-  }
-
   ResourceInstance createStackVersionResource(String stackName,
                                               String stackVersion) {
     Map<Resource.Type, String> mapIds = new HashMap<Resource.Type, String>();

http://git-wip-us.apache.org/repos/asf/ambari/blob/92815dd5/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ClusterStackVersionResourceProvider.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ClusterStackVersionResourceProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ClusterStackVersionResourceProvider.java
index 09f9b7f..9329ea9 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ClusterStackVersionResourceProvider.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ClusterStackVersionResourceProvider.java
@@ -193,11 +193,12 @@ public class ClusterStackVersionResourceProvider extends AbstractControllerResou
       for (RepositoryVersionState state: RepositoryVersionState.values()) {
         hostStates.put(state.name(), new ArrayList<String>());
       }
-      for (HostVersionEntity hostVersionEntity: hostVersionDAO.findByClusterStackAndVersion(entity.getClusterEntity().getClusterName(), entity.getStack(), entity.getVersion())) {
+      for (HostVersionEntity hostVersionEntity: hostVersionDAO.findByClusterStackAndVersion(entity.getClusterEntity().getClusterName(),
+          entity.getRepositoryVersion().getStack(), entity.getRepositoryVersion().getVersion())) {
         hostStates.get(hostVersionEntity.getState().name()).add(hostVersionEntity.getHostName());
       }
-      StackId stackId = new StackId(entity.getStack());
-      RepositoryVersionEntity repoVerEntity = repositoryVersionDAO.findByStackAndVersion(stackId.getStackId(), entity.getVersion());
+      StackId stackId = new StackId(entity.getRepositoryVersion().getStack());
+      RepositoryVersionEntity repoVerEntity = repositoryVersionDAO.findByStackAndVersion(stackId.getStackId(), entity.getRepositoryVersion().getVersion());
 
       setResourceProperty(resource, CLUSTER_STACK_VERSION_CLUSTER_NAME_PROPERTY_ID, entity.getClusterEntity().getClusterName(), requestedIds);
       setResourceProperty(resource, CLUSTER_STACK_VERSION_HOST_STATES_PROPERTY_ID, hostStates, requestedIds);

http://git-wip-us.apache.org/repos/asf/ambari/blob/92815dd5/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/HostStackVersionResourceProvider.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/HostStackVersionResourceProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/HostStackVersionResourceProvider.java
index e26a800..3b1b462 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/HostStackVersionResourceProvider.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/HostStackVersionResourceProvider.java
@@ -204,9 +204,9 @@ public class HostStackVersionResourceProvider extends AbstractControllerResource
                                    Set<String> requestedIds,
                                    String clusterName) {
     for (HostVersionEntity entity: requestedEntities) {
-      StackId stackId = new StackId(entity.getStack());
+      StackId stackId = new StackId(entity.getRepositoryVersion().getStack());
 
-      RepositoryVersionEntity repoVerEntity = repositoryVersionDAO.findByStackAndVersion(stackId.getStackId(), entity.getVersion());
+      RepositoryVersionEntity repoVerEntity = repositoryVersionDAO.findByStackAndVersion(stackId.getStackId(), entity.getRepositoryVersion().getVersion());
 
       final Resource resource = new ResourceImpl(Resource.Type.HostStackVersion);
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/92815dd5/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/upgrade/DistributeRepositoriesActionListener.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/upgrade/DistributeRepositoriesActionListener.java b/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/upgrade/DistributeRepositoriesActionListener.java
index 428a0d2..da6e680 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/upgrade/DistributeRepositoriesActionListener.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/upgrade/DistributeRepositoriesActionListener.java
@@ -140,7 +140,7 @@ public class DistributeRepositoriesActionListener {
     }
     List<HostVersionEntity> hostVersions = hostVersionDAO.get().findByHost(event.getHostname());
     for (HostVersionEntity hostVersion : hostVersions) {
-      if (repositoryVersion != null && ! hostVersion.getVersion().equals(repositoryVersion)) {
+      if (repositoryVersion != null && ! hostVersion.getRepositoryVersion().getVersion().equals(repositoryVersion)) {
         // Are we going to update state of a concrete host stack version?
         continue;
       }
@@ -151,7 +151,7 @@ public class DistributeRepositoriesActionListener {
         if (clusterId != null) { // Update state of a cluster stack version
           try {
             Cluster cluster = clusters.get().getClusterById(clusterId);
-            cluster.recalculateClusterVersionState(hostVersion.getVersion());
+            cluster.recalculateClusterVersionState(hostVersion.getRepositoryVersion().getVersion());
           } catch (AmbariException e) {
             LOG.error("Can not get cluster with Id " + clusterId, e);
           }
@@ -161,7 +161,7 @@ public class DistributeRepositoriesActionListener {
           try {
             Set<Cluster> clustersForHost = clusters.get().getClustersForHost(event.getHostname());
             for (Cluster cluster : clustersForHost) {
-              cluster.recalculateClusterVersionState(hostVersion.getVersion());
+              cluster.recalculateClusterVersionState(hostVersion.getRepositoryVersion().getVersion());
             }
           } catch (AmbariException e) {
             LOG.error("Can not update state of clusters", e);

http://git-wip-us.apache.org/repos/asf/ambari/blob/92815dd5/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ClusterVersionEntity.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ClusterVersionEntity.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ClusterVersionEntity.java
index a7d4d24..01b9c03 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ClusterVersionEntity.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ClusterVersionEntity.java
@@ -18,13 +18,13 @@
 
 package org.apache.ambari.server.orm.entities;
 
-import org.apache.ambari.server.state.RepositoryVersionState;
+import static org.apache.commons.lang.StringUtils.defaultString;
 
 import javax.persistence.Basic;
 import javax.persistence.Column;
 import javax.persistence.Entity;
-import javax.persistence.Enumerated;
 import javax.persistence.EnumType;
+import javax.persistence.Enumerated;
 import javax.persistence.GeneratedValue;
 import javax.persistence.GenerationType;
 import javax.persistence.Id;
@@ -32,10 +32,10 @@ import javax.persistence.JoinColumn;
 import javax.persistence.ManyToOne;
 import javax.persistence.NamedQueries;
 import javax.persistence.NamedQuery;
-import javax.persistence.TableGenerator;
 import javax.persistence.Table;
+import javax.persistence.TableGenerator;
 
-import static org.apache.commons.lang.StringUtils.defaultString;
+import org.apache.ambari.server.state.RepositoryVersionState;
 
 @Table(name = "cluster_version")
 @Entity
@@ -48,7 +48,7 @@ import static org.apache.commons.lang.StringUtils.defaultString;
 @NamedQueries({
     @NamedQuery(name = "clusterVersionByClusterAndStackAndVersion", query =
         "SELECT clusterVersion FROM ClusterVersionEntity clusterVersion JOIN clusterVersion.clusterEntity cluster " +
-        "WHERE cluster.clusterName=:clusterName AND clusterVersion.stack=:stack AND clusterVersion.version=:version"),
+        "WHERE cluster.clusterName=:clusterName AND clusterVersion.repositoryVersion.stack=:stack AND clusterVersion.repositoryVersion.version=:version"),
     @NamedQuery(name = "clusterVersionByClusterAndState", query =
         "SELECT clusterVersion FROM ClusterVersionEntity clusterVersion JOIN clusterVersion.clusterEntity cluster " +
         "WHERE cluster.clusterName=:clusterName AND clusterVersion.state=:state"),
@@ -56,7 +56,7 @@ import static org.apache.commons.lang.StringUtils.defaultString;
         "SELECT clusterVersion FROM ClusterVersionEntity clusterVersion JOIN clusterVersion.clusterEntity cluster " +
         "WHERE cluster.clusterName=:clusterName"),
     @NamedQuery(name = "clusterVersionByStackVersion",
-        query = "SELECT clusterVersion FROM ClusterVersionEntity clusterVersion WHERE clusterVersion.stack=:stack AND clusterVersion.version=:version"),
+        query = "SELECT clusterVersion FROM ClusterVersionEntity clusterVersion WHERE clusterVersion.repositoryVersion.stack=:stack AND clusterVersion.repositoryVersion.version=:version"),
 })
 public class ClusterVersionEntity {
 
@@ -72,13 +72,9 @@ public class ClusterVersionEntity {
   @JoinColumn(name = "cluster_id", referencedColumnName = "cluster_id", nullable = false)
   private ClusterEntity clusterEntity;
 
-  @Basic
-  @Column(name = "stack", nullable = false, insertable = true, updatable = true)
-  private String stack = "";
-
-  @Basic
-  @Column(name = "version", nullable = false, insertable = true, updatable = true)
-  private String version = "";
+  @ManyToOne
+  @JoinColumn(name = "repo_version_id", referencedColumnName = "repo_version_id", nullable = false)
+  private RepositoryVersionEntity repositoryVersion;
 
   @Column(name = "state", nullable = false, insertable = true, updatable = true)
   @Enumerated(value = EnumType.STRING)
@@ -105,17 +101,15 @@ public class ClusterVersionEntity {
   /**
    * Full constructor that doesn't have the endTime
    * @param cluster Cluster entity
-   * @param stack Stack name (e.g., HDP)
-   * @param version Stack version (e.g., 2.2.0.0-995)
+   * @param repositoryVersion repository version
    * @param state Cluster version state
    * @param startTime Time the cluster version reached its first state
    * @param userName User who performed the action
    */
-  public ClusterVersionEntity(ClusterEntity cluster, String stack, String version, RepositoryVersionState state, long startTime, String userName) {
+  public ClusterVersionEntity(ClusterEntity cluster, RepositoryVersionEntity repositoryVersion, RepositoryVersionState state, long startTime, String userName) {
     this.clusterId = cluster.getClusterId();
+    this.repositoryVersion = repositoryVersion;
     this.clusterEntity = cluster;
-    this.stack = stack;
-    this.version = version;
     this.state = state;
     this.startTime = startTime;
     this.userName = userName;
@@ -124,15 +118,14 @@ public class ClusterVersionEntity {
   /**
    * Full constructor that does have the endTime
    * @param cluster Cluster entity
-   * @param stack Stack name (e.g., HDP)
-   * @param version Stack version (e.g., 2.2.0.0-995)
+   * @param repositoryVersion repository version
    * @param state Cluster version state
    * @param startTime Time the cluster version reached its first state
    * @param endTime Time the cluster version finalized its state
    * @param userName User who performed the action
    */
-  public ClusterVersionEntity(ClusterEntity cluster, String stack, String version, RepositoryVersionState state, long startTime, long endTime, String userName) {
-    this(cluster, stack, version, state, startTime, userName);
+  public ClusterVersionEntity(ClusterEntity cluster, RepositoryVersionEntity repositoryVersion, RepositoryVersionState state, long startTime, long endTime, String userName) {
+    this(cluster, repositoryVersion, state, startTime, userName);
     this.endTime = endTime;
   }
 
@@ -160,22 +153,6 @@ public class ClusterVersionEntity {
     this.clusterEntity = clusterEntity;
   }
 
-  public String getStack() {
-    return defaultString(stack);
-  }
-
-  public void setStack(String stack) {
-    this.stack = stack;
-  }
-
-  public String getVersion() {
-    return defaultString(version);
-  }
-
-  public void setVersion(String version) {
-    this.version = version;
-  }
-
   public RepositoryVersionState getState() {
     return state;
   }
@@ -196,17 +173,34 @@ public class ClusterVersionEntity {
 
   public void setUserName(String userName) { this.userName = userName; }
 
+  public void setRepositoryVersion(RepositoryVersionEntity repositoryVersion) {
+    this.repositoryVersion = repositoryVersion;
+  }
+
+  public RepositoryVersionEntity getRepositoryVersion() {
+    return repositoryVersion;
+  }
+
   @Override
   public boolean equals(Object o) {
-    if (this == o) return true;
-    if (o == null || getClass() != o.getClass()) return false;
+    if (this == o) {
+      return true;
+    }
+    if (o == null || getClass() != o.getClass()) {
+      return false;
+    }
 
     ClusterVersionEntity that = (ClusterVersionEntity) o;
 
-    if (this.id != that.id || this.clusterId != that.clusterId || !this.stack.equals(that.stack)
-        || !this.version.equals(that.version) || !this.state.equals(that.state)
-        || !this.startTime.equals(that.startTime) || !this.endTime.equals(that.endTime)
-        || !this.userName.equals(that.userName)) return false;
+    if (this.id != that.id
+        || this.clusterId != that.clusterId
+        || !this.repositoryVersion.equals(that.repositoryVersion)
+        || !this.state.equals(that.state)
+        || !this.startTime.equals(that.startTime)
+        || !this.endTime.equals(that.endTime)
+        || !this.userName.equals(that.userName)) {
+      return false;
+    }
 
     return true;
   }
@@ -215,11 +209,10 @@ public class ClusterVersionEntity {
   public int hashCode() {
     int result = id !=null ? id.intValue() : 0;
     result = 31 * result + (clusterId != null ? clusterId.hashCode() : 0);
-    result = 31 * result + (stack != null ? stack.hashCode() : 0);
-    result = 31 * result + (version != null ? version.hashCode() : 0);
+    result = 31 * result + (repositoryVersion != null ? repositoryVersion.hashCode() : 0);
     result = 31 * result + (state != null ? state.hashCode() : 0);
     result = 31 * result + (startTime != null ? startTime.hashCode() : 0);
-    result = 31 * result + (endTime != null ? stack.hashCode() : 0);
+    result = 31 * result + (endTime != null ? endTime.hashCode() : 0);
     result = 31 * result + (userName != null ? userName.hashCode() : 0);
     return result;
   }

http://git-wip-us.apache.org/repos/asf/ambari/blob/92815dd5/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/HostVersionEntity.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/HostVersionEntity.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/HostVersionEntity.java
index c090056..4d8f3be 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/HostVersionEntity.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/HostVersionEntity.java
@@ -18,23 +18,21 @@
 
 package org.apache.ambari.server.orm.entities;
 
-import org.apache.ambari.server.state.RepositoryVersionState;
-import javax.persistence.Basic;
 import javax.persistence.Column;
 import javax.persistence.Entity;
-import javax.persistence.Enumerated;
 import javax.persistence.EnumType;
+import javax.persistence.Enumerated;
 import javax.persistence.GeneratedValue;
 import javax.persistence.GenerationType;
 import javax.persistence.Id;
 import javax.persistence.JoinColumn;
 import javax.persistence.ManyToOne;
-import javax.persistence.NamedQuery;
 import javax.persistence.NamedQueries;
-import javax.persistence.TableGenerator;
+import javax.persistence.NamedQuery;
 import javax.persistence.Table;
+import javax.persistence.TableGenerator;
 
-import static org.apache.commons.lang.StringUtils.defaultString;
+import org.apache.ambari.server.state.RepositoryVersionState;
 
 @Table(name = "host_version")
 @Entity
@@ -47,7 +45,7 @@ import static org.apache.commons.lang.StringUtils.defaultString;
 @NamedQueries({
     @NamedQuery(name = "hostVersionByClusterAndStackAndVersion", query =
         "SELECT hostVersion FROM HostVersionEntity hostVersion JOIN hostVersion.hostEntity host JOIN ClusterEntity cluster " +
-            "WHERE cluster.clusterName=:clusterName AND hostVersion.stack=:stack AND hostVersion.version=:version"),
+            "WHERE cluster.clusterName=:clusterName AND hostVersion.repositoryVersion.stack=:stack AND hostVersion.repositoryVersion.version=:version"),
 
     @NamedQuery(name = "hostVersionByClusterAndHostname", query =
         "SELECT hostVersion FROM HostVersionEntity hostVersion JOIN hostVersion.hostEntity host JOIN ClusterEntity cluster " +
@@ -63,7 +61,7 @@ import static org.apache.commons.lang.StringUtils.defaultString;
 
     @NamedQuery(name = "hostVersionByClusterStackVersionAndHostname", query =
         "SELECT hostVersion FROM HostVersionEntity hostVersion JOIN hostVersion.hostEntity host JOIN ClusterEntity cluster " +
-            "WHERE cluster.clusterName=:clusterName AND hostVersion.stack=:stack AND hostVersion.version=:version AND " +
+            "WHERE cluster.clusterName=:clusterName AND hostVersion.repositoryVersion.stack=:stack AND hostVersion.repositoryVersion.version=:version AND " +
             "hostVersion.hostName=:hostName"),
 })
 public class HostVersionEntity {
@@ -77,17 +75,13 @@ public class HostVersionEntity {
   private String hostName;
 
   @ManyToOne
+  @JoinColumn(name = "repo_version_id", referencedColumnName = "repo_version_id", nullable = false)
+  private RepositoryVersionEntity repositoryVersion;
+
+  @ManyToOne
   @JoinColumn(name = "host_name", referencedColumnName = "host_name", nullable = false)
   private HostEntity hostEntity;
 
-  @Basic
-  @Column(name = "stack", nullable = false, insertable = true, updatable = true)
-  private String stack = "";
-
-  @Basic
-  @Column(name = "version", nullable = false, insertable = true, updatable = true)
-  private String version = "";
-
   @Column(name = "state", nullable = false, insertable = true, updatable = true)
   @Enumerated(value = EnumType.STRING)
   private RepositoryVersionState state;
@@ -98,10 +92,9 @@ public class HostVersionEntity {
   public HostVersionEntity() {
   }
 
-  public HostVersionEntity(String hostName, String stack, String version, RepositoryVersionState state) {
+  public HostVersionEntity(String hostName, RepositoryVersionEntity repositoryVersion, RepositoryVersionState state) {
     this.hostName = hostName;
-    this.stack = stack;
-    this.version = version;
+    this.repositoryVersion = repositoryVersion;
     this.state = state;
   }
 
@@ -110,8 +103,7 @@ public class HostVersionEntity {
    */
   public HostVersionEntity(HostVersionEntity other) {
     this.hostName = other.hostName;
-    this.stack = other.stack;
-    this.version = other.version;
+    this.repositoryVersion = other.repositoryVersion;
     this.state = other.state;
   }
 
@@ -139,22 +131,6 @@ public class HostVersionEntity {
     this.hostEntity = hostEntity;
   }
 
-  public String getStack() {
-    return defaultString(stack);
-  }
-
-  public void setStack(String stack) {
-    this.stack = stack;
-  }
-
-  public String getVersion() {
-    return defaultString(version);
-  }
-
-  public void setVersion(String version) {
-    this.version = version;
-  }
-
   public RepositoryVersionState getState() {
     return state;
   }
@@ -163,26 +139,47 @@ public class HostVersionEntity {
     this.state = state;
   }
 
-  @Override
-  public boolean equals(Object o) {
-    if (this == o) return true;
-    if (o == null || getClass() != o.getClass()) return false;
-
-    HostVersionEntity that = (HostVersionEntity) o;
-
-    if (this.id != that.id || !this.hostName.equals(that.hostName) || !this.stack.equals(that.stack)
-        || !this.version.equals(that.version) || !this.state.equals(that.state)) return false;
+  public RepositoryVersionEntity getRepositoryVersion() {
+    return repositoryVersion;
+  }
 
-    return true;
+  public void setRepositoryVersion(RepositoryVersionEntity repositoryVersion) {
+    this.repositoryVersion = repositoryVersion;
   }
 
   @Override
   public int hashCode() {
-    int result = id !=null ? id.intValue() : 0;
-    result = 31 * result + (hostName != null ? hostName.hashCode() : 0);
-    result = 31 * result + (stack != null ? stack.hashCode() : 0);
-    result = 31 * result + (version != null ? version.hashCode() : 0);
-    result = 31 * result + (state != null ? state.hashCode() : 0);
+    final int prime = 31;
+    int result = 1;
+    result = prime * result + ((hostEntity == null) ? 0 : hostEntity.hashCode());
+    result = prime * result + ((hostName == null) ? 0 : hostName.hashCode());
+    result = prime * result + ((id == null) ? 0 : id.hashCode());
+    result = prime * result + ((repositoryVersion == null) ? 0 : repositoryVersion.hashCode());
+    result = prime * result + ((state == null) ? 0 : state.hashCode());
     return result;
   }
+
+  @Override
+  public boolean equals(Object obj) {
+    if (this == obj) return true;
+    if (obj == null) return false;
+    if (getClass() != obj.getClass()) return false;
+
+    HostVersionEntity other = (HostVersionEntity) obj;
+    if (hostEntity == null) {
+      if (other.hostEntity != null) return false;
+    } else if (!hostEntity.equals(other.hostEntity)) return false;
+    if (hostName == null) {
+      if (other.hostName != null) return false;
+    } else if (!hostName.equals(other.hostName)) return false;
+    if (id == null) {
+      if (other.id != null) return false;
+    } else if (!id.equals(other.id)) return false;
+    if (repositoryVersion == null) {
+      if (other.repositoryVersion != null) return false;
+    } else if (!repositoryVersion.equals(other.repositoryVersion)) return false;
+    if (state != other.state) return false;
+    return true;
+  }
+
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/92815dd5/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClusterImpl.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClusterImpl.java b/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClusterImpl.java
index e3f4893..a90fcc7 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClusterImpl.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClusterImpl.java
@@ -61,6 +61,7 @@ import org.apache.ambari.server.orm.dao.ConfigGroupHostMappingDAO;
 import org.apache.ambari.server.orm.dao.HostConfigMappingDAO;
 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.ServiceConfigDAO;
 import org.apache.ambari.server.orm.dao.UpgradeDAO;
 import org.apache.ambari.server.orm.entities.ClusterConfigEntity;
@@ -74,6 +75,7 @@ import org.apache.ambari.server.orm.entities.HostEntity;
 import org.apache.ambari.server.orm.entities.HostVersionEntity;
 import org.apache.ambari.server.orm.entities.PermissionEntity;
 import org.apache.ambari.server.orm.entities.PrivilegeEntity;
+import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
 import org.apache.ambari.server.orm.entities.RequestScheduleEntity;
 import org.apache.ambari.server.orm.entities.ResourceEntity;
 import org.apache.ambari.server.orm.entities.ServiceConfigEntity;
@@ -220,6 +222,9 @@ public class ClusterImpl implements Cluster {
   private UpgradeDAO upgradeDAO;
 
   @Inject
+  private RepositoryVersionDAO repositoryVersionDAO;
+
+  @Inject
   private AmbariSessionManager sessionManager;
 
   private volatile boolean svcHostsLoaded = false;
@@ -1140,7 +1145,8 @@ public class ClusterImpl implements Cluster {
       readWriteLock.writeLock().lock();
       try {
         Set<String> existingHostsWithClusterStackAndVersion = new HashSet<String>();
-        List<HostVersionEntity> existingHostVersionEntities = hostVersionDAO.findByClusterStackAndVersion(this.getClusterName(), currentClusterVersion.getStack(), currentClusterVersion.getVersion());
+        List<HostVersionEntity> existingHostVersionEntities = hostVersionDAO.findByClusterStackAndVersion(this.getClusterName(),
+            currentClusterVersion.getRepositoryVersion().getStack(), currentClusterVersion.getRepositoryVersion().getVersion());
         if (existingHostVersionEntities != null) {
           for (HostVersionEntity entity : existingHostVersionEntities) {
             existingHostsWithClusterStackAndVersion.add(entity.getHostName());
@@ -1154,7 +1160,7 @@ public class ClusterImpl implements Cluster {
           // to be robust and only insert records for the missing hosts.
           if (!intersection.contains(hostname)) {
             HostEntity hostEntity = hostDAO.findByName(hostname);
-            HostVersionEntity hostVersionEntity = new HostVersionEntity(hostname, currentClusterVersion.getStack(), currentClusterVersion.getVersion(), desiredState);
+            HostVersionEntity hostVersionEntity = new HostVersionEntity(hostname, currentClusterVersion.getRepositoryVersion(), desiredState);
             hostVersionEntity.setHostEntity(hostEntity);
             hostVersionDAO.create(hostVersionEntity);
           }
@@ -1190,7 +1196,7 @@ public class ClusterImpl implements Cluster {
         Set<String> existingHostsWithClusterStackAndVersion = new HashSet<String>();
         HashMap<String, HostVersionEntity>  existingHostStackVersions = new HashMap<String, HostVersionEntity>();
         List<HostVersionEntity> existingHostVersionEntities = hostVersionDAO.findByClusterStackAndVersion(this.getClusterName(),
-                sourceClusterVersion.getStack(), sourceClusterVersion.getVersion());
+                sourceClusterVersion.getRepositoryVersion().getStack(), sourceClusterVersion.getRepositoryVersion().getVersion());
         if (existingHostVersionEntities != null) {
           for (HostVersionEntity entity : existingHostVersionEntities) {
             existingHostsWithClusterStackAndVersion.add(entity.getHostName());
@@ -1206,8 +1212,7 @@ public class ClusterImpl implements Cluster {
           if (hostsMissingRepoVersion.contains(hostname)) {
             // Create new host stack version
             HostEntity hostEntity = hostDAO.findByName(hostname);
-            HostVersionEntity hostVersionEntity = new HostVersionEntity(hostname, sourceClusterVersion.getStack(),
-                    sourceClusterVersion.getVersion(), RepositoryVersionState.INSTALLING);
+            HostVersionEntity hostVersionEntity = new HostVersionEntity(hostname, sourceClusterVersion.getRepositoryVersion(), RepositoryVersionState.INSTALLING);
             hostVersionEntity.setHostEntity(hostEntity);
             hostVersionDAO.create(hostVersionEntity);
           } else {
@@ -1319,7 +1324,12 @@ public class ClusterImpl implements Cluster {
               version + " for cluster " + this.getClusterName() + " already exists");
         }
 
-        ClusterVersionEntity clusterVersionEntity = new ClusterVersionEntity(this.clusterEntity, stack, version, state, System.currentTimeMillis(), System.currentTimeMillis(), userName);
+        RepositoryVersionEntity repositoryVersionEntity = repositoryVersionDAO.findByStackAndVersion(stack, version);
+        if (repositoryVersionEntity == null) {
+          throw new AmbariException("Could not find repository version for stack=" + stack + ", version=" + version );
+        }
+
+        ClusterVersionEntity clusterVersionEntity = new ClusterVersionEntity(this.clusterEntity, repositoryVersionEntity, state, System.currentTimeMillis(), System.currentTimeMillis(), userName);
         clusterVersionDAO.create(clusterVersionEntity);
       } finally {
         readWriteLock.writeLock().unlock();

http://git-wip-us.apache.org/repos/asf/ambari/blob/92815dd5/ambari-server/src/main/java/org/apache/ambari/server/state/stack/UpgradePack.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/stack/UpgradePack.java b/ambari-server/src/main/java/org/apache/ambari/server/state/stack/UpgradePack.java
index 8d2a902..736a99c 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/state/stack/UpgradePack.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/state/stack/UpgradePack.java
@@ -94,7 +94,7 @@ public class UpgradePack {
   }
 
   /**
-   * A service definition that holds a list of componenents in the 'order' element.
+   * A service definition that holds a list of components in the 'order' element.
    */
   public static class OrderService {
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/92815dd5/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog200.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog200.java b/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog200.java
index 2357004..9201f52 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog200.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog200.java
@@ -116,8 +116,6 @@ public class UpgradeCatalog200 extends AbstractUpgradeCatalog {
         String.class, 2000, null, true));
     dbAccessor.alterColumn("viewinstancedata", new DBColumnInfo("value",
         String.class, 2000, null, true));
-
-    ddlUpdateRepositoryVersion();
   }
 
   /**
@@ -154,13 +152,13 @@ public class UpgradeCatalog200 extends AbstractUpgradeCatalog {
   }
 
   /**
-   * Creates repoversion table and all its constraints and dependencies.
-   *
-   * @throws SQLException if SQL error happens
+   * Add any columns, tables, and keys needed for Rolling Upgrades.
+   * @throws SQLException
    */
-  private void ddlUpdateRepositoryVersion() throws SQLException {
-    final List<DBColumnInfo> columns = new ArrayList<DBColumnInfo>();
-    columns.add(new DBColumnInfo("repoversion_id",  Long.class,    null,  null, false));
+  private void prepareRollingUpgradesDDL() throws SQLException {
+    List<DBAccessor.DBColumnInfo> columns = new ArrayList<DBAccessor.DBColumnInfo>();
+
+    columns.add(new DBColumnInfo("repo_version_id", Long.class,    null,  null, false));
     columns.add(new DBColumnInfo("stack",           String.class,  255,   null, false));
     columns.add(new DBColumnInfo("version",         String.class,  255,   null, false));
     columns.add(new DBColumnInfo("display_name",    String.class,  128,   null, false));
@@ -170,14 +168,6 @@ public class UpgradeCatalog200 extends AbstractUpgradeCatalog {
     dbAccessor.executeQuery("INSERT INTO ambari_sequences(sequence_name, sequence_value) VALUES('repo_version_id_seq', 0)", false);
     dbAccessor.executeQuery("ALTER TABLE repo_version ADD CONSTRAINT UQ_repo_version_display_name UNIQUE (display_name)");
     dbAccessor.executeQuery("ALTER TABLE repo_version ADD CONSTRAINT UQ_repo_version_stack_version UNIQUE (stack, version)");
-  }
-
-  /**
-   * Add any columns, tables, and keys needed for Rolling Upgrades.
-   * @throws SQLException
-   */
-  private void prepareRollingUpgradesDDL() throws SQLException {
-    List<DBAccessor.DBColumnInfo> columns = new ArrayList<DBAccessor.DBColumnInfo>();
 
     // New columns
     dbAccessor.addColumn("hostcomponentstate", new DBAccessor.DBColumnInfo("upgrade_state",
@@ -187,10 +177,10 @@ public class UpgradeCatalog200 extends AbstractUpgradeCatalog {
         Integer.class, 1, 0, false));
 
     // New tables
+    columns = new ArrayList<DBColumnInfo>();
     columns.add(new DBAccessor.DBColumnInfo("id", Long.class, null, null, false));
+    columns.add(new DBAccessor.DBColumnInfo("repo_version_id", Long.class, null, null, false));
     columns.add(new DBAccessor.DBColumnInfo("cluster_id", Long.class, null, null, false));
-    columns.add(new DBAccessor.DBColumnInfo("stack", String.class, 255, null, false));
-    columns.add(new DBAccessor.DBColumnInfo("version", String.class, 255, null, false));
     columns.add(new DBAccessor.DBColumnInfo("state", String.class, 32, null, false));
     columns.add(new DBAccessor.DBColumnInfo("start_time", Long.class, null, null, false));
     columns.add(new DBAccessor.DBColumnInfo("end_time", Long.class, null, null, true));
@@ -199,15 +189,16 @@ public class UpgradeCatalog200 extends AbstractUpgradeCatalog {
 
     columns = new ArrayList<DBColumnInfo>();
     columns.add(new DBAccessor.DBColumnInfo("id", Long.class, null, null, false));
+    columns.add(new DBAccessor.DBColumnInfo("repo_version_id", Long.class, null, null, false));
     columns.add(new DBAccessor.DBColumnInfo("host_name", String.class, 255, null, false));
-    columns.add(new DBAccessor.DBColumnInfo("stack", String.class, 255, null, false));
-    columns.add(new DBAccessor.DBColumnInfo("version", String.class, 255, null, false));
     columns.add(new DBAccessor.DBColumnInfo("state", String.class, 32, null, false));
     dbAccessor.createTable("host_version", columns, "id");
 
     // Foreign Key Constraints
     dbAccessor.addFKConstraint("cluster_version", "FK_cluster_version_cluster_id", "cluster_id", "clusters", "cluster_id", false);
+    dbAccessor.addFKConstraint("cluster_version", "FK_cluster_version_repovers_id", "repo_version_id", "repo_version", "repo_version_id", false);
     dbAccessor.addFKConstraint("host_version", "FK_host_version_host_name", "host_name", "hosts", "host_name", false);
+    dbAccessor.addFKConstraint("host_version", "FK_host_version_repovers_id", "repo_version_id", "repo_version", "repo_version_id", false);
 
     // New sequences
     dbAccessor.executeQuery("INSERT INTO ambari_sequences(sequence_name, sequence_value) VALUES('cluster_version_id_seq', 0)", false);

http://git-wip-us.apache.org/repos/asf/ambari/blob/92815dd5/ambari-server/src/main/resources/Ambari-DDL-MySQL-CREATE.sql
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/resources/Ambari-DDL-MySQL-CREATE.sql b/ambari-server/src/main/resources/Ambari-DDL-MySQL-CREATE.sql
index b3cb6bd..d6229b3 100644
--- a/ambari-server/src/main/resources/Ambari-DDL-MySQL-CREATE.sql
+++ b/ambari-server/src/main/resources/Ambari-DDL-MySQL-CREATE.sql
@@ -82,10 +82,9 @@ CREATE TABLE clusterstate (
 
 CREATE TABLE cluster_version (
   id BIGINT NOT NULL,
+  repo_version_id BIGINT NOT NULL,
   cluster_id BIGINT NOT NULL,
-  stack VARCHAR(255) NOT NULL,
   state VARCHAR(32) NOT NULL,
-  version VARCHAR(255) NOT NULL,
   start_time BIGINT NOT NULL,
   end_time BIGINT,
   user_name VARCHAR(32),
@@ -145,9 +144,8 @@ CREATE TABLE hoststate (
 
 CREATE TABLE host_version (
   id BIGINT NOT NULL,
+  repo_version_id BIGINT NOT NULL,
   host_name VARCHAR(255) NOT NULL,
-  stack VARCHAR(255) NOT NULL,
-  version VARCHAR(255) NOT NULL,
   state VARCHAR(32) NOT NULL,
   PRIMARY KEY (id));
 
@@ -538,12 +536,14 @@ ALTER TABLE clusterservices ADD CONSTRAINT FK_clusterservices_cluster_id FOREIGN
 ALTER TABLE clusterconfigmapping ADD CONSTRAINT clusterconfigmappingcluster_id FOREIGN KEY (cluster_id) REFERENCES clusters (cluster_id);
 ALTER TABLE clusterstate ADD CONSTRAINT FK_clusterstate_cluster_id FOREIGN KEY (cluster_id) REFERENCES clusters (cluster_id);
 ALTER TABLE cluster_version ADD CONSTRAINT FK_cluster_version_cluster_id FOREIGN KEY (cluster_id) REFERENCES clusters (cluster_id);
+ALTER TABLE cluster_version ADD CONSTRAINT FK_cluster_version_repovers_id FOREIGN KEY (repo_version_id) REFERENCES repo_version (repo_version_id);
 ALTER TABLE hostcomponentdesiredstate ADD CONSTRAINT hstcmponentdesiredstatehstname FOREIGN KEY (host_name) REFERENCES hosts (host_name);
 ALTER TABLE hostcomponentdesiredstate ADD CONSTRAINT hstcmpnntdesiredstatecmpnntnme FOREIGN KEY (component_name, cluster_id, service_name) REFERENCES servicecomponentdesiredstate (component_name, cluster_id, service_name);
 ALTER TABLE hostcomponentstate ADD CONSTRAINT hstcomponentstatecomponentname FOREIGN KEY (component_name, cluster_id, service_name) REFERENCES servicecomponentdesiredstate (component_name, cluster_id, service_name);
 ALTER TABLE hostcomponentstate ADD CONSTRAINT hostcomponentstate_host_name FOREIGN KEY (host_name) REFERENCES hosts (host_name);
 ALTER TABLE hoststate ADD CONSTRAINT FK_hoststate_host_name FOREIGN KEY (host_name) REFERENCES hosts (host_name);
 ALTER TABLE host_version ADD CONSTRAINT FK_host_version_host_name FOREIGN KEY (host_name) REFERENCES hosts (host_name);
+ALTER TABLE host_version ADD CONSTRAINT FK_host_version_repovers_id FOREIGN KEY (repo_version_id) REFERENCES repo_version (repo_version_id);
 ALTER TABLE servicecomponentdesiredstate ADD CONSTRAINT srvccmponentdesiredstatesrvcnm FOREIGN KEY (service_name, cluster_id) REFERENCES clusterservices (service_name, cluster_id);
 ALTER TABLE servicedesiredstate ADD CONSTRAINT servicedesiredstateservicename FOREIGN KEY (service_name, cluster_id) REFERENCES clusterservices (service_name, cluster_id);
 ALTER TABLE execution_command ADD CONSTRAINT FK_execution_command_task_id FOREIGN KEY (task_id) REFERENCES host_role_command (task_id);

http://git-wip-us.apache.org/repos/asf/ambari/blob/92815dd5/ambari-server/src/main/resources/Ambari-DDL-Oracle-CREATE.sql
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/resources/Ambari-DDL-Oracle-CREATE.sql b/ambari-server/src/main/resources/Ambari-DDL-Oracle-CREATE.sql
index 720e253..cb8f776 100644
--- a/ambari-server/src/main/resources/Ambari-DDL-Oracle-CREATE.sql
+++ b/ambari-server/src/main/resources/Ambari-DDL-Oracle-CREATE.sql
@@ -73,9 +73,8 @@ CREATE TABLE clusterstate (
 
 CREATE TABLE cluster_version (
   id BIGINT NUMBER(19) NULL,
+  repo_version_id NUMBER(19) NOT NULL,
   cluster_id NUMBER(19) NOT NULL,
-  stack VARCHAR2(255) NOT NULL,
-  version VARCHAR2(255) NOT NULL,
   state VARCHAR2(32) NOT NULL,
   start_time NUMBER(19) NOT NULL,
   end_time NUMBER(19),
@@ -136,9 +135,8 @@ CREATE TABLE hoststate (
 
 CREATE TABLE host_version (
   id NUMBER(19) NOT NULL,
+  repo_version_id NUMBER(19) NOT NULL,
   host_name VARCHAR2(255) NOT NULL,
-  stack VARCHAR2(255) NOT NULL,
-  version VARCHAR2(255) NOT NULL,
   state VARCHAR2(32) NOT NULL,
   PRIMARY KEY (id));
 
@@ -529,12 +527,14 @@ ALTER TABLE clusterservices ADD CONSTRAINT FK_clusterservices_cluster_id FOREIGN
 ALTER TABLE clusterconfigmapping ADD CONSTRAINT clusterconfigmappingcluster_id FOREIGN KEY (cluster_id) REFERENCES clusters (cluster_id);
 ALTER TABLE clusterstate ADD CONSTRAINT FK_clusterstate_cluster_id FOREIGN KEY (cluster_id) REFERENCES clusters (cluster_id);
 ALTER TABLE cluster_version ADD CONSTRAINT FK_cluster_version_cluster_id FOREIGN KEY (cluster_id) REFERENCES clusters (cluster_id);
+ALTER TABLE cluster_version ADD CONSTRAINT FK_cluster_version_repovers_id FOREIGN KEY (repo_version_id) REFERENCES repo_version (repo_version_id);
 ALTER TABLE hostcomponentdesiredstate ADD CONSTRAINT hstcmponentdesiredstatehstname FOREIGN KEY (host_name) REFERENCES hosts (host_name);
 ALTER TABLE hostcomponentdesiredstate ADD CONSTRAINT hstcmpnntdesiredstatecmpnntnme FOREIGN KEY (component_name, cluster_id, service_name) REFERENCES servicecomponentdesiredstate (component_name, cluster_id, service_name);
 ALTER TABLE hostcomponentstate ADD CONSTRAINT hstcomponentstatecomponentname FOREIGN KEY (component_name, cluster_id, service_name) REFERENCES servicecomponentdesiredstate (component_name, cluster_id, service_name);
 ALTER TABLE hostcomponentstate ADD CONSTRAINT hostcomponentstate_host_name FOREIGN KEY (host_name) REFERENCES hosts (host_name);
 ALTER TABLE hoststate ADD CONSTRAINT FK_hoststate_host_name FOREIGN KEY (host_name) REFERENCES hosts (host_name);
 ALTER TABLE host_version ADD CONSTRAINT FK_host_version_host_name FOREIGN KEY (host_name) REFERENCES hosts (host_name);
+ALTER TABLE host_version ADD CONSTRAINT FK_host_version_repovers_id FOREIGN KEY (repo_version_id) REFERENCES repo_version (repo_version_id);
 ALTER TABLE servicecomponentdesiredstate ADD CONSTRAINT srvccmponentdesiredstatesrvcnm FOREIGN KEY (service_name, cluster_id) REFERENCES clusterservices (service_name, cluster_id);
 ALTER TABLE servicedesiredstate ADD CONSTRAINT servicedesiredstateservicename FOREIGN KEY (service_name, cluster_id) REFERENCES clusterservices (service_name, cluster_id);
 ALTER TABLE execution_command ADD CONSTRAINT FK_execution_command_task_id FOREIGN KEY (task_id) REFERENCES host_role_command (task_id);

http://git-wip-us.apache.org/repos/asf/ambari/blob/92815dd5/ambari-server/src/main/resources/Ambari-DDL-Postgres-CREATE.sql
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/resources/Ambari-DDL-Postgres-CREATE.sql b/ambari-server/src/main/resources/Ambari-DDL-Postgres-CREATE.sql
index 08651eb..4599390 100644
--- a/ambari-server/src/main/resources/Ambari-DDL-Postgres-CREATE.sql
+++ b/ambari-server/src/main/resources/Ambari-DDL-Postgres-CREATE.sql
@@ -81,9 +81,8 @@ CREATE TABLE clusterstate (
 
 CREATE TABLE cluster_version (
   id BIGINT NOT NULL,
+  repo_version_id BIGINT NOT NULL,
   cluster_id BIGINT NOT NULL,
-  stack VARCHAR(255) NOT NULL,
-  version VARCHAR(255) NOT NULL,
   state VARCHAR(32) NOT NULL,
   start_time BIGINT NOT NULL,
   end_time BIGINT,
@@ -143,9 +142,8 @@ CREATE TABLE hoststate (
 
 CREATE TABLE host_version (
   id BIGINT NOT NULL,
+  repo_version_id BIGINT NOT NULL,
   host_name VARCHAR(255) NOT NULL,
-  stack VARCHAR(255) NOT NULL,
-  version VARCHAR(255) NOT NULL,
   state VARCHAR(32) NOT NULL,
   PRIMARY KEY (id));
 
@@ -526,12 +524,14 @@ ALTER TABLE clusterservices ADD CONSTRAINT FK_clusterservices_cluster_id FOREIGN
 ALTER TABLE clusterconfigmapping ADD CONSTRAINT clusterconfigmappingcluster_id FOREIGN KEY (cluster_id) REFERENCES clusters (cluster_id);
 ALTER TABLE clusterstate ADD CONSTRAINT FK_clusterstate_cluster_id FOREIGN KEY (cluster_id) REFERENCES clusters (cluster_id);
 ALTER TABLE cluster_version ADD CONSTRAINT FK_cluster_version_cluster_id FOREIGN KEY (cluster_id) REFERENCES clusters (cluster_id);
+ALTER TABLE cluster_version ADD CONSTRAINT FK_cluster_version_repovers_id FOREIGN KEY (repo_version_id) REFERENCES repo_version (repo_version_id);
 ALTER TABLE hostcomponentdesiredstate ADD CONSTRAINT hstcmponentdesiredstatehstname FOREIGN KEY (host_name) REFERENCES hosts (host_name);
 ALTER TABLE hostcomponentdesiredstate ADD CONSTRAINT hstcmpnntdesiredstatecmpnntnme FOREIGN KEY (component_name, cluster_id, service_name) REFERENCES servicecomponentdesiredstate (component_name, cluster_id, service_name);
 ALTER TABLE hostcomponentstate ADD CONSTRAINT hstcomponentstatecomponentname FOREIGN KEY (component_name, cluster_id, service_name) REFERENCES servicecomponentdesiredstate (component_name, cluster_id, service_name);
 ALTER TABLE hostcomponentstate ADD CONSTRAINT hostcomponentstate_host_name FOREIGN KEY (host_name) REFERENCES hosts (host_name);
 ALTER TABLE hoststate ADD CONSTRAINT FK_hoststate_host_name FOREIGN KEY (host_name) REFERENCES hosts (host_name);
 ALTER TABLE host_version ADD CONSTRAINT FK_host_version_host_name FOREIGN KEY (host_name) REFERENCES hosts (host_name);
+ALTER TABLE host_version ADD CONSTRAINT FK_host_version_repovers_id FOREIGN KEY (repo_version_id) REFERENCES repo_version (repo_version_id);
 ALTER TABLE servicecomponentdesiredstate ADD CONSTRAINT srvccmponentdesiredstatesrvcnm FOREIGN KEY (service_name, cluster_id) REFERENCES clusterservices (service_name, cluster_id);
 ALTER TABLE servicedesiredstate ADD CONSTRAINT servicedesiredstateservicename FOREIGN KEY (service_name, cluster_id) REFERENCES clusterservices (service_name, cluster_id);
 ALTER TABLE execution_command ADD CONSTRAINT FK_execution_command_task_id FOREIGN KEY (task_id) REFERENCES host_role_command (task_id);

http://git-wip-us.apache.org/repos/asf/ambari/blob/92815dd5/ambari-server/src/main/resources/Ambari-DDL-Postgres-EMBEDDED-CREATE.sql
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/resources/Ambari-DDL-Postgres-EMBEDDED-CREATE.sql b/ambari-server/src/main/resources/Ambari-DDL-Postgres-EMBEDDED-CREATE.sql
index a91bc6a..cc33ae6 100644
--- a/ambari-server/src/main/resources/Ambari-DDL-Postgres-EMBEDDED-CREATE.sql
+++ b/ambari-server/src/main/resources/Ambari-DDL-Postgres-EMBEDDED-CREATE.sql
@@ -101,9 +101,8 @@ GRANT ALL PRIVILEGES ON TABLE ambari.clusterstate TO :username;
 
 CREATE TABLE ambari.cluster_version (
   id BIGINT NOT NULL,
+  repo_version_id BIGINT NOT NULL,
   cluster_id BIGINT NOT NULL,
-  stack VARCHAR(255) NOT NULL,
-  version VARCHAR(255) NOT NULL,
   state VARCHAR(32) NOT NULL,
   start_time BIGINT NOT NULL,
   end_time BIGINT,
@@ -170,8 +169,7 @@ GRANT ALL PRIVILEGES ON TABLE ambari.hoststate TO :username;
 CREATE TABLE ambari.host_version (
   id BIGINT NOT NULL,
   host_name VARCHAR(255) NOT NULL,
-  stack VARCHAR(255) NOT NULL,
-  version VARCHAR(255) NOT NULL,
+  repo_version_id BIGINT NOT NULL,
   state VARCHAR(32) NOT NULL,
   PRIMARY KEY (id));
 GRANT ALL PRIVILEGES ON TABLE ambari.host_version TO :username;
@@ -598,12 +596,14 @@ ALTER TABLE ambari.clusterservices ADD CONSTRAINT FK_clusterservices_cluster_id
 ALTER TABLE ambari.clusterconfigmapping ADD CONSTRAINT clusterconfigmappingcluster_id FOREIGN KEY (cluster_id) REFERENCES ambari.clusters (cluster_id);
 ALTER TABLE ambari.clusterstate ADD CONSTRAINT FK_clusterstate_cluster_id FOREIGN KEY (cluster_id) REFERENCES ambari.clusters (cluster_id);
 ALTER TABLE ambari.cluster_version ADD CONSTRAINT FK_cluster_version_cluster_id FOREIGN KEY (cluster_id) REFERENCES ambari.clusters (cluster_id);
+ALTER TABLE ambari.cluster_version ADD CONSTRAINT FK_cluster_version_repovers_id FOREIGN KEY (repo_version_id) REFERENCES ambari.repo_version (repo_version_id);
 ALTER TABLE ambari.hostcomponentdesiredstate ADD CONSTRAINT hstcmponentdesiredstatehstname FOREIGN KEY (host_name) REFERENCES ambari.hosts (host_name);
 ALTER TABLE ambari.hostcomponentdesiredstate ADD CONSTRAINT hstcmpnntdesiredstatecmpnntnme FOREIGN KEY (component_name, cluster_id, service_name) REFERENCES ambari.servicecomponentdesiredstate (component_name, cluster_id, service_name);
 ALTER TABLE ambari.hostcomponentstate ADD CONSTRAINT hstcomponentstatecomponentname FOREIGN KEY (component_name, cluster_id, service_name) REFERENCES ambari.servicecomponentdesiredstate (component_name, cluster_id, service_name);
 ALTER TABLE ambari.hostcomponentstate ADD CONSTRAINT hostcomponentstate_host_name FOREIGN KEY (host_name) REFERENCES ambari.hosts (host_name);
 ALTER TABLE ambari.hoststate ADD CONSTRAINT FK_hoststate_host_name FOREIGN KEY (host_name) REFERENCES ambari.hosts (host_name);
 ALTER TABLE ambari.host_version ADD CONSTRAINT FK_host_version_host_name FOREIGN KEY (host_name) REFERENCES ambari.hosts (host_name);
+ALTER TABLE ambari.host_version ADD CONSTRAINT FK_host_version_repovers_id FOREIGN KEY (repo_version_id) REFERENCES ambari.repo_version (repo_version_id);
 ALTER TABLE ambari.servicecomponentdesiredstate ADD CONSTRAINT srvccmponentdesiredstatesrvcnm FOREIGN KEY (service_name, cluster_id) REFERENCES ambari.clusterservices (service_name, cluster_id);
 ALTER TABLE ambari.servicedesiredstate ADD CONSTRAINT servicedesiredstateservicename FOREIGN KEY (service_name, cluster_id) REFERENCES ambari.clusterservices (service_name, cluster_id);
 ALTER TABLE ambari.execution_command ADD CONSTRAINT FK_execution_command_task_id FOREIGN KEY (task_id) REFERENCES ambari.host_role_command (task_id);

http://git-wip-us.apache.org/repos/asf/ambari/blob/92815dd5/ambari-server/src/main/resources/Ambari-DDL-SQLServer-CREATE.sql
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/resources/Ambari-DDL-SQLServer-CREATE.sql b/ambari-server/src/main/resources/Ambari-DDL-SQLServer-CREATE.sql
index 968ebed..8836f04 100644
--- a/ambari-server/src/main/resources/Ambari-DDL-SQLServer-CREATE.sql
+++ b/ambari-server/src/main/resources/Ambari-DDL-SQLServer-CREATE.sql
@@ -39,7 +39,7 @@ CREATE TABLE serviceconfigmapping (service_config_id BIGINT NOT NULL, config_id
 CREATE TABLE clusterconfigmapping (cluster_id BIGINT NOT NULL, type_name VARCHAR(255) NOT NULL, version_tag VARCHAR(255) NOT NULL, create_timestamp BIGINT NOT NULL, selected INTEGER NOT NULL DEFAULT 0, user_name VARCHAR(255) NOT NULL DEFAULT '_db', PRIMARY KEY CLUSTERED (cluster_id, type_name, create_timestamp));
 CREATE TABLE clusterservices (service_name VARCHAR(255) NOT NULL, cluster_id BIGINT NOT NULL, service_enabled INTEGER NOT NULL, PRIMARY KEY CLUSTERED (service_name, cluster_id));
 CREATE TABLE clusterstate (cluster_id BIGINT NOT NULL, current_cluster_state VARCHAR(255) NOT NULL, current_stack_version VARCHAR(255) NOT NULL, PRIMARY KEY CLUSTERED (cluster_id));
-CREATE TABLE cluster_version (id BIGINT NOT NULL, cluster_id BIGINT NOT NULL, stack VARCHAR(255) NOT NULL, version VARCHAR(255) NOT NULL, state VARCHAR(255) NOT NULL, start_time BIGINT NOT NULL, end_time BIGINT, user_name VARCHAR(255), PRIMARY KEY (id));
+CREATE TABLE cluster_version (id BIGINT NOT NULL, cluster_id BIGINT NOT NULL, repo_version_id BIGINT NOT NULL, state VARCHAR(255) NOT NULL, start_time BIGINT NOT NULL, end_time BIGINT, user_name VARCHAR(255), PRIMARY KEY (id));
 CREATE TABLE hostcomponentdesiredstate (cluster_id BIGINT NOT NULL, component_name VARCHAR(255) NOT NULL, desired_stack_version VARCHAR(255) NOT NULL, desired_state VARCHAR(255) NOT NULL, host_name VARCHAR(255) NOT NULL, service_name VARCHAR(255) NOT NULL, admin_state VARCHAR(32), maintenance_state VARCHAR(32) NOT NULL, security_state VARCHAR(32) NOT NULL DEFAULT 'UNSECURED', restart_required BIT NOT NULL DEFAULT 0, PRIMARY KEY CLUSTERED (cluster_id, component_name, host_name, service_name));
 CREATE TABLE hostcomponentstate (cluster_id BIGINT NOT NULL, component_name VARCHAR(255) NOT NULL, current_stack_version VARCHAR(255) NOT NULL, current_state VARCHAR(255) NOT NULL, host_name VARCHAR(255) NOT NULL, service_name VARCHAR(255) NOT NULL, upgrade_state VARCHAR(32) NOT NULL DEFAULT 'NONE', security_state VARCHAR(32) NOT NULL DEFAULT 'UNSECURED', PRIMARY KEY CLUSTERED (cluster_id, component_name, host_name, service_name));
 CREATE TABLE hosts (host_name VARCHAR(255) NOT NULL, cpu_count INTEGER NOT NULL, ph_cpu_count INTEGER, cpu_info VARCHAR(255) NOT NULL, discovery_status VARCHAR(2000) NOT NULL, host_attributes VARCHAR(MAX) NOT NULL, ipv4 VARCHAR(255), ipv6 VARCHAR(255), public_host_name VARCHAR(255), last_registration_time BIGINT NOT NULL, os_arch VARCHAR(255) NOT NULL, os_info VARCHAR(1000) NOT NULL, os_type VARCHAR(255) NOT NULL, rack_info VARCHAR(255) NOT NULL, total_mem BIGINT NOT NULL, PRIMARY KEY CLUSTERED (host_name));
@@ -84,7 +84,7 @@ CREATE TABLE adminprincipaltype (principal_type_id INTEGER NOT NULL, principal_t
 CREATE TABLE adminprincipal (principal_id BIGINT NOT NULL, principal_type_id INTEGER NOT NULL, PRIMARY KEY(principal_id));
 CREATE TABLE adminpermission (permission_id BIGINT NOT NULL, permission_name VARCHAR(255) NOT NULL, resource_type_id INTEGER NOT NULL, PRIMARY KEY(permission_id));
 CREATE TABLE adminprivilege (privilege_id BIGINT, permission_id BIGINT NOT NULL, resource_id BIGINT NOT NULL, principal_id BIGINT NOT NULL, PRIMARY KEY(privilege_id));
-CREATE TABLE host_version (id BIGINT NOT NULL, host_name VARCHAR(255) NOT NULL, stack VARCHAR(255) NOT NULL, version VARCHAR(255) NOT NULL, state VARCHAR(32) NOT NULL, PRIMARY KEY (id));
+CREATE TABLE host_version (id BIGINT NOT NULL, repo_version_id BIGINT NOT NULL, host_name VARCHAR(255) NOT NULL, state VARCHAR(32) NOT NULL, PRIMARY KEY (id));
 CREATE TABLE repo_version (repo_version_id BIGINT NOT NULL, stack VARCHAR(255) NOT NULL, version VARCHAR(255) NOT NULL, display_name VARCHAR(128) NOT NULL, upgrade_package VARCHAR(255) NOT NULL, repositories VARCHAR(MAX) NOT NULL, PRIMARY KEY(repo_version_id));
 
 -- altering tables by creating unique constraints----------
@@ -109,6 +109,7 @@ ALTER TABLE clusterservices ADD CONSTRAINT FK_clusterservices_cluster_id FOREIGN
 ALTER TABLE clusterconfigmapping ADD CONSTRAINT clusterconfigmappingcluster_id FOREIGN KEY (cluster_id) REFERENCES clusters (cluster_id);
 ALTER TABLE clusterstate ADD CONSTRAINT FK_clusterstate_cluster_id FOREIGN KEY (cluster_id) REFERENCES clusters (cluster_id);
 ALTER TABLE cluster_version ADD CONSTRAINT FK_cluster_version_cluster_id FOREIGN KEY (cluster_id) REFERENCES clusters (cluster_id);
+ALTER TABLE cluster_version ADD CONSTRAINT FK_cluster_version_repovers_id FOREIGN KEY (repo_version_id) REFERENCES repo_version (repo_version_id);
 ALTER TABLE hostcomponentdesiredstate ADD CONSTRAINT hstcmponentdesiredstatehstname FOREIGN KEY (host_name) REFERENCES hosts (host_name);
 ALTER TABLE hostcomponentdesiredstate ADD CONSTRAINT hstcmpnntdesiredstatecmpnntnme FOREIGN KEY (component_name, cluster_id, service_name) REFERENCES servicecomponentdesiredstate (component_name, cluster_id, service_name);
 ALTER TABLE hostcomponentstate ADD CONSTRAINT hstcomponentstatecomponentname FOREIGN KEY (component_name, cluster_id, service_name) REFERENCES servicecomponentdesiredstate (component_name, cluster_id, service_name);
@@ -159,6 +160,7 @@ ALTER TABLE users ADD CONSTRAINT FK_users_principal_id FOREIGN KEY (principal_id
 ALTER TABLE groups ADD CONSTRAINT FK_groups_principal_id FOREIGN KEY (principal_id) REFERENCES adminprincipal(principal_id);
 ALTER TABLE clusters ADD CONSTRAINT FK_clusters_resource_id FOREIGN KEY (resource_id) REFERENCES adminresource(resource_id);
 ALTER TABLE host_version ADD CONSTRAINT FK_host_version_host_name FOREIGN KEY (host_name) REFERENCES hosts (host_name);
+ALTER TABLE host_version ADD CONSTRAINT FK_host_version_repovers_id FOREIGN KEY (repo_version_id) REFERENCES repo_version (repo_version_id);
 
 -- Alerting Framework
 CREATE TABLE alert_definition (

http://git-wip-us.apache.org/repos/asf/ambari/blob/92815dd5/ambari-server/src/test/java/org/apache/ambari/server/agent/TestHeartbeatHandler.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/agent/TestHeartbeatHandler.java b/ambari-server/src/test/java/org/apache/ambari/server/agent/TestHeartbeatHandler.java
index c6e5b26..a6df0db 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/agent/TestHeartbeatHandler.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/agent/TestHeartbeatHandler.java
@@ -78,6 +78,7 @@ import org.apache.ambari.server.controller.HostsMap;
 import org.apache.ambari.server.events.publishers.AmbariEventPublisher;
 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.state.Alert;
 import org.apache.ambari.server.state.Cluster;
 import org.apache.ambari.server.state.Clusters;
@@ -122,6 +123,8 @@ public class TestHeartbeatHandler {
   Configuration config;
   @Inject
   ActionDBAccessor actionDBAccessor;
+  @Inject
+  OrmTestHelper helper;
 
   private UnitOfWork unitOfWork;
 
@@ -2174,6 +2177,7 @@ public class TestHeartbeatHandler {
     StackId stackId = new StackId(DummyStackId);
     cluster.setDesiredStackVersion(stackId);
     cluster.setCurrentStackVersion(stackId);
+    helper.getOrCreateRepositoryVersion(stackId.getStackName(), stackId.getStackVersion());
     cluster.createClusterVersion(stackId.getStackName(), stackId.getStackVersion(), "admin", RepositoryVersionState.CURRENT);
     return cluster;
   }

http://git-wip-us.apache.org/repos/asf/ambari/blob/92815dd5/ambari-server/src/test/java/org/apache/ambari/server/agent/TestHeartbeatMonitor.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/agent/TestHeartbeatMonitor.java b/ambari-server/src/test/java/org/apache/ambari/server/agent/TestHeartbeatMonitor.java
index 26bf0b9..cf183ec 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/agent/TestHeartbeatMonitor.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/agent/TestHeartbeatMonitor.java
@@ -38,6 +38,7 @@ import org.apache.ambari.server.actionmanager.ActionManager;
 import org.apache.ambari.server.api.services.AmbariMetaInfo;
 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.state.Cluster;
 import org.apache.ambari.server.state.Clusters;
 import org.apache.ambari.server.state.RepositoryVersionState;
@@ -76,6 +77,7 @@ public class TestHeartbeatMonitor {
   private String serviceName = "HDFS";
   private int heartbeatMonitorWakeupIntervalMS = 30;
   private AmbariMetaInfo ambariMetaInfo;
+  private OrmTestHelper helper;
 
   private static final Logger LOG =
           LoggerFactory.getLogger(TestHeartbeatMonitor.class);
@@ -84,7 +86,7 @@ public class TestHeartbeatMonitor {
   public void setup() throws Exception {
     injector = Guice.createInjector(new InMemoryDefaultTestModule());
     injector.getInstance(GuiceJpaInitializer.class);
-    //injector.getInstance(OrmTestHelper.class).createDefaultData();
+    helper = injector.getInstance(OrmTestHelper.class);
     ambariMetaInfo = injector.getInstance(AmbariMetaInfo.class);
     ambariMetaInfo.init();
   }
@@ -116,6 +118,7 @@ public class TestHeartbeatMonitor {
     Cluster cluster = clusters.getCluster(clusterName);
     StackId stackId = new StackId("HDP-0.1");
     cluster.setDesiredStackVersion(stackId);
+    helper.getOrCreateRepositoryVersion(stackId.getStackName(), stackId.getStackVersion());
     cluster.createClusterVersion(stackId.getStackName(), stackId.getStackVersion(), "admin", RepositoryVersionState.CURRENT);
     Set<String> hostNames = new HashSet<String>(){{
       add(hostname1);
@@ -201,6 +204,7 @@ public class TestHeartbeatMonitor {
     Cluster cluster = clusters.getCluster(clusterName);
     StackId stackId = new StackId("HDP-0.1");
     cluster.setDesiredStackVersion(stackId);
+    helper.getOrCreateRepositoryVersion(stackId.getStackName(), stackId.getStackVersion());
     cluster.createClusterVersion(stackId.getStackName(), stackId.getStackVersion(), "admin", RepositoryVersionState.CURRENT);
     Set<String> hostNames = new HashSet<String>() {{
       add(hostname1);
@@ -306,6 +310,7 @@ public class TestHeartbeatMonitor {
     Cluster cluster = clusters.getCluster(clusterName);
     StackId stackId = new StackId("HDP-0.1");
     cluster.setDesiredStackVersion(stackId);
+    helper.getOrCreateRepositoryVersion(stackId.getStackName(), stackId.getStackVersion());
     cluster.createClusterVersion(stackId.getStackName(), stackId.getStackVersion(), "admin", RepositoryVersionState.CURRENT);
 
     Set<String> hostNames = new HashSet<String>(){{
@@ -416,6 +421,7 @@ public class TestHeartbeatMonitor {
     Cluster cluster = clusters.getCluster(clusterName);
     StackId stackId = new StackId("HDP-0.1");
     cluster.setDesiredStackVersion(stackId);
+    helper.getOrCreateRepositoryVersion(stackId.getStackName(), stackId.getStackVersion());
     cluster.createClusterVersion(stackId.getStackName(), stackId.getStackVersion(), "admin", RepositoryVersionState.CURRENT);
 
     Set<String> hostNames = new HashSet<String>(){{
@@ -534,6 +540,7 @@ public class TestHeartbeatMonitor {
     Cluster cluster = clusters.getCluster(clusterName);
     StackId stackId = new StackId("HDP-2.0.7");
     cluster.setDesiredStackVersion(stackId);
+    helper.getOrCreateRepositoryVersion(stackId.getStackName(), stackId.getStackVersion());
     cluster.createClusterVersion(stackId.getStackName(), stackId.getStackVersion(), "admin", RepositoryVersionState.CURRENT);
 
     Set<String> hostNames = new HashSet<String>(){{

http://git-wip-us.apache.org/repos/asf/ambari/blob/92815dd5/ambari-server/src/test/java/org/apache/ambari/server/api/services/StacksServiceTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/api/services/StacksServiceTest.java b/ambari-server/src/test/java/org/apache/ambari/server/api/services/StacksServiceTest.java
index 268c008..212eaa0 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/api/services/StacksServiceTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/api/services/StacksServiceTest.java
@@ -134,13 +134,6 @@ public class StacksServiceTest extends BaseServiceTest {
     }
 
     @Override
-    ResourceInstance createRepositoryResource(String stackName,
-        String stackVersion, String repositoryVersion, String osType,
-        String repoId) {
-      return getTestResource();
-    }
-
-    @Override
     ResourceInstance createStackServiceResource(String stackName,
         String stackVersion, String serviceName) {
       return getTestResource();
@@ -156,12 +149,6 @@ public class StacksServiceTest extends BaseServiceTest {
       return getTestResource();
     }
 
-    ResourceInstance createOperatingSystemResource(String stackName,
-        String stackVersion, String osType) {
-      return getTestResource();
-    }
-
-
     @Override
     RequestFactory getRequestFactory() {
       return getTestRequestFactory();

http://git-wip-us.apache.org/repos/asf/ambari/blob/92815dd5/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerTest.java b/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerTest.java
index a7b2034..9dd967a 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerTest.java
@@ -33,6 +33,7 @@ import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 import static org.hamcrest.CoreMatchers.*;
+
 import java.io.File;
 import java.io.IOException;
 import java.io.StringReader;
@@ -89,6 +90,7 @@ import org.apache.ambari.server.events.publishers.AmbariEventPublisher;
 import org.apache.ambari.server.metadata.ActionMetadata;
 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.ExecutionCommandDAO;
 import org.apache.ambari.server.orm.dao.HostDAO;
 import org.apache.ambari.server.orm.entities.ExecutionCommandEntity;
@@ -189,6 +191,7 @@ public class AmbariManagementControllerTest {
   private Configuration configuration;
   private ConfigHelper configHelper;
   private ConfigGroupFactory configGroupFactory;
+  private OrmTestHelper helper;
 
   @Rule
   public ExpectedException expectedException = ExpectedException.none();
@@ -214,6 +217,7 @@ public class AmbariManagementControllerTest {
     configuration = injector.getInstance(Configuration.class);
     configHelper = injector.getInstance(ConfigHelper.class);
     configGroupFactory = injector.getInstance(ConfigGroupFactory.class);
+    helper = injector.getInstance(OrmTestHelper.class);
   }
 
   @After
@@ -911,6 +915,7 @@ public class AmbariManagementControllerTest {
     Cluster c1 = clusters.getCluster("c1");
     StackId stackId = new StackId("HDP-0.1");
     c1.setDesiredStackVersion(stackId);
+    helper.getOrCreateRepositoryVersion(stackId.getStackName(), stackId.getStackVersion());
     c1.createClusterVersion(stackId.getStackName(), stackId.getStackVersion(), "admin", RepositoryVersionState.CURRENT);
     Service s1 = serviceFactory.createNew(c1, "HDFS");
     Service s2 = serviceFactory.createNew(c1, "MAPREDUCE");
@@ -993,6 +998,7 @@ public class AmbariManagementControllerTest {
     Cluster c1 = clusters.getCluster("c1");
     StackId stackId = new StackId("HDP-0.2");
     c1.setDesiredStackVersion(stackId);
+    helper.getOrCreateRepositoryVersion(stackId.getStackName(), stackId.getStackVersion());
     c1.createClusterVersion(stackId.getStackName(), stackId.getStackVersion(), "admin", RepositoryVersionState.CURRENT);
 
     Service s1 = serviceFactory.createNew(c1, "HDFS");
@@ -1260,6 +1266,8 @@ public class AmbariManagementControllerTest {
     Cluster c2 = clusters.getCluster("c2");
 
     StackId stackId = new StackId("HDP-0.2");
+    helper.getOrCreateRepositoryVersion(stackId.getStackName(), stackId.getStackVersion());
+
     foo.setDesiredStackVersion(stackId);
     foo.setCurrentStackVersion(stackId);
     foo.createClusterVersion(stackId.getStackName(), stackId.getStackVersion(), "admin", RepositoryVersionState.CURRENT);
@@ -1479,6 +1487,7 @@ public class AmbariManagementControllerTest {
     Cluster c = clusters.getCluster("foo");
     StackId stackId = new StackId("HDP-0.1");
     c.setDesiredStackVersion(stackId);
+    helper.getOrCreateRepositoryVersion(stackId.getStackName(), stackId.getStackVersion());
     c.createClusterVersion(stackId.getStackName(), stackId.getStackVersion(), "admin", RepositoryVersionState.CURRENT);
 
     HostResourceProviderTest.createHosts(controller, requests);
@@ -1500,6 +1509,7 @@ public class AmbariManagementControllerTest {
     Cluster c = clusters.getCluster("c1");
     StackId stackID = new StackId("HDP-0.1");
     c.setDesiredStackVersion(stackID);
+    helper.getOrCreateRepositoryVersion(stackID.getStackName(), stackID.getStackVersion());
     c.createClusterVersion(stackID.getStackName(), stackID.getStackVersion(), "admin", RepositoryVersionState.CURRENT);
 
     setOsFamily(clusters.getHost("h1"), "redhat", "5.9");
@@ -1845,6 +1855,7 @@ public class AmbariManagementControllerTest {
 
     StackId stackId = new StackId("HDP-0.1");
     c1.setDesiredStackVersion(stackId);
+    helper.getOrCreateRepositoryVersion(stackId.getStackName(), stackId.getStackVersion());
     c1.createClusterVersion(stackId.getStackName(), stackId.getStackVersion(), "admin", RepositoryVersionState.CURRENT);
 
     ClusterRequest r = new ClusterRequest(null, null, null, null);
@@ -7769,6 +7780,7 @@ public class AmbariManagementControllerTest {
     Cluster c = clusters.getCluster(clusterName);
     StackId stackID = new StackId("HDP-0.1");
     c.setDesiredStackVersion(stackID);
+    helper.getOrCreateRepositoryVersion(stackID.getStackName(), stackID.getStackVersion());
     c.createClusterVersion(stackID.getStackName(), stackID.getStackVersion(), "admin", RepositoryVersionState.CURRENT);
     clusters.addHost(hostName1);
     setOsFamily(clusters.getHost("h1"), "redhat", "5.9");

http://git-wip-us.apache.org/repos/asf/ambari/blob/92815dd5/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/StackDefinedPropertyProviderTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/StackDefinedPropertyProviderTest.java b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/StackDefinedPropertyProviderTest.java
index c31cc37..be40a68 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/StackDefinedPropertyProviderTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/StackDefinedPropertyProviderTest.java
@@ -20,6 +20,7 @@ package org.apache.ambari.server.controller.internal;
 import com.google.inject.Guice;
 import com.google.inject.Injector;
 import com.google.inject.persist.PersistService;
+
 import org.apache.ambari.server.controller.jmx.TestStreamProvider;
 import org.apache.ambari.server.controller.metrics.JMXPropertyProviderTest;
 import org.apache.ambari.server.controller.metrics.ganglia.GangliaPropertyProviderTest.TestGangliaHostProvider;
@@ -33,6 +34,7 @@ import org.apache.ambari.server.controller.utilities.PropertyHelper;
 import org.apache.ambari.server.controller.utilities.StreamProvider;
 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.state.Cluster;
 import org.apache.ambari.server.state.Clusters;
 import org.apache.ambari.server.state.Host;
@@ -64,9 +66,9 @@ public class StackDefinedPropertyProviderTest {
   private static final String HOST_COMPONENT_COMPONENT_NAME_PROPERTY_ID = "HostRoles/component_name";
   private static final String HOST_COMPONENT_STATE_PROPERTY_ID = "HostRoles/state";
 
-
   private Clusters clusters = null;
   private Injector injector = null;
+  private OrmTestHelper helper = null;
 
   @Before
   public void setup() throws Exception {
@@ -76,12 +78,15 @@ public class StackDefinedPropertyProviderTest {
     injector.getInstance(GuiceJpaInitializer.class);
     StackDefinedPropertyProvider.init(injector);
 
+    helper = injector.getInstance(OrmTestHelper.class);
+
     clusters = injector.getInstance(Clusters.class);
     clusters.addCluster("c1");
 
     Cluster cluster = clusters.getCluster("c1");
     StackId stackId = new StackId("HDP-2.0.5");
     cluster.setDesiredStackVersion(stackId);
+    helper.getOrCreateRepositoryVersion(stackId.getStackName(), stackId.getStackVersion());
     cluster.createClusterVersion(stackId.getStackName(), stackId.getStackVersion(), "admin", RepositoryVersionState.CURRENT);
 
     clusters.addHost("h1");
@@ -965,7 +970,7 @@ public class StackDefinedPropertyProviderTest {
       }
     }
 
-    // size + properties defined before "Object[][] testData ... " above 
+    // size + properties defined before "Object[][] testData ... " above
     Assert.assertEquals(properties.size() + 3, PropertyHelper.getProperties(resource).size());
 
     int i = 0;

http://git-wip-us.apache.org/repos/asf/ambari/blob/92815dd5/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 9b5a52a..791c2eb 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
@@ -43,6 +43,7 @@ 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.OrmTestHelper;
 import org.apache.ambari.server.orm.dao.RepositoryVersionDAO;
 import org.apache.ambari.server.orm.dao.UpgradeDAO;
 import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
@@ -75,7 +76,7 @@ public class UpgradeResourceProviderTest {
   private RepositoryVersionDAO repoVersionDao = null;
   private Injector injector;
   private Clusters clusters;
-//  private UpgradeResourceProvider upgradeResourceProvider;
+  private OrmTestHelper helper;
   AmbariManagementController amc;
 
   @Before
@@ -84,9 +85,9 @@ public class UpgradeResourceProviderTest {
     injector = Guice.createInjector(new InMemoryDefaultTestModule());
     injector.getInstance(GuiceJpaInitializer.class);
 
+    helper = injector.getInstance(OrmTestHelper.class);
 
     amc = injector.getInstance(AmbariManagementController.class);
-//    upgradeResourceProvider = createProvider(amc);
 
     Field field = AmbariServer.class.getDeclaredField("clusterController");
     field.setAccessible(true);
@@ -96,7 +97,6 @@ public class UpgradeResourceProviderTest {
     repoVersionDao = injector.getInstance(RepositoryVersionDAO.class);
 
     ViewRegistry.initInstance(new ViewRegistry());
-    System.out.println(AmbariServer.getController());
 
     RepositoryVersionEntity repoVersionEntity = new RepositoryVersionEntity();
     repoVersionEntity.setDisplayName("My New Version");
@@ -112,6 +112,7 @@ public class UpgradeResourceProviderTest {
     Cluster cluster = clusters.getCluster("c1");
     StackId stackId = new StackId("HDP-2.1.1");
     cluster.setDesiredStackVersion(stackId);
+    helper.getOrCreateRepositoryVersion(stackId.getStackName(), stackId.getStackVersion());
     cluster.createClusterVersion(stackId.getStackName(), stackId.getStackVersion(), "admin", RepositoryVersionState.CURRENT);
 
     clusters.addHost("h1");

http://git-wip-us.apache.org/repos/asf/ambari/blob/92815dd5/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 cc79f09..eabdc09 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
@@ -27,6 +27,7 @@ import junit.framework.Assert;
 import org.apache.ambari.server.events.publishers.AmbariEventPublisher;
 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.state.Cluster;
 import org.apache.ambari.server.state.Clusters;
 import org.apache.ambari.server.state.Host;
@@ -67,6 +68,7 @@ public class EventsTest {
   private ServiceComponentHostFactory m_schFactory;
   private AmbariEventPublisher m_eventPublisher;
   private MockEventListener m_listener;
+  private OrmTestHelper m_helper;
 
   /**
    *
@@ -79,6 +81,8 @@ public class EventsTest {
     m_eventPublisher = m_injector.getInstance(AmbariEventPublisher.class);
     EventBus synchronizedBus = new EventBus();
 
+    m_helper = m_injector.getInstance(OrmTestHelper.class);
+
     // register mock listener
     m_listener = m_injector.getInstance(MockEventListener.class);
     synchronizedBus.register(m_listener);
@@ -109,6 +113,7 @@ public class EventsTest {
     Assert.assertNotNull(m_cluster);
     StackId stackId = new StackId("HDP", "2.0.6");
     m_cluster.setDesiredStackVersion(stackId);
+    m_helper.getOrCreateRepositoryVersion(stackId.getStackName(), stackId.getStackVersion());
     m_cluster.createClusterVersion(stackId.getStackName(), stackId.getStackVersion(), "admin", RepositoryVersionState.CURRENT);
 
     m_clusters.mapHostToCluster(HOSTNAME, m_clusterName);

http://git-wip-us.apache.org/repos/asf/ambari/blob/92815dd5/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 fed9827..374a925 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
@@ -43,6 +43,7 @@ import org.apache.ambari.server.orm.dao.AlertsDAO;
 import org.apache.ambari.server.orm.dao.ClusterDAO;
 import org.apache.ambari.server.orm.dao.HostDAO;
 import org.apache.ambari.server.orm.dao.HostRoleCommandDAO;
+import org.apache.ambari.server.orm.dao.RepositoryVersionDAO;
 import org.apache.ambari.server.orm.dao.RequestDAO;
 import org.apache.ambari.server.orm.dao.ResourceTypeDAO;
 import org.apache.ambari.server.orm.dao.StageDAO;
@@ -57,6 +58,7 @@ import org.apache.ambari.server.orm.entities.HostRoleCommandEntity;
 import org.apache.ambari.server.orm.entities.HostStateEntity;
 import org.apache.ambari.server.orm.entities.PrincipalEntity;
 import org.apache.ambari.server.orm.entities.PrincipalTypeEntity;
+import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
 import org.apache.ambari.server.orm.entities.RequestEntity;
 import org.apache.ambari.server.orm.entities.ResourceEntity;
 import org.apache.ambari.server.orm.entities.ResourceTypeEntity;
@@ -106,6 +108,9 @@ public class OrmTestHelper {
   @Inject
   public AlertsDAO alertsDAO;
 
+  @Inject
+  public RepositoryVersionDAO repositoryVersionDAO;
+
   public EntityManager getEntityManager() {
     return entityManagerProvider.get();
   }
@@ -327,6 +332,7 @@ public class OrmTestHelper {
   public Cluster initializeClusterWithStack(Cluster cluster) throws Exception {
     StackId stackId = new StackId("HDP", "2.0.6");
     cluster.setDesiredStackVersion(stackId);
+    getOrCreateRepositoryVersion(stackId.getStackName(), stackId.getStackVersion());
     cluster.createClusterVersion(stackId.getStackName(),
         stackId.getStackVersion(), "admin", RepositoryVersionState.CURRENT);
     return cluster;
@@ -538,4 +544,22 @@ public class OrmTestHelper {
     return defaultGroups;
   }
 
+  /**
+   * Convenient method to create or to get repository version for given stack.
+   *
+   * @param stack stack name
+   * @param version stack version
+   * @return repository version
+   */
+  public RepositoryVersionEntity getOrCreateRepositoryVersion(String stack, String version) {
+    RepositoryVersionEntity repositoryVersion = repositoryVersionDAO.findByStackAndVersion(stack, version);
+    if (repositoryVersion == null) {
+      try {
+        repositoryVersion = repositoryVersionDAO.create(stack, version, String.valueOf(System.currentTimeMillis()), "pack", "");
+      } catch (Exception ex) {
+      }
+    }
+    return repositoryVersion;
+  }
+
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/92815dd5/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/ClusterVersionDAOTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/ClusterVersionDAOTest.java b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/ClusterVersionDAOTest.java
index 4b29635..4837764 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/ClusterVersionDAOTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/ClusterVersionDAOTest.java
@@ -76,7 +76,7 @@ public class ClusterVersionDAOTest {
       clusterId = helper.createCluster();
       cluster = clusterDAO.findById(clusterId);
 
-      cvA = new ClusterVersionEntity(cluster, "HDP", "2.2.0.0-995", RepositoryVersionState.CURRENT, System.currentTimeMillis(), System.currentTimeMillis(), "admin");
+      cvA = new ClusterVersionEntity(cluster, helper.getOrCreateRepositoryVersion("HDP-2.2", "2.2.0.0-995"), RepositoryVersionState.CURRENT, System.currentTimeMillis(), System.currentTimeMillis(), "admin");
       clusterVersionDAO.create(cvA);
       cvAId = cvA.getId();
     } else {
@@ -87,7 +87,7 @@ public class ClusterVersionDAOTest {
     // Install B
     if (currStep >= 2) {
       if (lastStep <= 1) {
-        cvB = new ClusterVersionEntity(cluster, "HDP", "2.2.0.1-998", RepositoryVersionState.INSTALLED, System.currentTimeMillis(), System.currentTimeMillis(), "admin");
+        cvB = new ClusterVersionEntity(cluster, helper.getOrCreateRepositoryVersion("HDP-2.2", "2.2.0.1-998"), RepositoryVersionState.INSTALLED, System.currentTimeMillis(), System.currentTimeMillis(), "admin");
         clusterVersionDAO.create(cvB);
         cvBId = cvB.getId();
       } else {
@@ -106,7 +106,7 @@ public class ClusterVersionDAOTest {
     // Start upgrading C
     if (currStep >= 4) {
       if (lastStep <= 3) {
-        cvC = new ClusterVersionEntity(cluster, "HDP", "2.2.1.0-100", RepositoryVersionState.UPGRADING, System.currentTimeMillis(), "admin");
+        cvC = new ClusterVersionEntity(cluster, helper.getOrCreateRepositoryVersion("HDP-2.2", "2.2.1.0-100"), RepositoryVersionState.UPGRADING, System.currentTimeMillis(), "admin");
         clusterVersionDAO.create(cvC);
         cvCId = cvC.getId();
       } else {
@@ -147,23 +147,23 @@ public class ClusterVersionDAOTest {
   public void testFindByStackAndVersion() {
     createRecordsUntilStep(1);
     Assert.assertEquals(0, clusterVersionDAO.findByStackAndVersion("non existing", "non existing").size());
-    Assert.assertEquals(1, clusterVersionDAO.findByStackAndVersion("HDP", "2.2.0.0-995").size());
+    Assert.assertEquals(1, clusterVersionDAO.findByStackAndVersion("HDP-2.2", "2.2.0.0-995").size());
   }
-  
+
   @Test
   public void testFindByCluster() {
     createRecordsUntilStep(1);
     Assert.assertEquals(0, clusterVersionDAO.findByCluster("non existing").size());
     Assert.assertEquals(1, clusterVersionDAO.findByCluster(cluster.getClusterName()).size());
   }
-  
+
   @Test
   public void testFindByClusterAndStackAndVersion() {
     createRecordsUntilStep(1);
     Assert.assertNull(clusterVersionDAO.findByClusterAndStackAndVersion(cluster.getClusterName(), "non existing", "non existing"));
-    Assert.assertNotNull(clusterVersionDAO.findByClusterAndStackAndVersion(cluster.getClusterName(), "HDP", "2.2.0.0-995"));
+    Assert.assertNotNull(clusterVersionDAO.findByClusterAndStackAndVersion(cluster.getClusterName(), "HDP-2.2", "2.2.0.0-995"));
   }
-  
+
   /**
    * At all times the cluster should have a cluster version whose state is {@link org.apache.ambari.server.state.RepositoryVersionState#CURRENT}
    */