### Question:
HDFSServiceCalculatedState extends DefaultServiceCalculatedState implements ServiceCalculatedState { @Override public State getState(String clusterName, String serviceName) { try { Cluster cluster = getCluster(clusterName); if (cluster != null && managementControllerProvider != null) { AmbariMetaInfo ambariMetaInfo = managementControllerProvider.get().getAmbariMetaInfo(); Service service = cluster.getService(serviceName); StackId stackId = service.getDesiredStackId(); ServiceComponentHostRequest request = new ServiceComponentHostRequest(clusterName, serviceName, null, null, null); Set<ServiceComponentHostResponse> hostComponentResponses = managementControllerProvider.get().getHostComponents(Collections.singleton(request), true); Set<String> startedOrDisabledNNHosts = new HashSet<>(); int nameNodeCount = 0; int nameNodeStartedOrDisabledCount = 0; boolean hasSecondary = false; boolean hasJournal = false; State nonStartedState = null; for (ServiceComponentHostResponse hostComponentResponse : hostComponentResponses ) { try { ComponentInfo componentInfo = ambariMetaInfo.getComponent(stackId.getStackName(), stackId.getStackVersion(), hostComponentResponse.getServiceName(), hostComponentResponse.getComponentName()); if (componentInfo.isMaster()) { String componentName = hostComponentResponse.getComponentName(); boolean isNameNode = false; switch (componentName) { case "NAMENODE": ++nameNodeCount; isNameNode = true; break; case "SECONDARY_NAMENODE": hasSecondary = true; break; case "JOURNALNODE": hasJournal = true; break; } State state = getHostComponentState(hostComponentResponse); switch (state) { case STARTED: case DISABLED: if (isNameNode) { ++nameNodeStartedOrDisabledCount; startedOrDisabledNNHosts.add(hostComponentResponse.getHostname()); } break; default: nonStartedState = state; } } } catch (ObjectNotFoundException e) { } } boolean multipleNameServices = nameNodeCount > 2; int nameServiceWithStartedOrDisabledNNCount = 0; List<NameService> nameServices = new ArrayList<>(); if (multipleNameServices) { ConfigHelper configHelper = managementControllerProvider.get().getConfigHelper(); nameServices = NameService.fromConfig(configHelper, cluster); for (NameService nameService : nameServices) { boolean hasStartedOrDisabledNN = false; for (NameService.NameNode nameNode : nameService.getNameNodes()) { if (startedOrDisabledNNHosts.contains(nameNode.getHost())) { hasStartedOrDisabledNN = true; break; } } if (hasStartedOrDisabledNN) { nameServiceWithStartedOrDisabledNNCount++; } } } if (nonStartedState == null || ((nameNodeCount > 0 && !hasSecondary || hasJournal) && nameNodeStartedOrDisabledCount > 0 && (!multipleNameServices || nameServiceWithStartedOrDisabledNNCount == nameServices.size()))) { return State.STARTED; } return nonStartedState; } } catch (AmbariException e) { LOG.error("Can't determine service state.", e); } return State.UNKNOWN; }  @Override State getState(String clusterName, String serviceName);  }### Answer:
@Test public void testServiceState_STOPPED_WITH_TWO_NS() throws Exception { simulateNNFederation(); ServiceComponent nnComponent = service.getServiceComponent("NAMENODE"); updateServiceState(State.STARTED); nnComponent.getServiceComponentHost("h3").setState(State.INSTALLED); nnComponent.getServiceComponentHost("h4").setState(State.INSTALLED); State state = serviceCalculatedState.getState(clusterName, getServiceName()); Assert.assertEquals(State.INSTALLED, state); }
@Test public void testServiceState_STARTED_WITH_TWO_NS() throws Exception { simulateNNFederation(); ServiceComponent nnComponent = service.getServiceComponent("NAMENODE"); updateServiceState(State.STARTED); nnComponent.getServiceComponentHost("h1").setState(State.INSTALLED); nnComponent.getServiceComponentHost("h4").setState(State.INSTALLED); State state = serviceCalculatedState.getState(clusterName, getServiceName()); Assert.assertEquals(State.STARTED, state); }