AMBARI-16751: Refresh stale host entity reference when deleting a service
Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/db7f6cb4 Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/db7f6cb4 Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/db7f6cb4 Branch: refs/heads/branch-2.4 Commit: db7f6cb4527e3651545a8be1144465600f13868b Parents: 4bd3951 Author: Nahappan Somasundaram <nsomasunda...@hortonworks.com> Authored: Thu May 19 07:24:46 2016 -0700 Committer: Nahappan Somasundaram <nsomasunda...@hortonworks.com> Committed: Tue May 24 11:36:41 2016 -0700 ---------------------------------------------------------------------- .../ambari/server/orm/dao/HostComponentDesiredStateDAO.java | 7 ++++++- .../server/orm/dao/HostComponentDesiredStateDAOTest.java | 4 +++- 2 files changed, 9 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/db7f6cb4/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/HostComponentDesiredStateDAO.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/HostComponentDesiredStateDAO.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/HostComponentDesiredStateDAO.java index 46da9da..176e15b 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/HostComponentDesiredStateDAO.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/HostComponentDesiredStateDAO.java @@ -95,7 +95,12 @@ public class HostComponentDesiredStateDAO { @Transactional public void remove(HostComponentDesiredStateEntity hostComponentDesiredStateEntity) { - HostEntity hostEntity = hostComponentDesiredStateEntity.getHostEntity(); + HostEntity hostEntity = hostDAO.findById(hostComponentDesiredStateEntity.getHostId()); + + if (hostEntity == null) { + throw new IllegalStateException(String.format("Missing hostEntity for host id %1d", + hostComponentDesiredStateEntity.getHostId())); + } entityManagerProvider.get().remove(merge(hostComponentDesiredStateEntity)); http://git-wip-us.apache.org/repos/asf/ambari/blob/db7f6cb4/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/HostComponentDesiredStateDAOTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/HostComponentDesiredStateDAOTest.java b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/HostComponentDesiredStateDAOTest.java index b7a0677..28adb8c 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/HostComponentDesiredStateDAOTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/HostComponentDesiredStateDAOTest.java @@ -48,7 +48,7 @@ public class HostComponentDesiredStateDAOTest { HostEntity hostEntity = createNiceMock(HostEntity.class); HostComponentDesiredStateEntity hostComponentDesiredStateEntity = createNiceMock(HostComponentDesiredStateEntity.class); - expect(hostComponentDesiredStateEntity.getHostEntity()).andReturn(hostEntity); + expect(hostComponentDesiredStateEntity.getHostId()).andReturn(1L).anyTimes(); expect(entityManagerProvider.get()).andReturn(entityManager).anyTimes(); expect(entityManager.merge(hostComponentDesiredStateEntity)).andReturn(hostComponentDesiredStateEntity).anyTimes(); @@ -58,6 +58,8 @@ public class HostComponentDesiredStateDAOTest { expect(hostDAO.merge(hostEntity)).andReturn(hostEntity).anyTimes(); + expect(hostDAO.findById(1L)).andReturn(hostEntity).anyTimes(); + replay(entityManagerProvider, entityManager, hostDAO, hostEntity, hostComponentDesiredStateEntity); HostComponentDesiredStateDAO dao = new HostComponentDesiredStateDAO();