Repository: ambari Updated Branches: refs/heads/trunk 12df0682d -> c6f068c85
AMBARI-7822. Events with Service Checks results in Exceptions in log. Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/c6f068c8 Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/c6f068c8 Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/c6f068c8 Branch: refs/heads/trunk Commit: c6f068c8578db7b7030adeea755533bdd6aa1665 Parents: 12df068 Author: Siddharth Wagle <swa...@hortonworks.com> Authored: Tue Oct 21 11:03:34 2014 -0700 Committer: Siddharth Wagle <swa...@hortonworks.com> Committed: Tue Oct 21 11:03:34 2014 -0700 ---------------------------------------------------------------------- .../AmbariManagementControllerImpl.java | 43 +-- .../AmbariManagementControllerImplTest.java | 344 +++++++++++++------ 2 files changed, 256 insertions(+), 131 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/c6f068c8/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementControllerImpl.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementControllerImpl.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementControllerImpl.java index d12b6fd..a409078 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementControllerImpl.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementControllerImpl.java @@ -1979,9 +1979,10 @@ public class AmbariManagementControllerImpl implements AmbariManagementControlle for (String serviceName : smokeTestServices) { // Creates smoke test commands Service s = cluster.getService(serviceName); // find service component host - String clientHost = getClientHostForRunningAction(cluster, s); - String smokeTestRole = - actionMetadata.getServiceCheckAction(serviceName); + ServiceComponent component = getClientComponentForRunningAction(cluster, s); + String componentName = component != null ? component.getName() : null; + String clientHost = getClientHostForRunningAction(cluster, s, component); + String smokeTestRole = actionMetadata.getServiceCheckAction(serviceName); if (clientHost == null || smokeTestRole == null) { LOG.info("Nothing to do for service check as could not find role or" @@ -1995,7 +1996,7 @@ public class AmbariManagementControllerImpl implements AmbariManagementControlle customCommandExecutionHelper.addServiceCheckAction(stage, clientHost, smokeTestRole, nowTimestamp, serviceName, - null, null); + componentName, null); } RoleCommandOrder rco = getRoleCommandOrder(cluster); @@ -2880,7 +2881,17 @@ public class AmbariManagementControllerImpl implements AmbariManagementControlle } } - private String getClientHostForRunningAction(Cluster cluster, + protected String getClientHostForRunningAction(Cluster cluster, Service service, ServiceComponent serviceComponent) + throws AmbariException { + if (serviceComponent != null && !serviceComponent.getServiceComponentHosts().isEmpty()) { + Set<String> candidateHosts = serviceComponent.getServiceComponentHosts().keySet(); + filterHostsForAction(candidateHosts, service, cluster, Resource.Type.Cluster); + return getHealthyHost(candidateHosts); + } + return null; + } + + protected ServiceComponent getClientComponentForRunningAction(Cluster cluster, Service service) throws AmbariException { /* * We assume Cluster level here. That means that we never run service @@ -2888,7 +2899,6 @@ public class AmbariManagementControllerImpl implements AmbariManagementControlle * That also means that we can not run service check if the only host * that has client component is in maintenance state */ - Resource.Type opLvl = Resource.Type.Cluster; StackId stackId = service.getDesiredStackVersion(); ComponentInfo compInfo = @@ -2899,9 +2909,7 @@ public class AmbariManagementControllerImpl implements AmbariManagementControlle ServiceComponent serviceComponent = service.getServiceComponent(compInfo.getName()); if (!serviceComponent.getServiceComponentHosts().isEmpty()) { - Set<String> candidateHosts = serviceComponent.getServiceComponentHosts().keySet(); - filterHostsForAction(candidateHosts, service, cluster, opLvl); - return getHealthyHost(candidateHosts); + return serviceComponent; } } catch (ServiceComponentNotFoundException e) { LOG.warn("Could not find required component to run action" @@ -2913,17 +2921,12 @@ public class AmbariManagementControllerImpl implements AmbariManagementControlle // any component will do Map<String, ServiceComponent> components = service.getServiceComponents(); - if (components.isEmpty()) { - return null; - } - - for (ServiceComponent serviceComponent : components.values()) { - if (serviceComponent.getServiceComponentHosts().isEmpty()) { - continue; + if (!components.isEmpty()) { + for (ServiceComponent serviceComponent : components.values()) { + if (!serviceComponent.getServiceComponentHosts().isEmpty()) { + return serviceComponent; + } } - Set<String> candidateHosts = serviceComponent.getServiceComponentHosts().keySet(); - filterHostsForAction(candidateHosts, service, cluster, opLvl); - return getHealthyHost(candidateHosts); } return null; } @@ -2932,7 +2935,7 @@ public class AmbariManagementControllerImpl implements AmbariManagementControlle * Utility method that filters out hosts from set based on their maintenance * state status. */ - private void filterHostsForAction(Set<String> candidateHosts, Service service, + protected void filterHostsForAction(Set<String> candidateHosts, Service service, final Cluster cluster, final Resource.Type level) throws AmbariException { http://git-wip-us.apache.org/repos/asf/ambari/blob/c6f068c8/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerImplTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerImplTest.java b/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerImplTest.java index affa4c9..5c8b4ad 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerImplTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerImplTest.java @@ -36,6 +36,7 @@ import org.apache.ambari.server.actionmanager.ActionDBAccessorImpl; import org.apache.ambari.server.actionmanager.ActionManager; import org.apache.ambari.server.api.services.AmbariMetaInfo; import org.apache.ambari.server.configuration.Configuration; +import org.apache.ambari.server.controller.spi.Resource; import org.apache.ambari.server.orm.InMemoryDefaultTestModule; import org.apache.ambari.server.orm.entities.LdapSyncSpecEntity; import org.apache.ambari.server.security.authorization.Users; @@ -43,11 +44,13 @@ import org.apache.ambari.server.security.ldap.AmbariLdapDataPopulator; import org.apache.ambari.server.security.ldap.LdapBatchDto; import org.apache.ambari.server.state.Cluster; import org.apache.ambari.server.state.Clusters; +import org.apache.ambari.server.state.ComponentInfo; import org.apache.ambari.server.state.Host; import org.apache.ambari.server.state.MaintenanceState; import org.apache.ambari.server.state.Service; import org.apache.ambari.server.state.ServiceComponent; import org.apache.ambari.server.state.ServiceComponentHost; +import org.apache.ambari.server.state.ServiceComponentImpl; import org.apache.ambari.server.state.ServiceInfo; import org.apache.ambari.server.state.ServiceOsSpecific; import org.apache.ambari.server.state.StackId; @@ -83,6 +86,8 @@ import static org.easymock.EasyMock.replay; import static org.easymock.EasyMock.reset; import static org.easymock.EasyMock.verify; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -175,7 +180,6 @@ public class AmbariManagementControllerImplTest { // member state mocks Injector injector = createStrictMock(Injector.class); Capture<AmbariManagementController> controllerCapture = new Capture<AmbariManagementController>(); - Clusters clusters = createNiceMock(Clusters.class); ClusterRequest request1 = new ClusterRequest(null, "cluster1", "1", Collections.<String>emptySet()); Cluster cluster = createNiceMock(Cluster.class); @@ -208,6 +212,182 @@ public class AmbariManagementControllerImplTest { verify(injector, clusters, cluster, response); } + @Test + public void testGetClientHostForRunningAction_componentIsNull() throws Exception { + Injector injector = createNiceMock(Injector.class); + + Cluster cluster = createNiceMock(Cluster.class); + Service service = createNiceMock(Service.class); + ServiceComponent component = null; + + replay(cluster, service, injector); + + AmbariManagementControllerImpl controller = new AmbariManagementControllerImpl(null, clusters, injector); + String host = controller.getClientHostForRunningAction(cluster, service, component); + + assertNull(host); + verify(cluster, service, injector); + } + + @Test + public void testGetClientHostForRunningAction_componentMapIsEmpty() throws Exception { + Injector injector = createNiceMock(Injector.class); + + Cluster cluster = createNiceMock(Cluster.class); + Service service = createNiceMock(Service.class); + ServiceComponent component = createNiceMock(ServiceComponent.class); + Map<String, ServiceComponentHost> hostMap = new HashMap<String, ServiceComponentHost>(); + expect(component.getServiceComponentHosts()).andReturn(hostMap); + + replay(cluster, service, component, injector); + + AmbariManagementControllerImpl controller = new AmbariManagementControllerImpl(null, clusters, injector); + String host = controller.getClientHostForRunningAction(cluster, service, component); + + verify(cluster, service, component, injector); + assertNull(host); + } + + @Test + public void testGetClientHostForRunningAction_returnsHelathyHost() throws Exception { + Injector injector = createNiceMock(Injector.class); + ActionManager actionManager = createNiceMock(ActionManager.class); + + Cluster cluster = createNiceMock(Cluster.class); + Service service = createNiceMock(Service.class); + ServiceComponent component = createNiceMock(ServiceComponent.class); + Map<String, ServiceComponentHost> hostMap = createNiceMock(Map.class); + Set<String> hostsSet = createNiceMock(Set.class); + expect(hostMap.isEmpty()).andReturn(false); + expect(hostMap.keySet()).andReturn(hostsSet); + expect(component.getServiceComponentHosts()).andReturn(hostMap).times(2); + + replay(cluster, service, component, injector, actionManager, hostMap, hostsSet); + + AmbariManagementControllerImpl controller = createMockBuilder(AmbariManagementControllerImpl.class) + .addMockedMethod("filterHostsForAction") + .addMockedMethod("getHealthyHost") + .withConstructor(actionManager, clusters, injector) + .createMock(); + expect(controller.getHealthyHost(hostsSet)).andReturn("healthy_host"); + controller.filterHostsForAction(hostsSet, service, cluster, Resource.Type.Cluster); + expectLastCall().once(); + + replay(controller); + String host = controller.getClientHostForRunningAction(cluster, service, component); + + assertEquals("healthy_host", host); + verify(controller, cluster, service, component, injector, hostMap); + } + + @Test + public void testGetClientHostForRunningAction_clientComponent() throws Exception { + Injector injector = createNiceMock(Injector.class); + Cluster cluster = createNiceMock(Cluster.class); + Service service = createNiceMock(Service.class); + StackId stackId = createNiceMock(StackId.class); + ServiceComponent component = createNiceMock(ServiceComponent.class); + + expect(service.getName()).andReturn("service"); + expect(service.getServiceComponent("component")).andReturn(component); + expect(service.getDesiredStackVersion()).andReturn(stackId); + expect(stackId.getStackName()).andReturn("stack"); + expect(stackId.getStackVersion()).andReturn("1.0"); + + ServiceInfo serviceInfo = createNiceMock(ServiceInfo.class); + ComponentInfo compInfo = createNiceMock(ComponentInfo.class); + expect(serviceInfo.getClientComponent()).andReturn(compInfo); + expect(compInfo.getName()).andReturn("component"); + expect(component.getServiceComponentHosts()).andReturn(Collections.<String, ServiceComponentHost>singletonMap("host", null)); + expect(ambariMetaInfo.getServiceInfo("stack", "1.0", "service")).andReturn(serviceInfo); + + replay(injector, cluster, service, component, serviceInfo, compInfo, ambariMetaInfo, stackId); + + AmbariManagementControllerImpl controller = new AmbariManagementControllerImpl(null, clusters, injector); + setAmbariMetaInfo(ambariMetaInfo, controller); + ServiceComponent resultComponent = controller.getClientComponentForRunningAction(cluster, service); + + assertNotNull(resultComponent); + assertEquals(component, resultComponent); + verify(injector, cluster, service, component, serviceInfo, compInfo, ambariMetaInfo, stackId); + } + + @Test + public void testGetClientHostForRunningAction_clientComponentThrowsException() throws Exception { + Injector injector = createNiceMock(Injector.class); + Cluster cluster = createNiceMock(Cluster.class); + Service service = createNiceMock(Service.class); + StackId stackId = createNiceMock(StackId.class); + ServiceComponent component1 = createNiceMock(ServiceComponent.class); + ServiceComponent component2 = createNiceMock(ServiceComponent.class); + + expect(service.getName()).andReturn("service"); + expect(service.getServiceComponent("component")).andThrow( + new ServiceComponentNotFoundException("cluster", "service", "component")); + expect(service.getDesiredStackVersion()).andReturn(stackId); + expect(stackId.getStackName()).andReturn("stack"); + expect(stackId.getStackVersion()).andReturn("1.0"); + Map<String, ServiceComponent> componentsMap = new HashMap<String, ServiceComponent>(); + componentsMap.put("component1", component1); + componentsMap.put("component2", component2); + expect(service.getServiceComponents()).andReturn(componentsMap); + expect(component1.getServiceComponentHosts()).andReturn(Collections.EMPTY_MAP); + expect(component2.getServiceComponentHosts()).andReturn( + Collections.<String, ServiceComponentHost>singletonMap("anyHost", null)); + + ServiceInfo serviceInfo = createNiceMock(ServiceInfo.class); + ComponentInfo compInfo = createNiceMock(ComponentInfo.class); + expect(serviceInfo.getClientComponent()).andReturn(compInfo); + expect(compInfo.getName()).andReturn("component"); + expect(ambariMetaInfo.getServiceInfo("stack", "1.0", "service")).andReturn(serviceInfo); + + replay(injector, cluster, service, component1, component2, serviceInfo, compInfo, ambariMetaInfo, stackId); + + AmbariManagementControllerImpl controller = new AmbariManagementControllerImpl(null, clusters, injector); + setAmbariMetaInfo(ambariMetaInfo, controller); + ServiceComponent resultComponent = controller.getClientComponentForRunningAction(cluster, service); + + assertNotNull(resultComponent); + assertEquals(component2, resultComponent); + verify(injector, cluster, service, component1, component2, serviceInfo, compInfo, ambariMetaInfo, stackId); + } + + @Test + public void testGetClientHostForRunningAction_noClientComponent() throws Exception { + Injector injector = createNiceMock(Injector.class); + Cluster cluster = createNiceMock(Cluster.class); + Service service = createNiceMock(Service.class); + StackId stackId = createNiceMock(StackId.class); + ServiceComponent component1 = createNiceMock(ServiceComponent.class); + ServiceComponent component2 = createNiceMock(ServiceComponent.class); + + expect(service.getName()).andReturn("service"); + expect(service.getDesiredStackVersion()).andReturn(stackId); + expect(stackId.getStackName()).andReturn("stack"); + expect(stackId.getStackVersion()).andReturn("1.0"); + Map<String, ServiceComponent> componentsMap = new HashMap<String, ServiceComponent>(); + componentsMap.put("component1", component1); + componentsMap.put("component2", component2); + expect(service.getServiceComponents()).andReturn(componentsMap); + expect(component1.getServiceComponentHosts()).andReturn(Collections.EMPTY_MAP); + expect(component2.getServiceComponentHosts()).andReturn( + Collections.<String, ServiceComponentHost>singletonMap("anyHost", null)); + + ServiceInfo serviceInfo = createNiceMock(ServiceInfo.class); + expect(serviceInfo.getClientComponent()).andReturn(null); + expect(ambariMetaInfo.getServiceInfo("stack", "1.0", "service")).andReturn(serviceInfo); + + replay(injector, cluster, service, component1, component2, serviceInfo, ambariMetaInfo, stackId); + + AmbariManagementControllerImpl controller = new AmbariManagementControllerImpl(null, clusters, injector); + setAmbariMetaInfo(ambariMetaInfo, controller); + ServiceComponent resultComponent = controller.getClientComponentForRunningAction(cluster, service); + + assertNotNull(resultComponent); + assertEquals(component2, resultComponent); + verify(injector, cluster, service, component1, component2, serviceInfo, ambariMetaInfo, stackId); + } + /** * Ensure that ClusterNotFoundException is propagated in case where there is a single request. */ @@ -216,7 +396,6 @@ public class AmbariManagementControllerImplTest { // member state mocks Injector injector = createStrictMock(Injector.class); Capture<AmbariManagementController> controllerCapture = new Capture<AmbariManagementController>(); - Clusters clusters = createNiceMock(Clusters.class); // requests ClusterRequest request1 = new ClusterRequest(null, "cluster1", "1", Collections.<String>emptySet()); @@ -259,7 +438,6 @@ public class AmbariManagementControllerImplTest { // member state mocks Injector injector = createStrictMock(Injector.class); Capture<AmbariManagementController> controllerCapture = new Capture<AmbariManagementController>(); - Clusters clusters = createNiceMock(Clusters.class); Cluster cluster = createNiceMock(Cluster.class); Cluster cluster2 = createNiceMock(Cluster.class); @@ -319,7 +497,6 @@ public class AmbariManagementControllerImplTest { Injector injector = createStrictMock(Injector.class); Cluster cluster = createNiceMock(Cluster.class); ActionManager actionManager = createNiceMock(ActionManager.class); - Clusters clusters = createNiceMock(Clusters.class); ClusterRequest clusterRequest = createNiceMock(ClusterRequest.class); // requests @@ -359,7 +536,6 @@ public class AmbariManagementControllerImplTest { Injector injector = createStrictMock(Injector.class); Cluster cluster = createNiceMock(Cluster.class); ActionManager actionManager = createNiceMock(ActionManager.class); - Clusters clusters = createNiceMock(Clusters.class); ClusterRequest clusterRequest = createNiceMock(ClusterRequest.class); // requests @@ -397,9 +573,7 @@ public class AmbariManagementControllerImplTest { // member state mocks Injector injector = createStrictMock(Injector.class); Capture<AmbariManagementController> controllerCapture = new Capture<AmbariManagementController>(); - Clusters clusters = createNiceMock(Clusters.class); StackId stack = createNiceMock(StackId.class); - AmbariMetaInfo metaInfo = createStrictMock(AmbariMetaInfo.class); Cluster cluster = createNiceMock(Cluster.class); final Host host = createNiceMock(Host.class); @@ -435,7 +609,7 @@ public class AmbariManagementControllerImplTest { expect(stack.getStackName()).andReturn("stackName"); expect(stack.getStackVersion()).andReturn("stackVersion"); - expect(metaInfo.getComponentToService("stackName", "stackVersion", "component1")).andReturn("service1"); + expect(ambariMetaInfo.getComponentToService("stackName", "stackVersion", "component1")).andReturn("service1"); expect(cluster.getService("service1")).andReturn(service); expect(service.getServiceComponent("component1")).andReturn(component); expect(component.getName()).andReturn("component1"); @@ -449,15 +623,11 @@ public class AmbariManagementControllerImplTest { // replay mocks replay(maintHelper, injector, clusters, cluster, host, response, stack, - metaInfo, service, component, componentHost); + ambariMetaInfo, service, component, componentHost); //test AmbariManagementController controller = new AmbariManagementControllerImpl(null, clusters, injector); - //need to set private field 'ambariMetaInfo' which is injected at runtime - Class<?> c = controller.getClass(); - Field f = c.getDeclaredField("ambariMetaInfo"); - f.setAccessible(true); - f.set(controller, metaInfo); + setAmbariMetaInfo(ambariMetaInfo, controller); Set<ServiceComponentHostResponse> setResponses = controller.getHostComponents(setRequests); @@ -466,7 +636,7 @@ public class AmbariManagementControllerImplTest { assertEquals(1, setResponses.size()); assertTrue(setResponses.contains(response)); - verify(injector, clusters, cluster, host, response, stack, metaInfo, service, component, componentHost); + verify(injector, clusters, cluster, host, response, stack, ambariMetaInfo, service, component, componentHost); } @Test @@ -474,9 +644,7 @@ public class AmbariManagementControllerImplTest { // member state mocks Injector injector = createStrictMock(Injector.class); Capture<AmbariManagementController> controllerCapture = new Capture<AmbariManagementController>(); - Clusters clusters = createNiceMock(Clusters.class); StackId stack = createNiceMock(StackId.class); - AmbariMetaInfo metaInfo = createStrictMock(AmbariMetaInfo.class); Cluster cluster = createNiceMock(Cluster.class); Host host = createNiceMock(Host.class); @@ -506,23 +674,19 @@ public class AmbariManagementControllerImplTest { expect(stack.getStackName()).andReturn("stackName"); expect(stack.getStackVersion()).andReturn("stackVersion"); - expect(metaInfo.getComponentToService("stackName", "stackVersion", "component1")).andReturn("service1"); + expect(ambariMetaInfo.getComponentToService("stackName", "stackVersion", "component1")).andReturn("service1"); expect(cluster.getService("service1")).andReturn(service); expect(service.getServiceComponent("component1")).andReturn(component); expect(component.getName()).andReturn("component1").anyTimes(); expect(component.getServiceComponentHosts()).andReturn(null); // replay mocks - replay(maintHelper, injector, clusters, cluster, host, stack, metaInfo, + replay(maintHelper, injector, clusters, cluster, host, stack, ambariMetaInfo, service, component); //test AmbariManagementController controller = new AmbariManagementControllerImpl(null, clusters, injector); - //need to set private field 'ambariMetaInfo' which is injected at runtime - Class<?> c = controller.getClass(); - Field f = c.getDeclaredField("ambariMetaInfo"); - f.setAccessible(true); - f.set(controller, metaInfo); + setAmbariMetaInfo(ambariMetaInfo, controller); try { controller.getHostComponents(setRequests); @@ -533,7 +697,7 @@ public class AmbariManagementControllerImplTest { // assert and verify assertSame(controller, controllerCapture.getValue()); - verify(injector, clusters, cluster, host, stack, metaInfo, service, component); + verify(injector, clusters, cluster, host, stack, ambariMetaInfo, service, component); } @Test @@ -541,9 +705,7 @@ public class AmbariManagementControllerImplTest { // member state mocks Injector injector = createStrictMock(Injector.class); Capture<AmbariManagementController> controllerCapture = new Capture<AmbariManagementController>(); - Clusters clusters = createNiceMock(Clusters.class); StackId stack = createNiceMock(StackId.class); - AmbariMetaInfo metaInfo = createNiceMock(AmbariMetaInfo.class); Cluster cluster = createNiceMock(Cluster.class); final Host host = createNiceMock(Host.class); @@ -598,7 +760,7 @@ public class AmbariManagementControllerImplTest { expect(stack.getStackName()).andReturn("stackName").anyTimes(); expect(stack.getStackVersion()).andReturn("stackVersion").anyTimes(); - expect(metaInfo.getComponentToService("stackName", "stackVersion", "component1")).andReturn("service1"); + expect(ambariMetaInfo.getComponentToService("stackName", "stackVersion", "component1")).andReturn("service1"); expect(service.getServiceComponent("component1")).andReturn(component); expect(component.getName()).andReturn("component1"); expect(component.getServiceComponentHosts()).andReturn( @@ -608,13 +770,13 @@ public class AmbariManagementControllerImplTest { expect(componentHost1.convertToResponse()).andReturn(response1); expect(componentHost1.getHostName()).andReturn("host1"); - expect(metaInfo.getComponentToService("stackName", "stackVersion", "component2")).andReturn("service1"); + expect(ambariMetaInfo.getComponentToService("stackName", "stackVersion", "component2")).andReturn("service1"); expect(service.getServiceComponent("component2")).andReturn(component2); expect(component2.getName()).andReturn("component2"); expect(component2.getServiceComponentHosts()).andReturn(null); expect(componentHost2.getHostName()).andReturn("host1"); - expect(metaInfo.getComponentToService("stackName", "stackVersion", "component3")).andReturn("service1"); + expect(ambariMetaInfo.getComponentToService("stackName", "stackVersion", "component3")).andReturn("service1"); expect(service.getServiceComponent("component3")).andReturn(component3); expect(component3.getName()).andReturn("component3"); expect(component3.getServiceComponentHosts()).andReturn( @@ -625,16 +787,12 @@ public class AmbariManagementControllerImplTest { // replay mocks replay(stateHelper, injector, clusters, cluster, host, stack, - metaInfo, service, component, component2, component3, componentHost1, + ambariMetaInfo, service, component, component2, component3, componentHost1, componentHost2, response1, response2); //test AmbariManagementController controller = new AmbariManagementControllerImpl(null, clusters, injector); - //need to set private field 'ambariMetaInfo' which is injected at runtime - Class<?> c = controller.getClass(); - Field f = c.getDeclaredField("ambariMetaInfo"); - f.setAccessible(true); - f.set(controller, metaInfo); + setAmbariMetaInfo(ambariMetaInfo, controller); Set<ServiceComponentHostResponse> setResponses = controller.getHostComponents(setRequests); @@ -644,7 +802,7 @@ public class AmbariManagementControllerImplTest { assertTrue(setResponses.contains(response1)); assertTrue(setResponses.contains(response2)); - verify(injector, clusters, cluster, host, stack, metaInfo, service, component, component2, component3, + verify(injector, clusters, cluster, host, stack, ambariMetaInfo, service, component, component2, component3, componentHost1, componentHost2, response1, response2); } @@ -653,9 +811,7 @@ public class AmbariManagementControllerImplTest { // member state mocks Injector injector = createStrictMock(Injector.class); Capture<AmbariManagementController> controllerCapture = new Capture<AmbariManagementController>(); - Clusters clusters = createNiceMock(Clusters.class); StackId stack = createNiceMock(StackId.class); - AmbariMetaInfo metaInfo = createNiceMock(AmbariMetaInfo.class); Cluster cluster = createNiceMock(Cluster.class); final Host host = createNiceMock(Host.class); @@ -706,7 +862,7 @@ public class AmbariManagementControllerImplTest { put("host1", host); }}).anyTimes(); - expect(metaInfo.getComponentToService("stackName", "stackVersion", "component1")).andReturn("service1"); + expect(ambariMetaInfo.getComponentToService("stackName", "stackVersion", "component1")).andReturn("service1"); expect(cluster.getService("service1")).andReturn(service); expect(service.getServiceComponent("component1")).andReturn(component); expect(component.getName()).andReturn("component1"); @@ -717,10 +873,10 @@ public class AmbariManagementControllerImplTest { expect(componentHost1.convertToResponse()).andReturn(response1); expect(componentHost1.getHostName()).andReturn("host1"); - expect(metaInfo.getComponentToService("stackName", "stackVersion", "component2")).andReturn("service2"); + expect(ambariMetaInfo.getComponentToService("stackName", "stackVersion", "component2")).andReturn("service2"); expect(cluster.getService("service2")).andThrow(new ServiceNotFoundException("cluster1", "service2")); - expect(metaInfo.getComponentToService("stackName", "stackVersion", "component3")).andReturn("service1"); + expect(ambariMetaInfo.getComponentToService("stackName", "stackVersion", "component3")).andReturn("service1"); expect(cluster.getService("service1")).andReturn(service); expect(service.getServiceComponent("component3")).andReturn(component3); expect(component3.getName()).andReturn("component3"); @@ -732,17 +888,13 @@ public class AmbariManagementControllerImplTest { expect(componentHost2.getHostName()).andReturn("host1"); // replay mocks - replay(maintHelper, injector, clusters, cluster, host, stack, metaInfo, + replay(maintHelper, injector, clusters, cluster, host, stack, ambariMetaInfo, service, component, component2, component3, componentHost1, componentHost2, response1, response2); //test AmbariManagementController controller = new AmbariManagementControllerImpl(null, clusters, injector); - //need to set private field 'ambariMetaInfo' which is injected at runtime - Class<?> c = controller.getClass(); - Field f = c.getDeclaredField("ambariMetaInfo"); - f.setAccessible(true); - f.set(controller, metaInfo); + setAmbariMetaInfo(ambariMetaInfo, controller); Set<ServiceComponentHostResponse> setResponses = controller.getHostComponents(setRequests); @@ -752,7 +904,7 @@ public class AmbariManagementControllerImplTest { assertTrue(setResponses.contains(response1)); assertTrue(setResponses.contains(response2)); - verify(injector, clusters, cluster, host, stack, metaInfo, service, component, component2, component3, + verify(injector, clusters, cluster, host, stack, ambariMetaInfo, service, component, component2, component3, componentHost1, componentHost2, response1, response2); } @@ -761,9 +913,7 @@ public class AmbariManagementControllerImplTest { // member state mocks Injector injector = createStrictMock(Injector.class); Capture<AmbariManagementController> controllerCapture = new Capture<AmbariManagementController>(); - Clusters clusters = createNiceMock(Clusters.class); StackId stack = createNiceMock(StackId.class); - AmbariMetaInfo metaInfo = createNiceMock(AmbariMetaInfo.class); Cluster cluster = createNiceMock(Cluster.class); final Host host = createNiceMock(Host.class); @@ -817,7 +967,7 @@ public class AmbariManagementControllerImplTest { expect(stack.getStackVersion()).andReturn("stackVersion").anyTimes(); - expect(metaInfo.getComponentToService("stackName", "stackVersion", "component1")).andReturn("service1"); + expect(ambariMetaInfo.getComponentToService("stackName", "stackVersion", "component1")).andReturn("service1"); expect(cluster.getService("service1")).andReturn(service); expect(service.getServiceComponent("component1")).andReturn(component); expect(component.getName()).andReturn("component1"); @@ -828,12 +978,12 @@ public class AmbariManagementControllerImplTest { expect(componentHost1.convertToResponse()).andReturn(response1); expect(componentHost1.getHostName()).andReturn("host1"); - expect(metaInfo.getComponentToService("stackName", "stackVersion", "component2")).andReturn("service2"); + expect(ambariMetaInfo.getComponentToService("stackName", "stackVersion", "component2")).andReturn("service2"); expect(cluster.getService("service2")).andReturn(service2); expect(service2.getServiceComponent("component2")). andThrow(new ServiceComponentNotFoundException("cluster1", "service2", "component2")); - expect(metaInfo.getComponentToService("stackName", "stackVersion", "component3")).andReturn("service1"); + expect(ambariMetaInfo.getComponentToService("stackName", "stackVersion", "component3")).andReturn("service1"); expect(cluster.getService("service1")).andReturn(service); expect(service.getServiceComponent("component3")).andReturn(component3); expect(component3.getName()).andReturn("component3"); @@ -845,17 +995,13 @@ public class AmbariManagementControllerImplTest { expect(componentHost2.getHostName()).andReturn("host1"); // replay mocks - replay(maintHelper, injector, clusters, cluster, host, stack, metaInfo, + replay(maintHelper, injector, clusters, cluster, host, stack, ambariMetaInfo, service, service2, component, component2, component3, componentHost1, componentHost2, response1, response2); //test AmbariManagementController controller = new AmbariManagementControllerImpl(null, clusters, injector); - //need to set private field 'ambariMetaInfo' which is injected at runtime - Class<?> c = controller.getClass(); - Field f = c.getDeclaredField("ambariMetaInfo"); - f.setAccessible(true); - f.set(controller, metaInfo); + setAmbariMetaInfo(ambariMetaInfo, controller); Set<ServiceComponentHostResponse> setResponses = controller.getHostComponents(setRequests); @@ -865,7 +1011,7 @@ public class AmbariManagementControllerImplTest { assertTrue(setResponses.contains(response1)); assertTrue(setResponses.contains(response2)); - verify(injector, clusters, cluster, host, stack, metaInfo, service, service2, component, component2, component3, + verify(injector, clusters, cluster, host, stack, ambariMetaInfo, service, service2, component, component2, component3, componentHost1, componentHost2, response1, response2); } @@ -874,9 +1020,7 @@ public class AmbariManagementControllerImplTest { // member state mocks Injector injector = createStrictMock(Injector.class); Capture<AmbariManagementController> controllerCapture = new Capture<AmbariManagementController>(); - Clusters clusters = createNiceMock(Clusters.class); StackId stack = createNiceMock(StackId.class); - AmbariMetaInfo metaInfo = createNiceMock(AmbariMetaInfo.class); Cluster cluster = createNiceMock(Cluster.class); final Host host = createNiceMock(Host.class); @@ -928,7 +1072,7 @@ public class AmbariManagementControllerImplTest { expect(stack.getStackName()).andReturn("stackName").anyTimes(); expect(stack.getStackVersion()).andReturn("stackVersion").anyTimes(); - expect(metaInfo.getComponentToService("stackName", "stackVersion", "component1")).andReturn("service1"); + expect(ambariMetaInfo.getComponentToService("stackName", "stackVersion", "component1")).andReturn("service1"); expect(cluster.getService("service1")).andReturn(service); expect(service.getServiceComponent("component1")).andReturn(component); expect(component.getName()).andReturn("component1"); @@ -938,7 +1082,7 @@ public class AmbariManagementControllerImplTest { expect(clusters.getClustersForHost("host2")).andThrow(new HostNotFoundException("host2")); - expect(metaInfo.getComponentToService("stackName", "stackVersion", "component3")).andReturn("service1"); + expect(ambariMetaInfo.getComponentToService("stackName", "stackVersion", "component3")).andReturn("service1"); expect(cluster.getService("service1")).andReturn(service); expect(service.getServiceComponent("component3")).andReturn(component3); expect(component3.getName()).andReturn("component3"); @@ -947,17 +1091,13 @@ public class AmbariManagementControllerImplTest { expect(componentHost2.getHostName()).andReturn("host1"); // replay mocks - replay(maintHelper, injector, clusters, cluster, host, stack, metaInfo, + replay(maintHelper, injector, clusters, cluster, host, stack, ambariMetaInfo, service, service2, component, component2, component3, componentHost1, componentHost2, response1, response2); //test AmbariManagementController controller = new AmbariManagementControllerImpl(null, clusters, injector); - //need to set private field 'ambariMetaInfo' which is injected at runtime - Class<?> c = controller.getClass(); - Field f = c.getDeclaredField("ambariMetaInfo"); - f.setAccessible(true); - f.set(controller, metaInfo); + setAmbariMetaInfo(ambariMetaInfo, controller); Set<ServiceComponentHostResponse> setResponses = controller.getHostComponents(setRequests); Assert.assertNotNull(setResponses); @@ -968,7 +1108,7 @@ public class AmbariManagementControllerImplTest { assertTrue(setResponses.contains(response1)); assertTrue(setResponses.contains(response2)); - verify(injector, clusters, cluster, host, stack, metaInfo, service, service2, component, component2, component3, + verify(injector, clusters, cluster, host, stack, ambariMetaInfo, service, service2, component, component2, component3, componentHost1, componentHost2, response1, response2); } @@ -977,9 +1117,7 @@ public class AmbariManagementControllerImplTest { // member state mocks Injector injector = createStrictMock(Injector.class); Capture<AmbariManagementController> controllerCapture = new Capture<AmbariManagementController>(); - Clusters clusters = createNiceMock(Clusters.class); StackId stack = createNiceMock(StackId.class); - AmbariMetaInfo metaInfo = createNiceMock(AmbariMetaInfo.class); MaintenanceStateHelper maintHelper = createNiceMock(MaintenanceStateHelper.class); Cluster cluster = createNiceMock(Cluster.class); @@ -1011,15 +1149,11 @@ public class AmbariManagementControllerImplTest { expect(clusters.getClustersForHost("host1")).andThrow(new HostNotFoundException("host1")); // replay mocks - replay(maintHelper, injector, clusters, cluster, stack, metaInfo); + replay(maintHelper, injector, clusters, cluster, stack, ambariMetaInfo); //test AmbariManagementController controller = new AmbariManagementControllerImpl(null, clusters, injector); - //need to set private field 'ambariMetaInfo' which is injected at runtime - Class<?> c = controller.getClass(); - Field f = c.getDeclaredField("ambariMetaInfo"); - f.setAccessible(true); - f.set(controller, metaInfo); + setAmbariMetaInfo(ambariMetaInfo, controller); try { controller.getHostComponents(setRequests); @@ -1031,7 +1165,7 @@ public class AmbariManagementControllerImplTest { // assert and verify assertSame(controller, controllerCapture.getValue()); - verify(injector, clusters, cluster, stack, metaInfo); + verify(injector, clusters, cluster, stack, ambariMetaInfo); } @Test @@ -1039,9 +1173,7 @@ public class AmbariManagementControllerImplTest { // member state mocks Injector injector = createStrictMock(Injector.class); Capture<AmbariManagementController> controllerCapture = new Capture<AmbariManagementController>(); - Clusters clusters = createNiceMock(Clusters.class); StackId stack = createNiceMock(StackId.class); - AmbariMetaInfo metaInfo = createNiceMock(AmbariMetaInfo.class); MaintenanceStateHelper maintHelper = createNiceMock(MaintenanceStateHelper.class); // requests @@ -1070,15 +1202,11 @@ public class AmbariManagementControllerImplTest { expect(clusters.getCluster("cluster1")).andThrow(new ClusterNotFoundException("cluster1")); // replay mocks - replay(maintHelper, injector, clusters, stack, metaInfo); + replay(maintHelper, injector, clusters, stack, ambariMetaInfo); //test AmbariManagementController controller = new AmbariManagementControllerImpl(null, clusters, injector); - //need to set private field 'ambariMetaInfo' which is injected at runtime - Class<?> c = controller.getClass(); - Field f = c.getDeclaredField("ambariMetaInfo"); - f.setAccessible(true); - f.set(controller, metaInfo); + setAmbariMetaInfo(ambariMetaInfo, controller); try { controller.getHostComponents(setRequests); @@ -1090,7 +1218,7 @@ public class AmbariManagementControllerImplTest { // assert and verify assertSame(controller, controllerCapture.getValue()); - verify(injector, clusters,stack, metaInfo); + verify(injector, clusters,stack, ambariMetaInfo); } @Test @@ -1098,9 +1226,7 @@ public class AmbariManagementControllerImplTest { // member state mocks Injector injector = createStrictMock(Injector.class); Capture<AmbariManagementController> controllerCapture = new Capture<AmbariManagementController>(); - Clusters clusters = createNiceMock(Clusters.class); StackId stack = createNiceMock(StackId.class); - AmbariMetaInfo metaInfo = createStrictMock(AmbariMetaInfo.class); Cluster cluster = createNiceMock(Cluster.class); Service service = createNiceMock(Service.class); @@ -1143,7 +1269,7 @@ public class AmbariManagementControllerImplTest { expect(stack.getStackName()).andReturn("stackName"); expect(stack.getStackVersion()).andReturn("stackVersion"); - expect(metaInfo.getComponentToService("stackName", "stackVersion", "component1")).andReturn("service1"); + expect(ambariMetaInfo.getComponentToService("stackName", "stackVersion", "component1")).andReturn("service1"); expect(cluster.getService("service1")).andReturn(service); expect(service.getServiceComponent("component1")).andReturn(component); expect(component.getName()).andReturn("component1").anyTimes(); @@ -1156,15 +1282,11 @@ public class AmbariManagementControllerImplTest { // replay mocks replay(maintHelper, injector, clusters, cluster, response1, response2, - stack, metaInfo, service, component, componentHost1, componentHost2); + stack, ambariMetaInfo, service, component, componentHost1, componentHost2); //test AmbariManagementController controller = new AmbariManagementControllerImpl(null, clusters, injector); - //need to set private field 'ambariMetaInfo' which is injected at runtime - Class<?> c = controller.getClass(); - Field f = c.getDeclaredField("ambariMetaInfo"); - f.setAccessible(true); - f.set(controller, metaInfo); + setAmbariMetaInfo(ambariMetaInfo, controller); Set<ServiceComponentHostResponse> setResponses = controller.getHostComponents(setRequests); @@ -1174,7 +1296,7 @@ public class AmbariManagementControllerImplTest { assertTrue(setResponses.contains(response1)); assertTrue(setResponses.contains(response2)); - verify(injector, clusters, cluster, response1, response2, stack, metaInfo, service, component, componentHost1, componentHost2); + verify(injector, clusters, cluster, response1, response2, stack, ambariMetaInfo, service, component, componentHost1, componentHost2); } @Test @@ -1182,9 +1304,7 @@ public class AmbariManagementControllerImplTest { // member state mocks Injector injector = createStrictMock(Injector.class); Capture<AmbariManagementController> controllerCapture = new Capture<AmbariManagementController>(); - Clusters clusters = createNiceMock(Clusters.class); StackId stack = createNiceMock(StackId.class); - AmbariMetaInfo metaInfo = createStrictMock(AmbariMetaInfo.class); Cluster cluster = createNiceMock(Cluster.class); Service service1 = createNiceMock(Service.class); @@ -1251,16 +1371,12 @@ public class AmbariManagementControllerImplTest { // replay mocks replay(maintHelper, injector, clusters, cluster, response1, response2, - response3, stack, metaInfo, service1, service2, component1, component2, + response3, stack, ambariMetaInfo, service1, service2, component1, component2, componentHost1, componentHost2, componentHost3); //test AmbariManagementController controller = new AmbariManagementControllerImpl(null, clusters, injector); - //need to set private field 'ambariMetaInfo' which is injected at runtime - Class<?> c = controller.getClass(); - Field f = c.getDeclaredField("ambariMetaInfo"); - f.setAccessible(true); - f.set(controller, metaInfo); + setAmbariMetaInfo(ambariMetaInfo, controller); Set<ServiceComponentHostResponse> setResponses = controller.getHostComponents(setRequests); @@ -1271,7 +1387,7 @@ public class AmbariManagementControllerImplTest { assertTrue(setResponses.contains(response2)); assertTrue(setResponses.contains(response3)); - verify(injector, clusters, cluster, response1, response2, response3, stack, metaInfo, service1, service2, + verify(injector, clusters, cluster, response1, response2, response3, stack, ambariMetaInfo, service1, service2, component1, component2, componentHost1, componentHost2, componentHost3); } @@ -1280,7 +1396,6 @@ public class AmbariManagementControllerImplTest { Capture<AmbariManagementController> controllerCapture = new Capture<AmbariManagementController>(); Injector injector = createStrictMock(Injector.class); MaintenanceStateHelper maintHelper = createNiceMock(MaintenanceStateHelper.class); - Clusters clusters = createNiceMock(Clusters.class); ServiceInfo serviceInfo = createNiceMock(ServiceInfo.class); Map<String, String> hostParams = new HashMap<String, String>(); @@ -1336,7 +1451,6 @@ public class AmbariManagementControllerImplTest { String SERVER_DB_NAME = "ServerDBName"; ActionManager manager = createNiceMock(ActionManager.class); - Clusters clusters = createNiceMock(Clusters.class); StackId stackId = createNiceMock(StackId.class); Cluster cluster = createNiceMock(Cluster.class); Injector injector = createNiceMock(Injector.class); @@ -1432,6 +1546,14 @@ public class AmbariManagementControllerImplTest { verify(ldapDataPopulator, clusters, users, ldapBatchDto); } + private void setAmbariMetaInfo(AmbariMetaInfo metaInfo, AmbariManagementController controller) throws NoSuchFieldException, IllegalAccessException { + //need to set private field 'ambariMetaInfo' which is injected at runtime + Class<?> c = controller.getClass(); + Field f = c.getDeclaredField("ambariMetaInfo"); + f.setAccessible(true); + f.set(controller, metaInfo); + } + private class MockModule implements Module { @Override