AmbariManagementControllerImpl implements AmbariManagementController { private Set<ServiceComponentHostResponse> getHostComponents( ServiceComponentHostRequest request) throws AmbariException { LOG.debug("Processing request {}", request); if (request.getClusterName() == null || request.getClusterName().isEmpty()) { IllegalArgumentException e = new IllegalArgumentException("Invalid arguments, cluster name should not be null"); LOG.debug("Cluster not specified in request", e); throw e; } final Cluster cluster; try { cluster = clusters.getCluster(request.getClusterName()); } catch (ClusterNotFoundException e) { LOG.error("Cluster not found ", e); throw new ParentObjectNotFoundException("Parent Cluster resource doesn't exist", e); } if (request.getHostname() != null) { try { if (!clusters.getClustersForHost(request.getHostname()).contains(cluster)) { LOG.error("Host doesn't belong to cluster - " + request.getHostname()); throw new ParentObjectNotFoundException("Parent Host resource doesn't exist", new HostNotFoundException(request.getClusterName(), request.getHostname())); } } catch (HostNotFoundException e) { LOG.error("Host not found", e); throw new ParentObjectNotFoundException("Parent Host resource doesn't exist", new HostNotFoundException(request.getClusterName(), request.getHostname())); } } if (request.getComponentName() != null) { if (request.getServiceName() == null || request.getServiceName().isEmpty()) { StackId stackId = cluster.getDesiredStackVersion(); String serviceName = ambariMetaInfo.getComponentToService(stackId.getStackName(), stackId.getStackVersion(), request.getComponentName()); if (LOG.isDebugEnabled()) { LOG.debug("Looking up service name for component" + ", componentName=" + request.getComponentName() + ", serviceName=" + serviceName + ", stackInfo=" + stackId.getStackId()); } if (serviceName == null || serviceName.isEmpty()) { LOG.error("Unable to find service for component {}", request.getComponentName()); throw new ServiceComponentHostNotFoundException( cluster.getClusterName(), null, request.getComponentName(), request.getHostname()); } request.setServiceName(serviceName); } } Set<Service> services = new HashSet<>(); if (request.getServiceName() != null && !request.getServiceName().isEmpty()) { services.add(cluster.getService(request.getServiceName())); } else { services.addAll(cluster.getServices().values()); } Set<ServiceComponentHostResponse> response = new HashSet<>(); boolean checkDesiredState = false; State desiredStateToCheck = null; boolean checkState = false; State stateToCheck = null; boolean filterBasedConfigStaleness = false; boolean staleConfig = true; if (request.getStaleConfig() != null) { filterBasedConfigStaleness = true; staleConfig = "true".equals(request.getStaleConfig().toLowerCase()); } if (request.getDesiredState() != null && !request.getDesiredState().isEmpty()) { desiredStateToCheck = State.valueOf(request.getDesiredState()); if (!desiredStateToCheck.isValidDesiredState()) { throw new IllegalArgumentException("Invalid arguments, invalid desired" + " state, desiredState=" + desiredStateToCheck); } checkDesiredState = true; } if (!StringUtils.isEmpty(request.getState())) { stateToCheck = State.valueOf(request.getState()); if (stateToCheck == null) { throw new IllegalArgumentException("Invalid arguments, invalid state, State=" + request.getState()); } checkState = true; } Map<String, DesiredConfig> desiredConfigs = cluster.getDesiredConfigs(); Map<String, Host> hosts = clusters.getHostsForCluster(cluster.getClusterName()); for (Service s : services) { Set<ServiceComponent> components = new HashSet<>(); if (request.getComponentName() != null) { components.add(s.getServiceComponent(request.getComponentName())); } else { components.addAll(s.getServiceComponents().values()); } for (ServiceComponent sc : components) { if (request.getComponentName() != null) { if (!sc.getName().equals(request.getComponentName())) { continue; } } Map<String, ServiceComponentHost> serviceComponentHostMap = sc.getServiceComponentHosts(); if (request.getHostname() != null) { try { if (serviceComponentHostMap == null || !serviceComponentHostMap.containsKey(request.getHostname())) { throw new ServiceComponentHostNotFoundException(cluster.getClusterName(), s.getName(), sc.getName(), request.getHostname()); } ServiceComponentHost sch = serviceComponentHostMap.get(request.getHostname()); if (null == sch) { continue; } if (checkDesiredState && (desiredStateToCheck != sch.getDesiredState())) { continue; } if (checkState && stateToCheck != sch.getState()) { continue; } if (request.getAdminState() != null) { String stringToMatch = sch.getComponentAdminState() == null ? "" : sch.getComponentAdminState().name(); if (!request.getAdminState().equals(stringToMatch)) { continue; } } ServiceComponentHostResponse r = sch.convertToResponse(desiredConfigs); if (null == r || (filterBasedConfigStaleness && r.isStaleConfig() != staleConfig)) { continue; } Host host = hosts.get(sch.getHostName()); if (host == null) { throw new HostNotFoundException(cluster.getClusterName(), sch.getHostName()); } MaintenanceState effectiveMaintenanceState = maintenanceStateHelper.getEffectiveState(sch, host); if(filterByMaintenanceState(request, effectiveMaintenanceState)) { continue; } r.setMaintenanceState(effectiveMaintenanceState.name()); response.add(r); } catch (ServiceComponentHostNotFoundException e) { if (request.getServiceName() == null || request.getComponentName() == null) { LOG.debug("Ignoring not specified host_component ", e); } else { LOG.debug("ServiceComponentHost not found ", e); throw new ServiceComponentHostNotFoundException(cluster.getClusterName(), request.getServiceName(), request.getComponentName(), request.getHostname()); } } } else { for (ServiceComponentHost sch : serviceComponentHostMap.values()) { if (null == sch) { continue; } if (checkDesiredState && (desiredStateToCheck != sch.getDesiredState())) { continue; } if (checkState && stateToCheck != sch.getState()) { continue; } if (request.getAdminState() != null) { String stringToMatch = sch.getComponentAdminState() == null ? "" : sch.getComponentAdminState().name(); if (!request.getAdminState().equals(stringToMatch)) { continue; } } ServiceComponentHostResponse r = sch.convertToResponse(desiredConfigs); if (null == r || (filterBasedConfigStaleness && r.isStaleConfig() != staleConfig)) { continue; } Host host = hosts.get(sch.getHostName()); if (host == null) { throw new HostNotFoundException(cluster.getClusterName(), sch.getHostName()); } MaintenanceState effectiveMaintenanceState = maintenanceStateHelper.getEffectiveState(sch, host); if(filterByMaintenanceState(request, effectiveMaintenanceState)) { continue; } r.setMaintenanceState(effectiveMaintenanceState.name()); response.add(r); } } } } return response; } @Inject  AmbariManagementControllerImpl(ActionManager actionManager,
      Clusters clusters, Injector injector); @Override String getAmbariServerURI(String path); @Override RoleCommandOrder getRoleCommandOrder(Cluster cluster); @Override void createCluster(ClusterRequest request); @Override synchronized void createHostComponents(Set<ServiceComponentHostRequest> requests); @Override void registerRackChange(String clusterName); @Override synchronized ConfigurationResponse createConfiguration(
      ConfigurationRequest request); @Override Config createConfig(Cluster cluster, String type, Map<String, String> properties,
                             String versionTag, Map<String, Map<String, String>> propertiesAttributes); @Override void createUsers(Set<UserRequest> requests); @Override void createGroups(Set<GroupRequest> requests); @Override void createMembers(Set<MemberRequest> requests); @Override Set<MemberResponse> getMembers(Set<MemberRequest> requests); @Override @SuppressWarnings("unchecked") synchronized void updateMembers(Set<MemberRequest> requests); @Override MaintenanceState getEffectiveMaintenanceState(ServiceComponentHost sch); @Override @Transactional synchronized RequestStatusResponse updateClusters(Set<ClusterRequest> requests,
                                                           Map<String, String> requestProperties); boolean isAttributeMapsEqual(Map<String, Map<String, String>> requestConfigAttributes,
          Map<String, Map<String, String>> clusterConfigAttributes); void saveClusterUpdate(ClusterRequest clusterRequest, ClusterResponse clusterResponse); @Override ClusterResponse getClusterUpdateResults(ClusterRequest clusterRequest); @Override String getJobTrackerHost(Cluster cluster); @Override Map<String, Map<String,String>> findConfigurationTagsWithOverrides(
          Cluster cluster, String hostName); @Override RequestExecutionFactory getRequestExecutionFactory(); @Override ExecutionScheduleManager getExecutionScheduleManager(); @Override List<ServiceOsSpecific.Package> getPackagesForServiceHost(ServiceInfo serviceInfo, Map<String, String> hostParams, String osFamily); @Override ExecutionCommand getExecutionCommand(Cluster cluster,
                                              ServiceComponentHost scHost,
                                              RoleCommand roleCommand); @Override Set<StackConfigurationDependencyResponse> getStackConfigurationDependencies(
          Set<StackConfigurationDependencyRequest> requests); @Override RequestStatusResponse createAndPersistStages(Cluster cluster, Map<String, String> requestProperties,
                                                      Map<String, String> requestParameters,
                                                      Map<State, List<Service>> changedServices,
                                                      Map<State, List<ServiceComponent>> changedComponents,
                                                      Map<String, Map<State, List<ServiceComponentHost>>> changedHosts,
                                                      Collection<ServiceComponentHost> ignoredHosts,
                                                      boolean runSmokeTest, boolean reconfigureClients); @Override RequestStageContainer addStages(RequestStageContainer requestStages, Cluster cluster, Map<String, String> requestProperties,
                                 Map<String, String> requestParameters, Map<State, List<Service>> changedServices,
                                 Map<State, List<ServiceComponent>> changedComponents,
                                 Map<String, Map<State, List<ServiceComponentHost>>> changedHosts,
                                 Collection<ServiceComponentHost> ignoredHosts, boolean runSmokeTest,
                                 boolean reconfigureClients); void validateServiceComponentHostRequest(ServiceComponentHostRequest request); @Override String findServiceName(Cluster cluster, String componentName); @Override synchronized void updateUsers(Set<UserRequest> requests); @Override synchronized void deleteCluster(ClusterRequest request); @Override DeleteStatusMetaData deleteHostComponents(
      Set<ServiceComponentHostRequest> requests); @Override void deleteUsers(Set<UserRequest> requests); @Override void deleteGroups(Set<GroupRequest> requests); @Override void deleteMembers(java.util.Set<MemberRequest> requests); @Override Set<ClusterResponse> getClusters(Set<ClusterRequest> requests); @Override Set<ServiceComponentHostResponse> getHostComponents(
      Set<ServiceComponentHostRequest> requests); @Override Set<ConfigurationResponse> getConfigurations(
      Set<ConfigurationRequest> requests); @Override Set<ServiceConfigVersionResponse> getServiceConfigVersions(Set<ServiceConfigVersionRequest> requests); @Override Set<UserResponse> getUsers(Set<UserRequest> requests); @Override Set<GroupResponse> getGroups(Set<GroupRequest> requests); @Override void updateGroups(Set<GroupRequest> requests); @Override List<String> selectHealthyHosts(Set<String> hostList); @Override String getHealthyHost(Set<String> hostList); @Override RequestStatusResponse createAction(ExecuteActionRequest actionRequest,
      Map<String, String> requestProperties); @Override Set<StackResponse> getStacks(Set<StackRequest> requests); @Override synchronized RequestStatusResponse updateStacks(); @Override Set<ExtensionResponse> getExtensions(Set<ExtensionRequest> requests); @Override Set<ExtensionVersionResponse> getExtensionVersions(
      Set<ExtensionVersionRequest> requests); @Override Set<RepositoryResponse> getRepositories(Set<RepositoryRequest> requests); @Override void updateRepositories(Set<RepositoryRequest> requests); @Override void verifyRepositories(Set<RepositoryRequest> requests); @Override Set<StackVersionResponse> getStackVersions(
      Set<StackVersionRequest> requests); @Override Set<StackServiceResponse> getStackServices(
      Set<StackServiceRequest> requests); @Override Set<StackConfigurationResponse> getStackLevelConfigurations(
      Set<StackLevelConfigurationRequest> requests); @Override Set<StackConfigurationResponse> getStackConfigurations(
      Set<StackConfigurationRequest> requests); @Override Set<StackServiceComponentResponse> getStackComponents(
      Set<StackServiceComponentRequest> requests); @Override Set<OperatingSystemResponse> getOperatingSystems(
      Set<OperatingSystemRequest> requests); @Override String getAuthName(); @Override int getAuthId(); @Override Set<RootServiceResponse> getRootServices(
      Set<RootServiceRequest> requests); @Override Set<RootServiceComponentResponse> getRootServiceComponents(
      Set<RootServiceComponentRequest> requests); @Override Clusters getClusters(); @Override ConfigHelper getConfigHelper(); @Override AmbariMetaInfo getAmbariMetaInfo(); @Override ServiceComponentFactory getServiceComponentFactory(); @Override ConfigGroupFactory getConfigGroupFactory(); @Override RoleGraphFactory getRoleGraphFactory(); @Override AbstractRootServiceResponseFactory getRootServiceResponseFactory(); @Override ActionManager getActionManager(); @Override String getJdkResourceUrl(); @Override String getJavaHome(); @Override String getJDKName(); @Override String getJCEName(); @Override String getServerDB(); @Override String getOjdbcUrl(); @Override String getMysqljdbcUrl(); @Override Map<String, String> getRcaParameters(); @Override boolean checkLdapConfigured(); @Override LdapSyncDto getLdapSyncInfo(); @Override boolean isLdapSyncInProgress(); @Override synchronized LdapBatchDto synchronizeLdapUsersAndGroups(
      LdapSyncRequest userRequest, LdapSyncRequest groupRequest); @SuppressWarnings("unchecked") @Override void initializeWidgetsAndLayouts(Cluster cluster, Service service); @Override TimelineMetricCacheProvider getTimelineMetricCacheProvider(); @Override MetricPropertyProviderFactory getMetricPropertyProviderFactory(); @Override LoggingSearchPropertyProvider getLoggingSearchPropertyProvider(); @Override LoggingService getLoggingService(String clusterName); @Override AmbariEventPublisher getAmbariEventPublisher(); @Override KerberosHelper getKerberosHelper(); @Override CredentialStoreService getCredentialStoreService(); Map<String,String> getCredentialStoreServiceProperties(); @Override MetricsCollectorHAManager getMetricsCollectorHAManager(); @Override void deleteExtensionLink(ExtensionLinkRequest request); @Override void createExtensionLink(ExtensionLinkRequest request); @Override void updateExtensionLink(ExtensionLinkRequest request); @Override void updateExtensionLink(ExtensionLinkEntity linkEntity); @Override QuickLinkVisibilityController getQuicklinkVisibilityController(); static final String SKIP_INSTALL_FOR_COMPONENTS; static final String DONT_SKIP_INSTALL_FOR_COMPONENTS; }
