ServiceResourceProvider extends AbstractControllerResourceProvider { private ServiceRequest getRequest(Map<String, Object> properties) { ServiceRequest svcRequest = new ServiceRequest( (String) properties.get(SERVICE_CLUSTER_NAME_PROPERTY_ID), (String) properties.get(SERVICE_SERVICE_NAME_PROPERTY_ID), (String) properties.get(SERVICE_SERVICE_STATE_PROPERTY_ID), (String) properties.get(SERVICE_CREDENTIAL_STORE_ENABLED_PROPERTY_ID)); Object o = properties.get(SERVICE_MAINTENANCE_STATE_PROPERTY_ID); if (null != o) { svcRequest.setMaintenanceState(o.toString()); } o = properties.get(SERVICE_CREDENTIAL_STORE_SUPPORTED_PROPERTY_ID); if (null != o) { svcRequest.setMaintenanceState(o.toString()); } return svcRequest; } @AssistedInject  ServiceResourceProvider(@Assisted Set<String> propertyIds,
                          @Assisted Map<Resource.Type, String> keyPropertyIds,
                          @Assisted AmbariManagementController managementController,
                          MaintenanceStateHelper maintenanceStateHelper); @Override Set<String> checkPropertyIds(Set<String> propertyIds); void createServices(Set<ServiceRequest> requests); static final String SERVICE_CLUSTER_NAME_PROPERTY_ID; static final String SERVICE_SERVICE_NAME_PROPERTY_ID; static final String SERVICE_SERVICE_STATE_PROPERTY_ID; static final String SERVICE_MAINTENANCE_STATE_PROPERTY_ID; static final String SERVICE_CREDENTIAL_STORE_SUPPORTED_PROPERTY_ID; static final String SERVICE_CREDENTIAL_STORE_ENABLED_PROPERTY_ID; static final String SERVICE_ATTRIBUTES_PROPERTY_ID; }
