MaintenanceStateHelper { public boolean isOperationAllowed(Cluster cluster, RequestOperationLevel levelObj, RequestResourceFilter reqFilter, String serviceName, String componentName, String hostname) throws AmbariException { Resource.Type level; if (levelObj == null) { level = guessOperationLevel(reqFilter); } else { level = levelObj.getLevel(); } return isOperationAllowed(cluster, level, serviceName, componentName, hostname); } @Inject  MaintenanceStateHelper(Injector injector); boolean isOperationAllowed(Cluster cluster,
      RequestOperationLevel levelObj, RequestResourceFilter reqFilter,
      String serviceName, String componentName, String hostname); boolean isOperationAllowed(Resource.Type level, Service service); boolean isOperationAllowed(Host host, long clusterId,
      Resource.Type level); MaintenanceState getEffectiveState(ServiceComponentHost sch, Host host); MaintenanceState getEffectiveState(ServiceComponentHost sch); MaintenanceState getEffectiveState(long clusterId, Alert alert); Set<Map<String, String>> getMaintenanceHostComponents(
      Clusters clusters, Cluster cluster); boolean isOperationAllowed(Resource.Type operationLevel,
      ServiceComponentHost sch); Resource.Type guessOperationLevel(RequestResourceFilter filter); Set<String> filterHostsInMaintenanceState(Set<String> candidateHosts,
      HostPredicate condition);  }
@Test public void testisOperationAllowed() throws Exception { Injector injector = createStrictMock(Injector.class); Cluster cluster = createMock(Cluster.class); Method isOperationAllowed = MaintenanceStateHelper.class.getDeclaredMethod( "isOperationAllowed", new Class[]{Cluster.class, Type.class, String.class, String.class, String.class}); MaintenanceStateHelper maintenanceStateHelper = createMockBuilder(MaintenanceStateHelper.class) .withConstructor(injector) .addMockedMethod(isOperationAllowed) .createNiceMock(); RequestResourceFilter filter = createMock(RequestResourceFilter.class); RequestOperationLevel level = createMock(RequestOperationLevel.class); expect(level.getLevel()).andReturn(Type.Cluster); expect(maintenanceStateHelper.isOperationAllowed( anyObject(Cluster.class), anyObject(Type.class), anyObject(String.class), anyObject(String.class), anyObject(String.class))).andStubReturn(true); replay(cluster, maintenanceStateHelper, level); maintenanceStateHelper.isOperationAllowed(cluster, level, filter, "service", "component", "hostname"); verify(maintenanceStateHelper, level); maintenanceStateHelper = createMockBuilder(MaintenanceStateHelper.class) .withConstructor(injector) .addMockedMethod(isOperationAllowed) .addMockedMethod("guessOperationLevel") .createNiceMock(); expect(maintenanceStateHelper.guessOperationLevel(anyObject(RequestResourceFilter.class))).andReturn(Type.Cluster); expect(maintenanceStateHelper.isOperationAllowed( anyObject(Cluster.class), anyObject(Type.class), anyObject(String.class), anyObject(String.class), anyObject(String.class))).andStubReturn(true); replay(maintenanceStateHelper); maintenanceStateHelper.isOperationAllowed(cluster, null, filter, "service", "component", "hostname"); verify(maintenanceStateHelper); }
@Test public void testServiceOperationsAllowance() throws Exception { Injector injector = createStrictMock(Injector.class); MaintenanceStateHelper maintenanceStateHelper = createMockBuilder(MaintenanceStateHelper.class) .withConstructor(injector) .createNiceMock(); Service service = createMock(Service.class); expect(service.getMaintenanceState()).andReturn(MaintenanceState.ON); expect(service.getMaintenanceState()).andReturn(MaintenanceState.OFF); replay(maintenanceStateHelper, service); Assert.assertEquals(false, maintenanceStateHelper.isOperationAllowed(Resource.Type.Cluster, service)); Assert.assertEquals(true, maintenanceStateHelper.isOperationAllowed(Resource.Type.Service, service)); Assert.assertEquals(true, maintenanceStateHelper.isOperationAllowed(Resource.Type.Host, service)); Assert.assertEquals(true, maintenanceStateHelper.isOperationAllowed(Resource.Type.HostComponent, service)); Assert.assertEquals(true, maintenanceStateHelper.isOperationAllowed(Resource.Type.Cluster, service)); Assert.assertEquals(true, maintenanceStateHelper.isOperationAllowed(Resource.Type.Service, service)); Assert.assertEquals(true, maintenanceStateHelper.isOperationAllowed(Resource.Type.Host, service)); Assert.assertEquals(true, maintenanceStateHelper.isOperationAllowed(Resource.Type.HostComponent, service)); verify(maintenanceStateHelper, service); }
@Test public void testHostOperationsAllowance() throws Exception { Injector injector = createStrictMock(Injector.class); MaintenanceStateHelper maintenanceStateHelper = createMockBuilder(MaintenanceStateHelper.class) .withConstructor(injector) .createNiceMock(); Host host = createMock(Host.class); expect(host.getMaintenanceState(anyInt())).andReturn(MaintenanceState.ON); expect(host.getMaintenanceState(anyInt())).andReturn(MaintenanceState.OFF); replay(maintenanceStateHelper, host); Assert.assertEquals(false, maintenanceStateHelper.isOperationAllowed(host, 1, Resource.Type.Cluster)); Assert.assertEquals(true, maintenanceStateHelper.isOperationAllowed(host, 1, Resource.Type.Service)); Assert.assertEquals(true, maintenanceStateHelper.isOperationAllowed(host, 1, Resource.Type.Host)); Assert.assertEquals(true, maintenanceStateHelper.isOperationAllowed(host, 1, Resource.Type.HostComponent)); Assert.assertEquals(true, maintenanceStateHelper.isOperationAllowed(host, 1, Resource.Type.Cluster)); Assert.assertEquals(true, maintenanceStateHelper.isOperationAllowed(host, 1, Resource.Type.Service)); Assert.assertEquals(true, maintenanceStateHelper.isOperationAllowed(host, 1, Resource.Type.Host)); Assert.assertEquals(true, maintenanceStateHelper.isOperationAllowed(host, 1, Resource.Type.HostComponent)); verify(maintenanceStateHelper, host); }