@Test public void testGetHostComponents___ServiceComponentHostFilteredByState() throws Exception { Injector injector = createStrictMock(Injector.class); Capture<AmbariManagementController> controllerCapture = EasyMock.newCapture(); StackId stack = createNiceMock(StackId.class); Cluster cluster = createNiceMock(Cluster.class); final Host host = createNiceMock(Host.class); Service service = createNiceMock(Service.class); ServiceComponent component = createNiceMock(ServiceComponent.class); MaintenanceStateHelper maintHelper = createNiceMock(MaintenanceStateHelper.class); final ServiceComponentHost componentHost1 = createNiceMock(ServiceComponentHost.class); ServiceComponentHostResponse response1 = createNiceMock(ServiceComponentHostResponse.class); ServiceComponentHostRequest request1 = new ServiceComponentHostRequest( "cluster1", null, "component1", "host1", null); request1.setState("INSTALLED"); Set<ServiceComponentHostRequest> setRequests = new HashSet<ServiceComponentHostRequest>(); setRequests.add(request1); injector.injectMembers(capture(controllerCapture)); expect(injector.getInstance(Gson.class)).andReturn(null); expect(injector.getInstance(MaintenanceStateHelper.class)).andReturn(maintHelper); expect(injector.getInstance(KerberosHelper.class)).andReturn(createNiceMock(KerberosHelper.class)); expect(maintHelper.getEffectiveState( anyObject(ServiceComponentHost.class), anyObject(Host.class))).andReturn(MaintenanceState.OFF).anyTimes(); expect(clusters.getCluster("cluster1")).andReturn(cluster); expect(clusters.getClustersForHost("host1")).andReturn(Collections.singleton(cluster)); expect(clusters.getHostsForCluster((String) anyObject())).andReturn( new HashMap<String, Host>() {{ put("host1", host); }}).anyTimes(); expect(cluster.getDesiredStackVersion()).andReturn(stack); expect(cluster.getClusterName()).andReturn("cl1"); expect(stack.getStackName()).andReturn("stackName"); expect(stack.getStackVersion()).andReturn("stackVersion"); 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(new HashMap<String, ServiceComponentHost>() {{ put("host1", componentHost1); }}); expect(componentHost1.getState()).andReturn(State.INSTALLED); expect(componentHost1.convertToResponse(null)).andReturn(response1); expect(componentHost1.getHostName()).andReturn("host1"); replay(maintHelper, injector, clusters, cluster, host, stack, ambariMetaInfo, service, component, componentHost1, response1); AmbariManagementController controller = new AmbariManagementControllerImpl(null, clusters, injector); setAmbariMetaInfo(ambariMetaInfo, controller); Set<ServiceComponentHostResponse> responses = controller.getHostComponents(setRequests); assertSame(controller, controllerCapture.getValue()); assertTrue(responses.size() == 1); verify(injector, clusters, cluster, host, stack, ambariMetaInfo, service, component, componentHost1, response1); }
@Test public void testGetHostComponents___ServiceComponentHostFilteredByMaintenanceState() throws Exception { Injector injector = createStrictMock(Injector.class); Capture<AmbariManagementController> controllerCapture = EasyMock.newCapture(); StackId stack = createNiceMock(StackId.class); Cluster cluster = createNiceMock(Cluster.class); final Host host = createNiceMock(Host.class); Service service = createNiceMock(Service.class); ServiceComponent component = createNiceMock(ServiceComponent.class); MaintenanceStateHelper maintHelper = createNiceMock(MaintenanceStateHelper.class); final ServiceComponentHost componentHost1 = createNiceMock(ServiceComponentHost.class); ServiceComponentHostResponse response1 = createNiceMock(ServiceComponentHostResponse.class); ServiceComponentHostRequest request1 = new ServiceComponentHostRequest( "cluster1", null, "component1", "host1", null); request1.setMaintenanceState("ON"); Set<ServiceComponentHostRequest> setRequests = new HashSet<ServiceComponentHostRequest>(); setRequests.add(request1); injector.injectMembers(capture(controllerCapture)); expect(injector.getInstance(Gson.class)).andReturn(null); expect(injector.getInstance(MaintenanceStateHelper.class)).andReturn(maintHelper); expect(injector.getInstance(KerberosHelper.class)).andReturn(createNiceMock(KerberosHelper.class)); expect(maintHelper.getEffectiveState( anyObject(ServiceComponentHost.class), anyObject(Host.class))).andReturn(MaintenanceState.IMPLIED_FROM_SERVICE).anyTimes(); expect(clusters.getCluster("cluster1")).andReturn(cluster); expect(clusters.getClustersForHost("host1")).andReturn(Collections.singleton(cluster)); expect(clusters.getHostsForCluster((String) anyObject())).andReturn( new HashMap<String, Host>() {{ put("host1", host); }}).anyTimes(); expect(cluster.getDesiredStackVersion()).andReturn(stack); expect(cluster.getClusterName()).andReturn("cl1"); expect(stack.getStackName()).andReturn("stackName"); expect(stack.getStackVersion()).andReturn("stackVersion"); 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(new HashMap<String, ServiceComponentHost>() {{ put("host1", componentHost1); }}); expect(componentHost1.convertToResponse(null)).andReturn(response1); expect(componentHost1.getHostName()).andReturn("host1"); replay(maintHelper, injector, clusters, cluster, host, stack, ambariMetaInfo, service, component, componentHost1, response1); AmbariManagementController controller = new AmbariManagementControllerImpl(null, clusters, injector); setAmbariMetaInfo(ambariMetaInfo, controller); Set<ServiceComponentHostResponse> responses = controller.getHostComponents(setRequests); assertSame(controller, controllerCapture.getValue()); assertTrue(responses.size() == 1); verify(injector, clusters, cluster, host, stack, ambariMetaInfo, service, component, componentHost1, response1); }
@Test public void testGetHostComponents___OR_Predicate_ServiceComponentHostNotFoundException() throws Exception { Injector injector = createStrictMock(Injector.class); Capture<AmbariManagementController> controllerCapture = EasyMock.newCapture(); StackId stack = createNiceMock(StackId.class); Cluster cluster = createNiceMock(Cluster.class); final Host host = createNiceMock(Host.class); Service service = createNiceMock(Service.class); ServiceComponent component = createNiceMock(ServiceComponent.class); ServiceComponent component2 = createNiceMock(ServiceComponent.class); ServiceComponent component3 = createNiceMock(ServiceComponent.class); final ServiceComponentHost componentHost1 = createNiceMock(ServiceComponentHost.class); final ServiceComponentHost componentHost2 = createNiceMock(ServiceComponentHost.class); ServiceComponentHostResponse response1 = createNiceMock(ServiceComponentHostResponse.class); ServiceComponentHostResponse response2 = createNiceMock(ServiceComponentHostResponse.class); MaintenanceStateHelper stateHelper = createNiceMock(MaintenanceStateHelper.class); expect(stateHelper.getEffectiveState( anyObject(ServiceComponentHost.class), anyObject(Host.class))).andReturn(MaintenanceState.OFF).anyTimes(); expect(clusters.getHostsForCluster((String) anyObject())).andReturn( new HashMap<String, Host>() {{ put("host1", host); }}).anyTimes(); ServiceComponentHostRequest request1 = new ServiceComponentHostRequest( "cluster1", null, "component1", "host1", null); ServiceComponentHostRequest request2 = new ServiceComponentHostRequest( "cluster1", null, "component2", "host1", null); ServiceComponentHostRequest request3 = new ServiceComponentHostRequest( "cluster1", null, "component3", "host1", null); Set<ServiceComponentHostRequest> setRequests = new HashSet<ServiceComponentHostRequest>(); setRequests.add(request1); setRequests.add(request2); setRequests.add(request3); injector.injectMembers(capture(controllerCapture)); expect(injector.getInstance(Gson.class)).andReturn(null); expect(injector.getInstance(MaintenanceStateHelper.class)).andReturn(stateHelper).anyTimes(); expect(injector.getInstance(KerberosHelper.class)).andReturn(createNiceMock(KerberosHelper.class)); expect(clusters.getCluster("cluster1")).andReturn(cluster).times(3); expect(clusters.getClustersForHost("host1")).andReturn(Collections.singleton(cluster)).anyTimes(); expect(cluster.getService("service1")).andReturn(service).times(3); expect(cluster.getDesiredStackVersion()).andReturn(stack).anyTimes(); expect(stack.getStackName()).andReturn("stackName").anyTimes(); expect(stack.getStackVersion()).andReturn("stackVersion").anyTimes(); expect(ambariMetaInfo.getComponentToService("stackName", "stackVersion", "component1")).andReturn("service1"); expect(service.getServiceComponent("component1")).andReturn(component); expect(component.getName()).andReturn("component1"); expect(component.getServiceComponentHosts()).andReturn( new HashMap<String, ServiceComponentHost>() {{ put("host1", componentHost1); }}); expect(componentHost1.convertToResponse(null)).andReturn(response1); expect(componentHost1.getHostName()).andReturn("host1"); 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(ambariMetaInfo.getComponentToService("stackName", "stackVersion", "component3")).andReturn("service1"); expect(service.getServiceComponent("component3")).andReturn(component3); expect(component3.getName()).andReturn("component3"); expect(component3.getServiceComponentHosts()).andReturn( new HashMap<String, ServiceComponentHost>() {{ put("host1", componentHost2); }}); expect(componentHost2.convertToResponse(null)).andReturn(response2); replay(stateHelper, injector, clusters, cluster, host, stack, ambariMetaInfo, service, component, component2, component3, componentHost1, componentHost2, response1, response2); AmbariManagementController controller = new AmbariManagementControllerImpl(null, clusters, injector); setAmbariMetaInfo(ambariMetaInfo, controller); Set<ServiceComponentHostResponse> setResponses = controller.getHostComponents(setRequests); assertSame(controller, controllerCapture.getValue()); assertEquals(2, setResponses.size()); assertTrue(setResponses.contains(response1)); assertTrue(setResponses.contains(response2)); verify(injector, clusters, cluster, host, stack, ambariMetaInfo, service, component, component2, component3, componentHost1, componentHost2, response1, response2); }
@Test public void testGetHostComponents___OR_Predicate_ServiceNotFoundException() throws Exception { Injector injector = createStrictMock(Injector.class); Capture<AmbariManagementController> controllerCapture = EasyMock.newCapture(); StackId stack = createNiceMock(StackId.class); Cluster cluster = createNiceMock(Cluster.class); final Host host = createNiceMock(Host.class); Service service = createNiceMock(Service.class); ServiceComponent component = createNiceMock(ServiceComponent.class); ServiceComponent component2 = createNiceMock(ServiceComponent.class); ServiceComponent component3 = createNiceMock(ServiceComponent.class); final ServiceComponentHost componentHost1 = createNiceMock(ServiceComponentHost.class); final ServiceComponentHost componentHost2 = createNiceMock(ServiceComponentHost.class); ServiceComponentHostResponse response1 = createNiceMock(ServiceComponentHostResponse.class); ServiceComponentHostResponse response2 = createNiceMock(ServiceComponentHostResponse.class); MaintenanceStateHelper maintHelper = createNiceMock(MaintenanceStateHelper.class); expect(maintHelper.getEffectiveState( anyObject(ServiceComponentHost.class), anyObject(Host.class))).andReturn(MaintenanceState.OFF).anyTimes(); ServiceComponentHostRequest request1 = new ServiceComponentHostRequest( "cluster1", null, "component1", "host1", null); ServiceComponentHostRequest request2 = new ServiceComponentHostRequest( "cluster1", null, "component2", "host1", null); ServiceComponentHostRequest request3 = new ServiceComponentHostRequest( "cluster1", null, "component3", "host1", null); Set<ServiceComponentHostRequest> setRequests = new HashSet<ServiceComponentHostRequest>(); setRequests.add(request1); setRequests.add(request2); setRequests.add(request3); injector.injectMembers(capture(controllerCapture)); expect(injector.getInstance(Gson.class)).andReturn(null); expect(injector.getInstance(MaintenanceStateHelper.class)).andReturn(maintHelper).anyTimes(); expect(injector.getInstance(KerberosHelper.class)).andReturn(createNiceMock(KerberosHelper.class)); expect(clusters.getCluster("cluster1")).andReturn(cluster).times(3); expect(clusters.getClustersForHost("host1")).andReturn(Collections.singleton(cluster)).anyTimes(); expect(cluster.getDesiredStackVersion()).andReturn(stack).anyTimes(); expect(stack.getStackName()).andReturn("stackName").anyTimes(); expect(stack.getStackVersion()).andReturn("stackVersion").anyTimes(); expect(clusters.getHostsForCluster((String) anyObject())).andReturn( new HashMap<String, Host>() {{ put("host1", host); }}).anyTimes(); 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"); expect(component.getServiceComponentHosts()).andReturn(new HashMap<String, ServiceComponentHost>() {{ put("host1", componentHost1); }}); expect(componentHost1.convertToResponse(null)).andReturn(response1); expect(componentHost1.getHostName()).andReturn("host1"); expect(ambariMetaInfo.getComponentToService("stackName", "stackVersion", "component2")).andReturn("service2"); expect(cluster.getService("service2")).andThrow(new ServiceNotFoundException("cluster1", "service2")); 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"); expect(component3.getServiceComponentHosts()).andReturn(new HashMap<String, ServiceComponentHost>() {{ put("host1", componentHost2); }}); expect(componentHost2.convertToResponse(null)).andReturn(response2); expect(componentHost2.getHostName()).andReturn("host1"); replay(maintHelper, injector, clusters, cluster, host, stack, ambariMetaInfo, service, component, component2, component3, componentHost1, componentHost2, response1, response2); AmbariManagementController controller = new AmbariManagementControllerImpl(null, clusters, injector); setAmbariMetaInfo(ambariMetaInfo, controller); Set<ServiceComponentHostResponse> setResponses = controller.getHostComponents(setRequests); assertSame(controller, controllerCapture.getValue()); assertEquals(2, setResponses.size()); assertTrue(setResponses.contains(response1)); assertTrue(setResponses.contains(response2)); verify(injector, clusters, cluster, host, stack, ambariMetaInfo, service, component, component2, component3, componentHost1, componentHost2, response1, response2); }
@Test public void testGetHostComponents___OR_Predicate_ServiceComponentNotFoundException() throws Exception { Injector injector = createStrictMock(Injector.class); Capture<AmbariManagementController> controllerCapture = EasyMock.newCapture(); StackId stack = createNiceMock(StackId.class); Cluster cluster = createNiceMock(Cluster.class); final Host host = createNiceMock(Host.class); Service service = createNiceMock(Service.class); Service service2 = createNiceMock(Service.class); ServiceComponent component = createNiceMock(ServiceComponent.class); ServiceComponent component2 = createNiceMock(ServiceComponent.class); ServiceComponent component3 = createNiceMock(ServiceComponent.class); final ServiceComponentHost componentHost1 = createNiceMock(ServiceComponentHost.class); final ServiceComponentHost componentHost2 = createNiceMock(ServiceComponentHost.class); ServiceComponentHostResponse response1 = createNiceMock(ServiceComponentHostResponse.class); ServiceComponentHostResponse response2 = createNiceMock(ServiceComponentHostResponse.class); MaintenanceStateHelper maintHelper = createNiceMock(MaintenanceStateHelper.class); expect(maintHelper.getEffectiveState( anyObject(ServiceComponentHost.class), anyObject(Host.class))).andReturn(MaintenanceState.OFF).anyTimes(); ServiceComponentHostRequest request1 = new ServiceComponentHostRequest( "cluster1", null, "component1", "host1", null); ServiceComponentHostRequest request2 = new ServiceComponentHostRequest( "cluster1", null, "component2", "host1", null); ServiceComponentHostRequest request3 = new ServiceComponentHostRequest( "cluster1", null, "component3", "host1", null); Set<ServiceComponentHostRequest> setRequests = new HashSet<ServiceComponentHostRequest>(); setRequests.add(request1); setRequests.add(request2); setRequests.add(request3); injector.injectMembers(capture(controllerCapture)); expect(injector.getInstance(Gson.class)).andReturn(null); expect(injector.getInstance(MaintenanceStateHelper.class)).andReturn(maintHelper).anyTimes(); expect(injector.getInstance(KerberosHelper.class)).andReturn(createNiceMock(KerberosHelper.class)); expect(clusters.getCluster("cluster1")).andReturn(cluster).times(3); expect(clusters.getClustersForHost("host1")).andReturn(Collections.singleton(cluster)).anyTimes(); expect(clusters.getHostsForCluster((String) anyObject())).andReturn( new HashMap<String, Host>() {{ put("host1", host); }}).anyTimes(); expect(cluster.getDesiredStackVersion()).andReturn(stack).anyTimes(); expect(stack.getStackName()).andReturn("stackName").anyTimes(); expect(stack.getStackVersion()).andReturn("stackVersion").anyTimes(); 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"); expect(component.getServiceComponentHosts()).andReturn( new HashMap<String, ServiceComponentHost>() {{ put("host1", componentHost1); }}); expect(componentHost1.convertToResponse(null)).andReturn(response1); expect(componentHost1.getHostName()).andReturn("host1"); 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(ambariMetaInfo.getComponentToService("stackName", "stackVersion", "component3")).andReturn("service1"); expect(cluster.getService("service1")).andReturn(service); expect(service.getServiceComponent("component3")).andReturn(component3); expect(component3.getName()).andReturn("component3"); expect(component3.getServiceComponentHosts()).andReturn( new HashMap<String, ServiceComponentHost>() {{ put("host1", componentHost2); }}); expect(componentHost2.convertToResponse(null)).andReturn(response2); expect(componentHost2.getHostName()).andReturn("host1"); replay(maintHelper, injector, clusters, cluster, host, stack, ambariMetaInfo, service, service2, component, component2, component3, componentHost1, componentHost2, response1, response2); AmbariManagementController controller = new AmbariManagementControllerImpl(null, clusters, injector); setAmbariMetaInfo(ambariMetaInfo, controller); Set<ServiceComponentHostResponse> setResponses = controller.getHostComponents(setRequests); assertSame(controller, controllerCapture.getValue()); assertEquals(2, setResponses.size()); assertTrue(setResponses.contains(response1)); assertTrue(setResponses.contains(response2)); verify(injector, clusters, cluster, host, stack, ambariMetaInfo, service, service2, component, component2, component3, componentHost1, componentHost2, response1, response2); }
@Test public void testGetHostComponents___OR_Predicate_HostNotFoundException_hostProvidedInQuery() throws Exception { Injector injector = createStrictMock(Injector.class); Capture<AmbariManagementController> controllerCapture = EasyMock.newCapture(); StackId stack = createNiceMock(StackId.class); Cluster cluster = createNiceMock(Cluster.class); final Host host = createNiceMock(Host.class); Service service = createNiceMock(Service.class); Service service2 = createNiceMock(Service.class); ServiceComponent component = createNiceMock(ServiceComponent.class); ServiceComponent component2 = createNiceMock(ServiceComponent.class); ServiceComponent component3 = createNiceMock(ServiceComponent.class); final ServiceComponentHost componentHost1 = createNiceMock(ServiceComponentHost.class); final ServiceComponentHost componentHost2 = createNiceMock(ServiceComponentHost.class); ServiceComponentHostResponse response1 = createNiceMock(ServiceComponentHostResponse.class); ServiceComponentHostResponse response2 = createNiceMock(ServiceComponentHostResponse.class); MaintenanceStateHelper maintHelper = createNiceMock(MaintenanceStateHelper.class); expect(maintHelper.getEffectiveState( anyObject(ServiceComponentHost.class), anyObject(Host.class))).andReturn(MaintenanceState.OFF).anyTimes(); ServiceComponentHostRequest request1 = new ServiceComponentHostRequest( "cluster1", null, "component1", null, null); ServiceComponentHostRequest request2 = new ServiceComponentHostRequest( "cluster1", null, "component2", "host2", null); ServiceComponentHostRequest request3 = new ServiceComponentHostRequest( "cluster1", null, "component3", null, null); Set<ServiceComponentHostRequest> setRequests = new HashSet<ServiceComponentHostRequest>(); setRequests.add(request1); setRequests.add(request2); setRequests.add(request3); injector.injectMembers(capture(controllerCapture)); expect(injector.getInstance(Gson.class)).andReturn(null); expect(injector.getInstance(MaintenanceStateHelper.class)).andReturn(maintHelper).anyTimes(); expect(injector.getInstance(KerberosHelper.class)).andReturn(createNiceMock(KerberosHelper.class)); expect(clusters.getCluster("cluster1")).andReturn(cluster).times(3); expect(clusters.getHostsForCluster((String) anyObject())).andReturn( new HashMap<String, Host>() {{ put("host1", host); }}).anyTimes(); expect(cluster.getDesiredStackVersion()).andReturn(stack).anyTimes(); expect(stack.getStackName()).andReturn("stackName").anyTimes(); expect(stack.getStackVersion()).andReturn("stackVersion").anyTimes(); 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"); expect(component.getServiceComponentHosts()).andReturn(Collections.singletonMap("foo", componentHost1)); expect(componentHost1.convertToResponse(null)).andReturn(response1); expect(componentHost1.getHostName()).andReturn("host1"); expect(clusters.getClustersForHost("host2")).andThrow(new HostNotFoundException("host2")); 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"); expect(component3.getServiceComponentHosts()).andReturn(Collections.singletonMap("foo", componentHost2)); expect(componentHost2.convertToResponse(null)).andReturn(response2); expect(componentHost2.getHostName()).andReturn("host1"); replay(maintHelper, injector, clusters, cluster, host, stack, ambariMetaInfo, service, service2, component, component2, component3, componentHost1, componentHost2, response1, response2); AmbariManagementController controller = new AmbariManagementControllerImpl(null, clusters, injector); setAmbariMetaInfo(ambariMetaInfo, controller); Set<ServiceComponentHostResponse> setResponses = controller.getHostComponents(setRequests); Assert.assertNotNull(setResponses); assertSame(controller, controllerCapture.getValue()); assertEquals(2, setResponses.size()); assertTrue(setResponses.contains(response1)); assertTrue(setResponses.contains(response2)); verify(injector, clusters, cluster, host, stack, ambariMetaInfo, service, service2, component, component2, component3, componentHost1, componentHost2, response1, response2); }
@Test public void testGetHostComponents___OR_Predicate_HostNotFoundException_hostProvidedInURL() throws Exception { Injector injector = createStrictMock(Injector.class); Capture<AmbariManagementController> controllerCapture = EasyMock.newCapture(); StackId stack = createNiceMock(StackId.class); MaintenanceStateHelper maintHelper = createNiceMock(MaintenanceStateHelper.class); Cluster cluster = createNiceMock(Cluster.class); ServiceComponentHostRequest request1 = new ServiceComponentHostRequest( "cluster1", null, "component1", "host1", null); ServiceComponentHostRequest request2 = new ServiceComponentHostRequest( "cluster1", null, "component2", "host1", null); ServiceComponentHostRequest request3 = new ServiceComponentHostRequest( "cluster1", null, "component3", "host1", null); Set<ServiceComponentHostRequest> setRequests = new HashSet<ServiceComponentHostRequest>(); setRequests.add(request1); setRequests.add(request2); setRequests.add(request3); injector.injectMembers(capture(controllerCapture)); expect(injector.getInstance(Gson.class)).andReturn(null); expect(injector.getInstance(MaintenanceStateHelper.class)).andReturn(maintHelper); expect(injector.getInstance(KerberosHelper.class)).andReturn(createNiceMock(KerberosHelper.class)); expect(clusters.getCluster("cluster1")).andReturn(cluster); expect(clusters.getClustersForHost("host1")).andThrow(new HostNotFoundException("host1")); replay(maintHelper, injector, clusters, cluster, stack, ambariMetaInfo); AmbariManagementController controller = new AmbariManagementControllerImpl(null, clusters, injector); setAmbariMetaInfo(ambariMetaInfo, controller); try { controller.getHostComponents(setRequests); fail("expected exception"); } catch (AmbariException e) { } assertSame(controller, controllerCapture.getValue()); verify(injector, clusters, cluster, stack, ambariMetaInfo); }
@Test public void testGetHostComponents___OR_Predicate_ClusterNotFoundException() throws Exception { Injector injector = createStrictMock(Injector.class); Capture<AmbariManagementController> controllerCapture = EasyMock.newCapture(); StackId stack = createNiceMock(StackId.class); MaintenanceStateHelper maintHelper = createNiceMock(MaintenanceStateHelper.class); ServiceComponentHostRequest request1 = new ServiceComponentHostRequest( "cluster1", null, "component1", "host1", null); ServiceComponentHostRequest request2 = new ServiceComponentHostRequest( "cluster1", null, "component2", "host2", null); ServiceComponentHostRequest request3 = new ServiceComponentHostRequest( "cluster1", null, "component3", "host1", null); Set<ServiceComponentHostRequest> setRequests = new HashSet<ServiceComponentHostRequest>(); setRequests.add(request1); setRequests.add(request2); setRequests.add(request3); injector.injectMembers(capture(controllerCapture)); expect(injector.getInstance(Gson.class)).andReturn(null); expect(injector.getInstance(MaintenanceStateHelper.class)).andReturn(maintHelper); expect(injector.getInstance(KerberosHelper.class)).andReturn(createNiceMock(KerberosHelper.class)); expect(clusters.getCluster("cluster1")).andThrow(new ClusterNotFoundException("cluster1")); replay(maintHelper, injector, clusters, stack, ambariMetaInfo); AmbariManagementController controller = new AmbariManagementControllerImpl(null, clusters, injector); setAmbariMetaInfo(ambariMetaInfo, controller); try { controller.getHostComponents(setRequests); fail("expected exception"); } catch (ParentObjectNotFoundException e) { } assertSame(controller, controllerCapture.getValue()); verify(injector, clusters, stack, ambariMetaInfo); }
@Test public void testGetHostComponents___NullHostName() throws Exception { Injector injector = createStrictMock(Injector.class); Capture<AmbariManagementController> controllerCapture = EasyMock.newCapture(); StackId stack = createNiceMock(StackId.class); Cluster cluster = createNiceMock(Cluster.class); Service service = createNiceMock(Service.class); ServiceComponent component = createNiceMock(ServiceComponent.class); ServiceComponentHost componentHost1 = createNiceMock(ServiceComponentHost.class); ServiceComponentHost componentHost2 = createNiceMock(ServiceComponentHost.class); ServiceComponentHostResponse response1 = createNiceMock(ServiceComponentHostResponse.class); ServiceComponentHostResponse response2 = createNiceMock(ServiceComponentHostResponse.class); MaintenanceStateHelper maintHelper = createNiceMock(MaintenanceStateHelper.class); expect(maintHelper.getEffectiveState( anyObject(ServiceComponentHost.class), anyObject(Host.class))).andReturn(MaintenanceState.OFF).anyTimes(); ServiceComponentHostRequest request1 = new ServiceComponentHostRequest( "cluster1", null, "component1", null, null); Set<ServiceComponentHostRequest> setRequests = new HashSet<ServiceComponentHostRequest>(); setRequests.add(request1); Map<String, ServiceComponentHost> mapHostComponents = new HashMap<String, ServiceComponentHost>(); mapHostComponents.put("foo", componentHost1); mapHostComponents.put("bar", componentHost2); injector.injectMembers(capture(controllerCapture)); expect(injector.getInstance(Gson.class)).andReturn(null); expect(injector.getInstance(MaintenanceStateHelper.class)).andReturn(maintHelper).anyTimes(); expect(injector.getInstance(KerberosHelper.class)).andReturn(createNiceMock(KerberosHelper.class)); expect(clusters.getCluster("cluster1")).andReturn(cluster); expect(clusters.getHostsForCluster((String) anyObject())).andReturn( new HashMap<String, Host>() {{ put("host1", createNiceMock(Host.class)); }}).anyTimes(); expect(cluster.getDesiredStackVersion()).andReturn(stack); expect(stack.getStackName()).andReturn("stackName"); expect(stack.getStackVersion()).andReturn("stackVersion"); 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(mapHostComponents); expect(componentHost1.convertToResponse(null)).andReturn(response1); expect(componentHost2.convertToResponse(null)).andReturn(response2); expect(componentHost1.getHostName()).andReturn("host1"); expect(componentHost2.getHostName()).andReturn("host1"); replay(maintHelper, injector, clusters, cluster, response1, response2, stack, ambariMetaInfo, service, component, componentHost1, componentHost2); AmbariManagementController controller = new AmbariManagementControllerImpl(null, clusters, injector); setAmbariMetaInfo(ambariMetaInfo, controller); Set<ServiceComponentHostResponse> setResponses = controller.getHostComponents(setRequests); assertSame(controller, controllerCapture.getValue()); assertEquals(2, setResponses.size()); assertTrue(setResponses.contains(response1)); assertTrue(setResponses.contains(response2)); verify(injector, clusters, cluster, response1, response2, stack, ambariMetaInfo, service, component, componentHost1, componentHost2); }
@Test public void testGetHostComponents___NullHostName_NullComponentName() throws Exception { Injector injector = createStrictMock(Injector.class); Capture<AmbariManagementController> controllerCapture = EasyMock.newCapture(); StackId stack = createNiceMock(StackId.class); Cluster cluster = createNiceMock(Cluster.class); Service service1 = createNiceMock(Service.class); Service service2 = createNiceMock(Service.class); ServiceComponent component1 = createNiceMock(ServiceComponent.class); ServiceComponent component2 = createNiceMock(ServiceComponent.class); ServiceComponentHost componentHost1 = createNiceMock(ServiceComponentHost.class); ServiceComponentHost componentHost2 = createNiceMock(ServiceComponentHost.class); ServiceComponentHost componentHost3 = createNiceMock(ServiceComponentHost.class); ServiceComponentHostResponse response1 = createNiceMock(ServiceComponentHostResponse.class); ServiceComponentHostResponse response2 = createNiceMock(ServiceComponentHostResponse.class); ServiceComponentHostResponse response3 = createNiceMock(ServiceComponentHostResponse.class); MaintenanceStateHelper maintHelper = createNiceMock(MaintenanceStateHelper.class); expect(maintHelper.getEffectiveState( anyObject(ServiceComponentHost.class), anyObject(Host.class))).andReturn(MaintenanceState.OFF).anyTimes(); ServiceComponentHostRequest request1 = new ServiceComponentHostRequest( "cluster1", null, null, null, null); Set<ServiceComponentHostRequest> setRequests = new HashSet<ServiceComponentHostRequest>(); setRequests.add(request1); Map<String, Service> mapServices = new HashMap<String, Service>(); mapServices.put("foo", service1); mapServices.put("bar", service2); Map<String, ServiceComponentHost> mapHostComponents = new HashMap<String, ServiceComponentHost>(); mapHostComponents.put("foo", componentHost1); mapHostComponents.put("bar", componentHost2); injector.injectMembers(capture(controllerCapture)); expect(injector.getInstance(Gson.class)).andReturn(null); expect(injector.getInstance(MaintenanceStateHelper.class)).andReturn(maintHelper).anyTimes(); expect(injector.getInstance(KerberosHelper.class)).andReturn(createNiceMock(KerberosHelper.class)); expect(clusters.getCluster("cluster1")).andReturn(cluster); expect(clusters.getHostsForCluster((String) anyObject())).andReturn( new HashMap<String, Host>() {{ put("host1", createNiceMock(Host.class)); }}).anyTimes(); expect(cluster.getServices()).andReturn(mapServices); expect(service1.getServiceComponents()).andReturn(Collections.singletonMap("foo", component1)); expect(service2.getServiceComponents()).andReturn(Collections.singletonMap("bar", component2)); expect(component1.getName()).andReturn("component1").anyTimes(); expect(component2.getName()).andReturn("component2").anyTimes(); expect(component1.getServiceComponentHosts()).andReturn(mapHostComponents); expect(componentHost1.convertToResponse(null)).andReturn(response1); expect(componentHost2.convertToResponse(null)).andReturn(response2); expect(componentHost1.getHostName()).andReturn("host1"); expect(componentHost2.getHostName()).andReturn("host1"); expect(componentHost3.getHostName()).andReturn("host1"); expect(component2.getServiceComponentHosts()).andReturn(Collections.singletonMap("foobar", componentHost3)); expect(componentHost3.convertToResponse(null)).andReturn(response3); replay(maintHelper, injector, clusters, cluster, response1, response2, response3, stack, ambariMetaInfo, service1, service2, component1, component2, componentHost1, componentHost2, componentHost3); AmbariManagementController controller = new AmbariManagementControllerImpl(null, clusters, injector); setAmbariMetaInfo(ambariMetaInfo, controller); Set<ServiceComponentHostResponse> setResponses = controller.getHostComponents(setRequests); assertSame(controller, controllerCapture.getValue()); assertEquals(3, setResponses.size()); assertTrue(setResponses.contains(response1)); assertTrue(setResponses.contains(response2)); assertTrue(setResponses.contains(response3)); verify(injector, clusters, cluster, response1, response2, response3, stack, ambariMetaInfo, service1, service2, component1, component2, componentHost1, componentHost2, componentHost3); }
@Test public void testGetHostComponents() throws Exception { Injector injector = createStrictMock(Injector.class); Capture<AmbariManagementController> controllerCapture = EasyMock.newCapture(); StackId stack = createNiceMock(StackId.class); Cluster cluster = createNiceMock(Cluster.class); final Host host = createNiceMock(Host.class); Service service = createNiceMock(Service.class); ServiceComponent component = createNiceMock(ServiceComponent.class); final ServiceComponentHost componentHost = createNiceMock(ServiceComponentHost.class); ServiceComponentHostResponse response = createNiceMock(ServiceComponentHostResponse.class); MaintenanceStateHelper maintHelper = createNiceMock(MaintenanceStateHelper.class); expect(maintHelper.getEffectiveState(componentHost)).andReturn(MaintenanceState.OFF).anyTimes(); ServiceComponentHostRequest request1 = new ServiceComponentHostRequest( "cluster1", null, "component1", "host1", null); Set<ServiceComponentHostRequest> setRequests = new HashSet<ServiceComponentHostRequest>(); setRequests.add(request1); injector.injectMembers(capture(controllerCapture)); expect(injector.getInstance(Gson.class)).andReturn(null); expect(injector.getInstance(MaintenanceStateHelper.class)).andReturn(maintHelper).anyTimes(); expect(injector.getInstance(KerberosHelper.class)).andReturn(createNiceMock(KerberosHelper.class)); expect(clusters.getCluster("cluster1")).andReturn(cluster); expect(clusters.getClustersForHost("host1")).andReturn(Collections.singleton(cluster)); expect(clusters.getHostsForCluster((String) anyObject())).andReturn( new HashMap<String, Host>() {{ put("host1", host); }}).anyTimes(); expect(cluster.getDesiredStackVersion()).andReturn(stack); expect(stack.getStackName()).andReturn("stackName"); expect(stack.getStackVersion()).andReturn("stackVersion"); 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"); expect(component.getServiceComponentHosts()).andReturn( new HashMap<String, ServiceComponentHost>() {{ put("host1", componentHost); }}); expect(componentHost.convertToResponse(null)).andReturn(response); expect(componentHost.getHostName()).andReturn("host1").anyTimes(); expect(maintHelper.getEffectiveState(componentHost, host)).andReturn(MaintenanceState.OFF); replay(maintHelper, injector, clusters, cluster, host, response, stack, ambariMetaInfo, service, component, componentHost); AmbariManagementController controller = new AmbariManagementControllerImpl(null, clusters, injector); setAmbariMetaInfo(ambariMetaInfo, controller); Set<ServiceComponentHostResponse> setResponses = controller.getHostComponents(setRequests); assertSame(controller, controllerCapture.getValue()); assertEquals(1, setResponses.size()); assertTrue(setResponses.contains(response)); verify(injector, clusters, cluster, host, response, stack, ambariMetaInfo, service, component, componentHost); }
@Test public void testGetHostComponents___ServiceComponentHostNotFoundException() throws Exception { Injector injector = createStrictMock(Injector.class); Capture<AmbariManagementController> controllerCapture = EasyMock.newCapture(); StackId stack = createNiceMock(StackId.class); Cluster cluster = createNiceMock(Cluster.class); Host host = createNiceMock(Host.class); Service service = createNiceMock(Service.class); ServiceComponent component = createNiceMock(ServiceComponent.class); MaintenanceStateHelper maintHelper = createNiceMock(MaintenanceStateHelper.class); ServiceComponentHostRequest request1 = new ServiceComponentHostRequest( "cluster1", null, "component1", "host1", null); Set<ServiceComponentHostRequest> setRequests = new HashSet<ServiceComponentHostRequest>(); setRequests.add(request1); injector.injectMembers(capture(controllerCapture)); expect(injector.getInstance(Gson.class)).andReturn(null); expect(injector.getInstance(MaintenanceStateHelper.class)).andReturn(maintHelper); expect(injector.getInstance(KerberosHelper.class)).andReturn(createNiceMock(KerberosHelper.class)); expect(clusters.getCluster("cluster1")).andReturn(cluster); expect(clusters.getClustersForHost("host1")).andReturn(Collections.singleton(cluster)); expect(cluster.getDesiredStackVersion()).andReturn(stack); expect(stack.getStackName()).andReturn("stackName"); expect(stack.getStackVersion()).andReturn("stackVersion"); 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(maintHelper, injector, clusters, cluster, host, stack, ambariMetaInfo, service, component); AmbariManagementController controller = new AmbariManagementControllerImpl(null, clusters, injector); setAmbariMetaInfo(ambariMetaInfo, controller); try { controller.getHostComponents(setRequests); fail("expected ServiceComponentHostNotFoundException"); } catch (ServiceComponentHostNotFoundException e) { } assertSame(controller, controllerCapture.getValue()); verify(injector, clusters, cluster, host, stack, ambariMetaInfo, service, component); }