@Test public void testDeleteResourcesBadServiceState() throws Exception { AmbariManagementController managementController = createMock(AmbariManagementController.class); Clusters clusters = createNiceMock(Clusters.class); Cluster cluster = createNiceMock(Cluster.class); Service service = createNiceMock(Service.class); String serviceName = "Service100"; expect(managementController.getClusters()).andReturn(clusters).anyTimes(); expect(clusters.getCluster("Cluster100")).andReturn(cluster).anyTimes(); expect(cluster.getClusterId()).andReturn(2L).anyTimes(); expect(cluster.getService(serviceName)).andReturn(service).anyTimes(); expect(service.getDesiredState()).andReturn(State.STARTED).anyTimes(); expect(service.getName()).andReturn(serviceName).anyTimes(); expect(service.getServiceComponents()).andReturn(new HashMap<String, ServiceComponent>()); expect(service.getCluster()).andReturn(cluster); cluster.deleteService(serviceName); replay(managementController, clusters, cluster, service); SecurityContextHolder.getContext().setAuthentication(TestAuthenticationFactory.createAdministrator()); ResourceProvider provider = getServiceProvider(managementController); AbstractResourceProviderTest.TestObserver observer = new AbstractResourceProviderTest.TestObserver(); ((ObservableResourceProvider)provider).addObserver(observer); Predicate predicate = new PredicateBuilder().property(ServiceResourceProvider.SERVICE_CLUSTER_NAME_PROPERTY_ID).equals("Cluster100").and() .property(ServiceResourceProvider.SERVICE_SERVICE_NAME_PROPERTY_ID).equals(serviceName).toPredicate(); provider.deleteResources(new RequestImpl(null, null, null, null), predicate); ResourceProviderEvent lastEvent = observer.getLastEvent(); Assert.assertNotNull(lastEvent); Assert.assertEquals(Resource.Type.Service, lastEvent.getResourceType()); Assert.assertEquals(ResourceProviderEvent.Type.Delete, lastEvent.getType()); Assert.assertEquals(predicate, lastEvent.getPredicate()); Assert.assertNull(lastEvent.getRequest()); verify(managementController, clusters, cluster, service); }
@Test public void testDeleteResourcesStoppedHostComponentState() throws Exception { AmbariManagementController managementController = createMock(AmbariManagementController.class); Clusters clusters = createNiceMock(Clusters.class); Cluster cluster = createNiceMock(Cluster.class); Service service = createNiceMock(Service.class); class TestComponent { public String Name; public ServiceComponent Component; public State DesiredState; public TestComponent(String name, ServiceComponent component, State desiredState) { Name = name; Component = component; DesiredState = desiredState; } } TestComponent component1 = new TestComponent("Component100", createNiceMock(ServiceComponent.class), State.STARTED); TestComponent component2 = new TestComponent("Component101", createNiceMock(ServiceComponent.class), State.STARTED); TestComponent component3 = new TestComponent("Component102", createNiceMock(ServiceComponent.class), State.STARTED); Map<String, ServiceComponent> scMap = new HashMap<String, ServiceComponent>(); scMap.put(component1.Name, component1.Component); scMap.put(component2.Name, component2.Component); scMap.put(component3.Name, component3.Component); Map<String, ServiceComponentHost> schMap1 = new HashMap<>(); ServiceComponentHost sch1 = createNiceMock(ServiceComponentHost.class); schMap1.put("Host1", sch1); Map<String, ServiceComponentHost> schMap2 = new HashMap<>(); ServiceComponentHost sch2 = createNiceMock(ServiceComponentHost.class); schMap2.put("Host2", sch2); Map<String, ServiceComponentHost> schMap3 = new HashMap<>(); ServiceComponentHost sch3 = createNiceMock(ServiceComponentHost.class); schMap3.put("Host3", sch3); String clusterName = "Cluster100"; String serviceName = "Service100"; expect(managementController.getClusters()).andReturn(clusters).anyTimes(); expect(clusters.getCluster(clusterName)).andReturn(cluster).anyTimes(); expect(cluster.getService(serviceName)).andReturn(service).anyTimes(); expect(service.getDesiredState()).andReturn(State.STARTED).anyTimes(); expect(service.getName()).andReturn(serviceName).anyTimes(); expect(service.getServiceComponents()).andReturn(scMap).anyTimes(); expect(component1.Component.getDesiredState()).andReturn(component1.DesiredState).anyTimes(); expect(component2.Component.getDesiredState()).andReturn(component2.DesiredState).anyTimes(); expect(component3.Component.getDesiredState()).andReturn(component3.DesiredState).anyTimes(); expect(component1.Component.canBeRemoved()).andReturn(component1.DesiredState.isRemovableState()).anyTimes(); expect(component2.Component.canBeRemoved()).andReturn(component2.DesiredState.isRemovableState()).anyTimes(); expect(component3.Component.canBeRemoved()).andReturn(component3.DesiredState.isRemovableState()).anyTimes(); expect(component1.Component.getServiceComponentHosts()).andReturn(schMap1).anyTimes(); expect(component2.Component.getServiceComponentHosts()).andReturn(schMap2).anyTimes(); expect(component3.Component.getServiceComponentHosts()).andReturn(schMap3).anyTimes(); State sch1State = State.INSTALLED; expect(sch1.getDesiredState()).andReturn(sch1State).anyTimes(); expect(sch1.canBeRemoved()).andReturn(sch1State.isRemovableState()).anyTimes(); State sch2State = State.INSTALLED; expect(sch2.getDesiredState()).andReturn(sch2State).anyTimes(); expect(sch2.canBeRemoved()).andReturn(sch2State.isRemovableState()).anyTimes(); State sch3State = State.INSTALLED; expect(sch3.getDesiredState()).andReturn(sch3State).anyTimes(); expect(sch3.canBeRemoved()).andReturn(sch3State.isRemovableState()).anyTimes(); expect(service.getCluster()).andReturn(cluster); cluster.deleteService(serviceName); replay(managementController, clusters, cluster, service, component1.Component, component2.Component, component3.Component, sch1, sch2, sch3); SecurityContextHolder.getContext().setAuthentication(TestAuthenticationFactory.createAdministrator()); ResourceProvider provider = getServiceProvider(managementController); AbstractResourceProviderTest.TestObserver observer = new AbstractResourceProviderTest.TestObserver(); ((ObservableResourceProvider)provider).addObserver(observer); Predicate predicate = new PredicateBuilder().property(ServiceResourceProvider.SERVICE_CLUSTER_NAME_PROPERTY_ID).equals(clusterName).and() .property(ServiceResourceProvider.SERVICE_SERVICE_NAME_PROPERTY_ID).equals(serviceName).toPredicate(); provider.deleteResources(new RequestImpl(null, null, null, null), predicate); ResourceProviderEvent lastEvent = observer.getLastEvent(); Assert.assertNotNull(lastEvent); Assert.assertEquals(Resource.Type.Service, lastEvent.getResourceType()); Assert.assertEquals(ResourceProviderEvent.Type.Delete, lastEvent.getType()); Assert.assertEquals(predicate, lastEvent.getPredicate()); Assert.assertNull(lastEvent.getRequest()); verify(managementController, clusters, cluster, service, component1.Component, component2.Component, component3.Component, sch1, sch2, sch3); }