Repository: ambari Updated Branches: refs/heads/branch-2.5 44e64e5f6 -> 4e9ed5c02
AMBARI-18255. Add unique constraint to host_version table (ncole) Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/4e9ed5c0 Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/4e9ed5c0 Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/4e9ed5c0 Branch: refs/heads/branch-2.5 Commit: 4e9ed5c0264b7283296fc7f66cb1b418e43977c5 Parents: 44e64e5 Author: Nate Cole <[email protected]> Authored: Wed Aug 24 14:26:35 2016 -0400 Committer: Nate Cole <[email protected]> Committed: Mon Sep 12 14:35:48 2016 -0400 ---------------------------------------------------------------------- .../upgrade/HostVersionOutOfSyncListener.java | 4 + .../server/orm/entities/HostVersionEntity.java | 3 +- .../server/state/cluster/ClusterImpl.java | 13 +++ .../server/upgrade/SchemaUpgradeHelper.java | 1 + .../server/upgrade/UpgradeCatalog250.java | 111 +++++++++++++++++++ .../main/resources/Ambari-DDL-Derby-CREATE.sql | 3 +- .../main/resources/Ambari-DDL-MySQL-CREATE.sql | 3 +- .../main/resources/Ambari-DDL-Oracle-CREATE.sql | 3 +- .../resources/Ambari-DDL-Postgres-CREATE.sql | 3 +- .../Ambari-DDL-Postgres-EMBEDDED-CREATE.sql | 3 +- .../resources/Ambari-DDL-SQLAnywhere-CREATE.sql | 3 +- .../resources/Ambari-DDL-SQLServer-CREATE.sql | 3 +- .../server/orm/dao/HostVersionDAOTest.java | 32 +++++- .../server/upgrade/UpgradeCatalog250Test.java | 89 +++++++++++++++ 14 files changed, 262 insertions(+), 12 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/4e9ed5c0/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/upgrade/HostVersionOutOfSyncListener.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/upgrade/HostVersionOutOfSyncListener.java b/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/upgrade/HostVersionOutOfSyncListener.java index f7644d3..1b298f6 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/upgrade/HostVersionOutOfSyncListener.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/upgrade/HostVersionOutOfSyncListener.java @@ -203,6 +203,10 @@ public class HostVersionOutOfSyncListener { RepositoryVersionEntity repositoryVersion = clusterVersion.getRepositoryVersion(); HostVersionEntity missingHostVersion = new HostVersionEntity(hostEntity, repositoryVersion, RepositoryVersionState.OUT_OF_SYNC); + + LOG.info("Creating host version for {}, state={}, repo={} (repo_id={})", + missingHostVersion.getHostName(), missingHostVersion.getState(), + missingHostVersion.getRepositoryVersion().getVersion(), missingHostVersion.getRepositoryVersion().getId()); hostVersionDAO.get().create(missingHostVersion); cluster.recalculateClusterVersionState(repositoryVersion); } http://git-wip-us.apache.org/repos/asf/ambari/blob/4e9ed5c0/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 6be4b50..4ed9617 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 @@ -31,10 +31,11 @@ import javax.persistence.NamedQueries; import javax.persistence.NamedQuery; import javax.persistence.Table; import javax.persistence.TableGenerator; +import javax.persistence.UniqueConstraint; import org.apache.ambari.server.state.RepositoryVersionState; -@Table(name = "host_version") +@Table(name = "host_version", uniqueConstraints = @UniqueConstraint(name = "UQ_host_repo", columnNames = { "repo_version_id", "host_id" })) @Entity @TableGenerator(name = "host_version_id_generator", table = "ambari_sequences", pkColumnName = "sequence_name", valueColumnName = "sequence_value" http://git-wip-us.apache.org/repos/asf/ambari/blob/4e9ed5c0/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 e1b9368..2f7d6b9 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 @@ -1480,6 +1480,10 @@ public class ClusterImpl implements Cluster { HostVersionEntity hostVersionEntity = new HostVersionEntity(hostEntity, sourceClusterVersion.getRepositoryVersion(), repositoryVersionState); + LOG.info("Creating host version for {}, state={}, repo={} (repo_id={})", + hostVersionEntity.getHostName(), hostVersionEntity.getState(), + hostVersionEntity.getRepositoryVersion().getVersion(), hostVersionEntity.getRepositoryVersion().getId()); + hostVersionDAO.create(hostVersionEntity); } else { // Update existing host stack version @@ -1731,6 +1735,11 @@ public class ClusterImpl implements Cluster { performingInitialBootstrap = true; } hostVersionEntity = new HostVersionEntity(host, repositoryVersion, RepositoryVersionState.INSTALLING); + + LOG.info("Creating host version for {}, state={}, repo={} (repo_id={})", + hostVersionEntity.getHostName(), hostVersionEntity.getState(), + hostVersionEntity.getRepositoryVersion().getVersion(), hostVersionEntity.getRepositoryVersion().getId()); + hostVersionDAO.create(hostVersionEntity); } @@ -1954,6 +1963,10 @@ public class ClusterImpl implements Cluster { HostVersionEntity hve = new HostVersionEntity(hostEntity, existingClusterVersion.getRepositoryVersion(), state); + LOG.info("Creating host version for {}, state={}, repo={} (repo_id={})", + hve.getHostName(), hve.getState(), + hve.getRepositoryVersion().getVersion(), hve.getRepositoryVersion().getId()); + hostVersionDAO.create(hve); } } http://git-wip-us.apache.org/repos/asf/ambari/blob/4e9ed5c0/ambari-server/src/main/java/org/apache/ambari/server/upgrade/SchemaUpgradeHelper.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/SchemaUpgradeHelper.java b/ambari-server/src/main/java/org/apache/ambari/server/upgrade/SchemaUpgradeHelper.java index ebfb267..1f4470b 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/SchemaUpgradeHelper.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/upgrade/SchemaUpgradeHelper.java @@ -189,6 +189,7 @@ public class SchemaUpgradeHelper { catalogBinder.addBinding().to(UpgradeCatalog222.class); catalogBinder.addBinding().to(UpgradeCatalog230.class); catalogBinder.addBinding().to(UpgradeCatalog240.class); + catalogBinder.addBinding().to(UpgradeCatalog250.class); catalogBinder.addBinding().to(FinalUpgradeCatalog.class); EventBusSynchronizer.synchronizeAmbariEventPublisher(binder()); http://git-wip-us.apache.org/repos/asf/ambari/blob/4e9ed5c0/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog250.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog250.java b/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog250.java new file mode 100644 index 0000000..43f489b --- /dev/null +++ b/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog250.java @@ -0,0 +1,111 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.ambari.server.upgrade; + +import java.sql.SQLException; + +import org.apache.ambari.server.AmbariException; +import org.apache.ambari.server.orm.dao.DaoUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.inject.Inject; +import com.google.inject.Injector; + +/** + * Upgrade catalog for version 2.5.0. + */ +public class UpgradeCatalog250 extends AbstractUpgradeCatalog { + + protected static final String HOST_VERSION_TABLE = "host_version"; + + /** + * Logger. + */ + private static final Logger LOG = LoggerFactory.getLogger(UpgradeCatalog250.class); + + @Inject + DaoUtils daoUtils; + + // ----- Constructors ------------------------------------------------------ + + /** + * Don't forget to register new UpgradeCatalogs in {@link org.apache.ambari.server.upgrade.SchemaUpgradeHelper.UpgradeHelperModule#configure()} + * + * @param injector Guice injector to track dependencies and uses bindings to inject them. + */ + @Inject + public UpgradeCatalog250(Injector injector) { + super(injector); + + daoUtils = injector.getInstance(DaoUtils.class); + } + + // ----- UpgradeCatalog ---------------------------------------------------- + + /** + * {@inheritDoc} + */ + @Override + public String getTargetVersion() { + return "2.5.0"; + } + + // ----- AbstractUpgradeCatalog -------------------------------------------- + + /** + * {@inheritDoc} + */ + @Override + public String getSourceVersion() { + return "2.4.0"; + } + + /** + * {@inheritDoc} + */ + @Override + protected void executeDDLUpdates() throws AmbariException, SQLException { + updateHostVersionTable(); + } + + /** + * {@inheritDoc} + */ + @Override + protected void executePreDMLUpdates() throws AmbariException, SQLException { + + } + + /** + * {@inheritDoc} + */ + @Override + protected void executeDMLUpdates() throws AmbariException, SQLException { + } + + protected void updateHostVersionTable() throws SQLException { + LOG.info("Updating the {} table", HOST_VERSION_TABLE); + + // Add the unique constraint to the host_version table + dbAccessor.addUniqueConstraint(HOST_VERSION_TABLE, "UQ_host_repo", "repo_version_id", "host_id"); + } + + +} + http://git-wip-us.apache.org/repos/asf/ambari/blob/4e9ed5c0/ambari-server/src/main/resources/Ambari-DDL-Derby-CREATE.sql ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/resources/Ambari-DDL-Derby-CREATE.sql b/ambari-server/src/main/resources/Ambari-DDL-Derby-CREATE.sql index c20cd1b..38f78c5 100644 --- a/ambari-server/src/main/resources/Ambari-DDL-Derby-CREATE.sql +++ b/ambari-server/src/main/resources/Ambari-DDL-Derby-CREATE.sql @@ -248,7 +248,8 @@ CREATE TABLE host_version ( state VARCHAR(32) NOT NULL, CONSTRAINT PK_host_version PRIMARY KEY (id), CONSTRAINT FK_host_version_host_id FOREIGN KEY (host_id) REFERENCES hosts (host_id), - CONSTRAINT FK_host_version_repovers_id FOREIGN KEY (repo_version_id) REFERENCES repo_version (repo_version_id)); + CONSTRAINT FK_host_version_repovers_id FOREIGN KEY (repo_version_id) REFERENCES repo_version (repo_version_id), + CONSTRAINT UQ_host_repo UNIQUE(repo_version_id, host_id)); CREATE TABLE servicedesiredstate ( cluster_id BIGINT NOT NULL, http://git-wip-us.apache.org/repos/asf/ambari/blob/4e9ed5c0/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 f30b2eb..25948aa 100644 --- a/ambari-server/src/main/resources/Ambari-DDL-MySQL-CREATE.sql +++ b/ambari-server/src/main/resources/Ambari-DDL-MySQL-CREATE.sql @@ -248,7 +248,8 @@ CREATE TABLE host_version ( state VARCHAR(32) NOT NULL, CONSTRAINT PK_host_version PRIMARY KEY (id), CONSTRAINT FK_host_version_host_id FOREIGN KEY (host_id) REFERENCES hosts (host_id), - CONSTRAINT FK_host_version_repovers_id FOREIGN KEY (repo_version_id) REFERENCES repo_version (repo_version_id)); + CONSTRAINT FK_host_version_repovers_id FOREIGN KEY (repo_version_id) REFERENCES repo_version (repo_version_id), + CONSTRAINT UQ_host_repo UNIQUE(repo_version_id, host_id)); CREATE TABLE servicedesiredstate ( cluster_id BIGINT NOT NULL, http://git-wip-us.apache.org/repos/asf/ambari/blob/4e9ed5c0/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 50feb87..07cd6a8 100644 --- a/ambari-server/src/main/resources/Ambari-DDL-Oracle-CREATE.sql +++ b/ambari-server/src/main/resources/Ambari-DDL-Oracle-CREATE.sql @@ -239,7 +239,8 @@ CREATE TABLE host_version ( state VARCHAR2(32) NOT NULL, CONSTRAINT PK_host_version PRIMARY KEY (id), CONSTRAINT FK_host_version_host_id FOREIGN KEY (host_id) REFERENCES hosts (host_id), - CONSTRAINT FK_host_version_repovers_id FOREIGN KEY (repo_version_id) REFERENCES repo_version (repo_version_id)); + CONSTRAINT FK_host_version_repovers_id FOREIGN KEY (repo_version_id) REFERENCES repo_version (repo_version_id), + CONSTRAINT UQ_host_repo UNIQUE(repo_version_id, host_id)); CREATE TABLE servicedesiredstate ( cluster_id NUMBER(19) NOT NULL, http://git-wip-us.apache.org/repos/asf/ambari/blob/4e9ed5c0/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 0843616..f03767b 100644 --- a/ambari-server/src/main/resources/Ambari-DDL-Postgres-CREATE.sql +++ b/ambari-server/src/main/resources/Ambari-DDL-Postgres-CREATE.sql @@ -248,7 +248,8 @@ CREATE TABLE host_version ( state VARCHAR(32) NOT NULL, CONSTRAINT PK_host_version PRIMARY KEY (id), CONSTRAINT FK_host_version_host_id FOREIGN KEY (host_id) REFERENCES hosts (host_id), - CONSTRAINT FK_host_version_repovers_id FOREIGN KEY (repo_version_id) REFERENCES repo_version (repo_version_id)); + CONSTRAINT FK_host_version_repovers_id FOREIGN KEY (repo_version_id) REFERENCES repo_version (repo_version_id), + CONSTRAINT UQ_host_repo UNIQUE(repo_version_id, host_id)); CREATE TABLE servicedesiredstate ( cluster_id BIGINT NOT NULL, http://git-wip-us.apache.org/repos/asf/ambari/blob/4e9ed5c0/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 43bdef9..8f03989 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 @@ -296,7 +296,8 @@ CREATE TABLE ambari.host_version ( state VARCHAR(32) NOT NULL, CONSTRAINT PK_host_version PRIMARY KEY (id), CONSTRAINT FK_host_version_host_id FOREIGN KEY (host_id) REFERENCES ambari.hosts (host_id), - CONSTRAINT FK_host_version_repovers_id FOREIGN KEY (repo_version_id) REFERENCES ambari.repo_version (repo_version_id) + CONSTRAINT FK_host_version_repovers_id FOREIGN KEY (repo_version_id) REFERENCES ambari.repo_version (repo_version_id), + CONSTRAINT UQ_host_repo UNIQUE(repo_version_id, host_id) ); GRANT ALL PRIVILEGES ON TABLE ambari.host_version TO :username; http://git-wip-us.apache.org/repos/asf/ambari/blob/4e9ed5c0/ambari-server/src/main/resources/Ambari-DDL-SQLAnywhere-CREATE.sql ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/resources/Ambari-DDL-SQLAnywhere-CREATE.sql b/ambari-server/src/main/resources/Ambari-DDL-SQLAnywhere-CREATE.sql index 1ef4ae1..535d847 100644 --- a/ambari-server/src/main/resources/Ambari-DDL-SQLAnywhere-CREATE.sql +++ b/ambari-server/src/main/resources/Ambari-DDL-SQLAnywhere-CREATE.sql @@ -237,7 +237,8 @@ CREATE TABLE host_version ( state VARCHAR(32) NOT NULL, CONSTRAINT PK_host_version PRIMARY KEY (id), CONSTRAINT FK_host_version_host_id FOREIGN KEY (host_id) REFERENCES hosts (host_id), - CONSTRAINT FK_host_version_repovers_id FOREIGN KEY (repo_version_id) REFERENCES repo_version (repo_version_id)); + CONSTRAINT FK_host_version_repovers_id FOREIGN KEY (repo_version_id) REFERENCES repo_version (repo_version_id), + CONSTRAINT UQ_host_repo UNIQUE(repo_version_id, host_id)); CREATE TABLE servicedesiredstate ( cluster_id NUMERIC(19) NOT NULL, http://git-wip-us.apache.org/repos/asf/ambari/blob/4e9ed5c0/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 babcb6d..1bfde7a 100644 --- a/ambari-server/src/main/resources/Ambari-DDL-SQLServer-CREATE.sql +++ b/ambari-server/src/main/resources/Ambari-DDL-SQLServer-CREATE.sql @@ -694,7 +694,8 @@ CREATE TABLE host_version ( STATE VARCHAR(32) NOT NULL, CONSTRAINT PK_host_version PRIMARY KEY CLUSTERED (id), CONSTRAINT FK_host_version_host_id FOREIGN KEY (host_id) REFERENCES hosts (host_id), - CONSTRAINT FK_host_version_repovers_id FOREIGN KEY (repo_version_id) REFERENCES repo_version (repo_version_id)); + CONSTRAINT FK_host_version_repovers_id FOREIGN KEY (repo_version_id) REFERENCES repo_version (repo_version_id), + CONSTRAINT UQ_host_repo UNIQUE(repo_version_id, host_id)); CREATE TABLE artifact ( artifact_name VARCHAR(255) NOT NULL, http://git-wip-us.apache.org/repos/asf/ambari/blob/4e9ed5c0/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/HostVersionDAOTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/HostVersionDAOTest.java b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/HostVersionDAOTest.java index d8e1576..e2f01a4 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/HostVersionDAOTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/HostVersionDAOTest.java @@ -26,7 +26,14 @@ 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.orm.entities.*; +import org.apache.ambari.server.orm.entities.ClusterEntity; +import org.apache.ambari.server.orm.entities.ClusterVersionEntity; +import org.apache.ambari.server.orm.entities.HostEntity; +import org.apache.ambari.server.orm.entities.HostVersionEntity; +import org.apache.ambari.server.orm.entities.RepositoryVersionEntity; +import org.apache.ambari.server.orm.entities.ResourceEntity; +import org.apache.ambari.server.orm.entities.ResourceTypeEntity; +import org.apache.ambari.server.orm.entities.StackEntity; import org.apache.ambari.server.security.authorization.ResourceType; import org.apache.ambari.server.state.RepositoryVersionState; import org.apache.ambari.server.state.StackId; @@ -44,7 +51,7 @@ import com.google.inject.persist.PersistService; * {@link org.apache.ambari.server.orm.dao.HostVersionDAO} unit tests. */ public class HostVersionDAOTest { - + private static Injector injector; private ResourceTypeDAO resourceTypeDAO; private ClusterDAO clusterDAO; @@ -133,7 +140,7 @@ public class HostVersionDAOTest { hostEntities.add(host1); hostEntities.add(host2); hostEntities.add(host3); - + // Both sides of relation should be set when modifying in runtime host1.setClusterEntities(Arrays.asList(clusterEntity)); host2.setClusterEntities(Arrays.asList(clusterEntity)); @@ -145,7 +152,7 @@ public class HostVersionDAOTest { clusterEntity.setHostEntities(hostEntities); clusterDAO.merge(clusterEntity); - + // Create the Host Versions HostVersionEntity hostVersionEntity1 = new HostVersionEntity(host1, clusterVersionEntity.getRepositoryVersion(), RepositoryVersionState.CURRENT); HostVersionEntity hostVersionEntity2 = new HostVersionEntity(host2, clusterVersionEntity.getRepositoryVersion(), RepositoryVersionState.INSTALLED); @@ -334,6 +341,23 @@ public class HostVersionDAOTest { Assert.assertEquals(hostVersionEntity3LastExpected, new HostVersionEntity(hostVersionEntity3LastActual)); } + @Test + public void testDuplicates() throws Exception { + HostEntity host1 = hostDAO.findByName("test_host1"); + + RepositoryVersionEntity repoVersion = helper.getOrCreateRepositoryVersion(HDP_22_STACK, repoVersion_2200); + + HostVersionEntity hostVersionEntity1 = new HostVersionEntity(host1, repoVersion, RepositoryVersionState.CURRENT); + + try { + hostVersionDAO.create(hostVersionEntity1); + Assert.fail("Each host can have a relationship to a repo version, but cannot have more than one for the same repo"); + } catch (Exception e) { + // expected + } + + } + @After public void after() { injector.getInstance(PersistService.class).stop(); http://git-wip-us.apache.org/repos/asf/ambari/blob/4e9ed5c0/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog250Test.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog250Test.java b/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog250Test.java new file mode 100644 index 0000000..6dadb22 --- /dev/null +++ b/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog250Test.java @@ -0,0 +1,89 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ambari.server.upgrade; + +import static org.easymock.EasyMock.createNiceMock; +import static org.easymock.EasyMock.createStrictMock; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.expectLastCall; +import static org.easymock.EasyMock.replay; +import static org.easymock.EasyMock.reset; +import static org.easymock.EasyMock.verify; + +import javax.persistence.EntityManager; + +import org.apache.ambari.server.orm.DBAccessor; +import org.apache.ambari.server.state.stack.OsFamily; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.google.inject.Binder; +import com.google.inject.Guice; +import com.google.inject.Injector; +import com.google.inject.Module; +import com.google.inject.Provider; + +/** + * {@link UpgradeCatalog250} unit tests. + */ +public class UpgradeCatalog250Test { + +// private Injector injector; + private Provider<EntityManager> entityManagerProvider = createStrictMock(Provider.class); + private EntityManager entityManager = createNiceMock(EntityManager.class); + + @Before + public void init() { + reset(entityManagerProvider); + expect(entityManagerProvider.get()).andReturn(entityManager).anyTimes(); + replay(entityManagerProvider); + + } + + @After + public void tearDown() { + } + + @Test + public void testExecuteDDLUpdates() throws Exception { + final DBAccessor dbAccessor = createNiceMock(DBAccessor.class); + + dbAccessor.addUniqueConstraint("host_version", "UQ_host_repo", "repo_version_id", "host_id"); + expectLastCall().once(); + + replay(dbAccessor); + + Module module = new Module() { + @Override + public void configure(Binder binder) { + binder.bind(DBAccessor.class).toInstance(dbAccessor); + binder.bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class)); + binder.bind(EntityManager.class).toInstance(entityManager); + } + }; + + Injector injector = Guice.createInjector(module); + UpgradeCatalog250 upgradeCatalog250 = injector.getInstance(UpgradeCatalog250.class); + upgradeCatalog250.executeDDLUpdates(); + + verify(dbAccessor); + } + +}
