AmbariManagementControllerImpl implements AmbariManagementController { private Set<ClusterResponse> getClusters(ClusterRequest request) throws AmbariException, AuthorizationException { Set<ClusterResponse> response = new HashSet<>(); if (LOG.isDebugEnabled()) { LOG.debug("Received a getClusters request" + ", clusterName=" + request.getClusterName() + ", clusterId=" + request.getClusterId() + ", stackInfo=" + request.getStackVersion()); } Cluster singleCluster = null; try { if (request.getClusterName() != null) { singleCluster = clusters.getCluster(request.getClusterName()); } else if (request.getClusterId() != null) { singleCluster = clusters.getClusterById(request.getClusterId()); } } catch(ClusterNotFoundException e) { if (AuthorizationHelper.isAuthorized(ResourceType.AMBARI, null, RoleAuthorization.AMBARI_ADD_DELETE_CLUSTERS)) { throw e; } else { throw new AuthorizationException(); } } if (singleCluster != null) { ClusterResponse cr = singleCluster.convertToResponse(); cr.setDesiredConfigs(singleCluster.getDesiredConfigs()); cr.setDesiredServiceConfigVersions(singleCluster.getActiveServiceConfigVersions()); cr.setCredentialStoreServiceProperties(getCredentialStoreServiceProperties()); response.add(cr); return response; } Map<String, Cluster> allClusters = clusters.getClusters(); for (Cluster c : allClusters.values()) { if (request.getStackVersion() != null) { if (!request.getStackVersion().equals( c.getDesiredStackVersion().getStackId())) { continue; } } ClusterResponse cr = c.convertToResponse(); cr.setDesiredConfigs(c.getDesiredConfigs()); cr.setDesiredServiceConfigVersions(c.getActiveServiceConfigVersions()); cr.setCredentialStoreServiceProperties(getCredentialStoreServiceProperties()); response.add(cr); } StringBuilder builder = new StringBuilder(); if (LOG.isDebugEnabled()) { clusters.debugDump(builder); LOG.debug("Cluster State for cluster " + builder.toString()); } 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 testGetClusters() throws Exception { Injector injector = createStrictMock(Injector.class); Capture<AmbariManagementController> controllerCapture = EasyMock.newCapture(); ClusterRequest request1 = new ClusterRequest(null, "cluster1", "1", Collections.<String>emptySet()); Cluster cluster = createNiceMock(Cluster.class); ClusterResponse response = createNiceMock(ClusterResponse.class); Set<ClusterRequest> setRequests = new HashSet<ClusterRequest>(); setRequests.add(request1); injector.injectMembers(capture(controllerCapture)); expect(injector.getInstance(Gson.class)).andReturn(null); expect(injector.getInstance(MaintenanceStateHelper.class)).andReturn(null); expect(injector.getInstance(KerberosHelper.class)).andReturn(createNiceMock(KerberosHelper.class)); expect(clusters.getCluster("cluster1")).andReturn(cluster); expect(cluster.convertToResponse()).andReturn(response); CredentialStoreService credentialStoreService = createNiceMock(CredentialStoreService.class); expect(credentialStoreService.isInitialized(anyObject(CredentialStoreType.class))).andReturn(true).anyTimes(); replay(injector, clusters, cluster, response, credentialStoreService); AmbariManagementController controller = new AmbariManagementControllerImpl(null, clusters, injector); Field f = controller.getClass().getDeclaredField("credentialStoreService"); f.setAccessible(true); f.set(controller, credentialStoreService); Set<ClusterResponse> setResponses = controller.getClusters(setRequests); assertEquals(1, setResponses.size()); assertTrue(setResponses.contains(response)); verify(injector, clusters, cluster, response, credentialStoreService); }
@Test public void testGetClusters___ClusterNotFoundException() throws Exception { Injector injector = createStrictMock(Injector.class); Capture<AmbariManagementController> controllerCapture = EasyMock.newCapture(); ClusterRequest request1 = new ClusterRequest(null, "cluster1", "1", Collections.<String>emptySet()); Set<ClusterRequest> setRequests = new HashSet<ClusterRequest>(); setRequests.add(request1); injector.injectMembers(capture(controllerCapture)); expect(injector.getInstance(Gson.class)).andReturn(null); expect(injector.getInstance(MaintenanceStateHelper.class)).andReturn(null); expect(injector.getInstance(KerberosHelper.class)).andReturn(createNiceMock(KerberosHelper.class)); expect(clusters.getCluster("cluster1")).andThrow(new ClusterNotFoundException("cluster1")); replay(injector, clusters); AmbariManagementController controller = new AmbariManagementControllerImpl(null, clusters, injector); try { controller.getClusters(setRequests); fail("expected ClusterNotFoundException"); } catch (ClusterNotFoundException e) { } verify(injector, clusters); }
@Test public void testGetClusters___OR_Predicate_ClusterNotFoundException() throws Exception { Injector injector = createStrictMock(Injector.class); Capture<AmbariManagementController> controllerCapture = EasyMock.newCapture(); Cluster cluster = createNiceMock(Cluster.class); Cluster cluster2 = createNiceMock(Cluster.class); ClusterResponse response = createNiceMock(ClusterResponse.class); ClusterResponse response2 = createNiceMock(ClusterResponse.class); ClusterRequest request1 = new ClusterRequest(null, "cluster1", "1", Collections.<String>emptySet()); ClusterRequest request2 = new ClusterRequest(null, "cluster2", "1", Collections.<String>emptySet()); ClusterRequest request3 = new ClusterRequest(null, "cluster3", "1", Collections.<String>emptySet()); ClusterRequest request4 = new ClusterRequest(null, "cluster4", "1", Collections.<String>emptySet()); Set<ClusterRequest> setRequests = new HashSet<ClusterRequest>(); setRequests.add(request1); setRequests.add(request2); setRequests.add(request3); setRequests.add(request4); injector.injectMembers(capture(controllerCapture)); expect(injector.getInstance(Gson.class)).andReturn(null); expect(injector.getInstance(MaintenanceStateHelper.class)).andReturn(null); expect(injector.getInstance(KerberosHelper.class)).andReturn(createNiceMock(KerberosHelper.class)); expect(clusters.getCluster("cluster1")).andThrow(new ClusterNotFoundException("cluster1")); expect(clusters.getCluster("cluster2")).andReturn(cluster); expect(clusters.getCluster("cluster3")).andReturn(cluster2); expect(clusters.getCluster("cluster4")).andThrow(new ClusterNotFoundException("cluster4")); expect(cluster.convertToResponse()).andReturn(response); expect(cluster2.convertToResponse()).andReturn(response2); CredentialStoreService credentialStoreService = createNiceMock(CredentialStoreService.class); expect(credentialStoreService.isInitialized(anyObject(CredentialStoreType.class))).andReturn(true).anyTimes(); replay(injector, clusters, cluster, cluster2, response, response2, credentialStoreService); AmbariManagementController controller = new AmbariManagementControllerImpl(null, clusters, injector); Field f = controller.getClass().getDeclaredField("credentialStoreService"); f.setAccessible(true); f.set(controller, credentialStoreService); Set<ClusterResponse> setResponses = controller.getClusters(setRequests); assertSame(controller, controllerCapture.getValue()); assertEquals(2, setResponses.size()); assertTrue(setResponses.contains(response)); assertTrue(setResponses.contains(response2)); verify(injector, clusters, cluster, cluster2, response, response2, credentialStoreService); }