Added: incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/AbstractResourceProviderTest.java URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/AbstractResourceProviderTest.java?rev=1440042&view=auto ============================================================================== --- incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/AbstractResourceProviderTest.java (added) +++ incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/AbstractResourceProviderTest.java Tue Jan 29 18:15:16 2013 @@ -0,0 +1,601 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ambari.server.controller.internal; + +import org.apache.ambari.server.controller.*; +import org.apache.ambari.server.controller.utilities.PredicateBuilder; +import org.apache.ambari.server.controller.utilities.PropertyHelper; +import org.apache.ambari.server.controller.RequestStatusResponse; +import org.apache.ambari.server.controller.spi.Predicate; +import org.apache.ambari.server.controller.spi.Request; +import org.apache.ambari.server.controller.spi.Resource; +import org.apache.ambari.server.controller.spi.ResourceProvider; +import org.easymock.EasyMock; +import org.easymock.IArgumentMatcher; +import org.junit.Assert; +import org.junit.Test; + +import static org.easymock.EasyMock.anyObject; +import static org.easymock.EasyMock.createMock; +import static org.easymock.EasyMock.createNiceMock; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.replay; +import static org.easymock.EasyMock.verify; + +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.Map; +import java.util.Set; + +/** + * Resource provider tests. + */ +public class AbstractResourceProviderTest { + + @Test + public void testCreateClusterResources() throws Exception{ + Resource.Type type = Resource.Type.Cluster; + + AmbariManagementController managementController = createMock(AmbariManagementController.class); + RequestStatusResponse response = createNiceMock(RequestStatusResponse.class); + + managementController.createCluster(Matchers.clusterRequest(null, "Cluster100", "HDP-0.1", null)); + managementController.createCluster(Matchers.clusterRequest(99L, null, "HDP-0.1", null)); + + // replay + replay(managementController, response); + + ResourceProvider provider = AbstractResourceProvider.getResourceProvider( + type, + PropertyHelper.getPropertyIds(type), + PropertyHelper.getKeyPropertyIds(type), + managementController); + + TestObserver observer = new TestObserver(); + + ((ObservableResourceProvider)provider).addObserver(observer); + + // add the property map to a set for the request. add more maps for multiple creates + Set<Map<String, Object>> propertySet = new LinkedHashSet<Map<String, Object>>(); + + // Cluster 1: create a map of properties for the request + Map<String, Object> properties = new LinkedHashMap<String, Object>(); + + // add the cluster name to the properties map + properties.put(ClusterResourceProvider.CLUSTER_NAME_PROPERTY_ID, "Cluster100"); + + // add the version to the properties map + properties.put(ClusterResourceProvider.CLUSTER_VERSION_PROPERTY_ID, "HDP-0.1"); + + propertySet.add(properties); + + // Cluster 2: create a map of properties for the request + properties = new LinkedHashMap<String, Object>(); + + // add the cluster id to the properties map + properties.put(ClusterResourceProvider.CLUSTER_ID_PROPERTY_ID, 99L); + + // add the version to the properties map + properties.put(ClusterResourceProvider.CLUSTER_VERSION_PROPERTY_ID, "HDP-0.1"); + + propertySet.add(properties); + + // create the request + Request request = PropertyHelper.getCreateRequest(propertySet); + + provider.createResources(request); + + ResourceProviderEvent lastEvent = observer.getLastEvent(); + Assert.assertNotNull(lastEvent); + Assert.assertEquals(Resource.Type.Cluster, lastEvent.getResourceType()); + Assert.assertEquals(ResourceProviderEvent.Type.Create, lastEvent.getType()); + Assert.assertEquals(request, lastEvent.getRequest()); + Assert.assertNull(lastEvent.getPredicate()); + + // verify + verify(managementController, response); + } + + @Test + public void testGetClusterResources() throws Exception{ + Resource.Type type = Resource.Type.Cluster; + + AmbariManagementController managementController = createMock(AmbariManagementController.class); + + Set<ClusterResponse> allResponse = new HashSet<ClusterResponse>(); + allResponse.add(new ClusterResponse(100L, "Cluster100", null, null)); + allResponse.add(new ClusterResponse(101L, "Cluster101", null, null)); + allResponse.add(new ClusterResponse(102L, "Cluster102", null, null)); + allResponse.add(new ClusterResponse(103L, "Cluster103", null, null)); + allResponse.add(new ClusterResponse(104L, "Cluster104", null, null)); + + Set<ClusterResponse> nameResponse = new HashSet<ClusterResponse>(); + nameResponse.add(new ClusterResponse(102L, "Cluster102", null, null)); + + Set<ClusterResponse> idResponse = new HashSet<ClusterResponse>(); + idResponse.add(new ClusterResponse(103L, "Cluster103", null, null)); + + // set expectations + expect(managementController.getClusters(anyObject(Set.class))).andReturn(allResponse).once(); + expect(managementController.getClusters(anyObject(Set.class))).andReturn(nameResponse).once(); + expect(managementController.getClusters(anyObject(Set.class))).andReturn(idResponse).once(); + + // replay + replay(managementController); + + ResourceProvider provider = AbstractResourceProvider.getResourceProvider( + type, + PropertyHelper.getPropertyIds(type), + PropertyHelper.getKeyPropertyIds(type), + managementController); + + Set<String> propertyIds = new HashSet<String>(); + + propertyIds.add(ClusterResourceProvider.CLUSTER_ID_PROPERTY_ID); + propertyIds.add(ClusterResourceProvider.CLUSTER_NAME_PROPERTY_ID); + + // create the request + Request request = PropertyHelper.getReadRequest(propertyIds); + + // get all ... no predicate + Set<Resource> resources = provider.getResources(request, null); + + Assert.assertEquals(5, resources.size()); + for (Resource resource : resources) { + Long id = (Long) resource.getPropertyValue(ClusterResourceProvider.CLUSTER_ID_PROPERTY_ID); + String name = (String) resource.getPropertyValue(ClusterResourceProvider.CLUSTER_NAME_PROPERTY_ID); + Assert.assertEquals(name, "Cluster" + id); + } + + // get cluster named Cluster102 + Predicate predicate = new PredicateBuilder().property(ClusterResourceProvider.CLUSTER_NAME_PROPERTY_ID).equals("Cluster102").toPredicate(); + resources = provider.getResources(request, predicate); + + Assert.assertEquals(1, resources.size()); + Assert.assertEquals(102L, resources.iterator().next().getPropertyValue(ClusterResourceProvider.CLUSTER_ID_PROPERTY_ID)); + Assert.assertEquals("Cluster102", resources.iterator().next().getPropertyValue(ClusterResourceProvider.CLUSTER_NAME_PROPERTY_ID)); + + // get cluster with id == 103 + predicate = new PredicateBuilder().property(ClusterResourceProvider.CLUSTER_ID_PROPERTY_ID).equals(103L).toPredicate(); + resources = provider.getResources(request, predicate); + + Assert.assertEquals(1, resources.size()); + Assert.assertEquals(103L, resources.iterator().next().getPropertyValue(ClusterResourceProvider.CLUSTER_ID_PROPERTY_ID)); + Assert.assertEquals("Cluster103", resources.iterator().next().getPropertyValue(ClusterResourceProvider.CLUSTER_NAME_PROPERTY_ID)); + + // verify + verify(managementController); + } + + @Test + public void testUpdateClusterResources() throws Exception{ + Resource.Type type = Resource.Type.Cluster; + + AmbariManagementController managementController = createMock(AmbariManagementController.class); + RequestStatusResponse response = createNiceMock(RequestStatusResponse.class); + + Set<ClusterResponse> nameResponse = new HashSet<ClusterResponse>(); + nameResponse.add(new ClusterResponse(102L, "Cluster102", null, null)); + + // set expectations + expect(managementController.getClusters(anyObject(Set.class))).andReturn(nameResponse).once(); + expect(managementController.updateCluster(Matchers.clusterRequest(102L, "Cluster102", "HDP-0.1", null))).andReturn(response).once(); + expect(managementController.updateCluster(Matchers.clusterRequest(103L, null, "HDP-0.1", null))).andReturn(response).once(); + + // replay + replay(managementController, response); + + ResourceProvider provider = AbstractResourceProvider.getResourceProvider( + type, + PropertyHelper.getPropertyIds(type), + PropertyHelper.getKeyPropertyIds(type), + managementController); + + TestObserver observer = new TestObserver(); + + ((ObservableResourceProvider)provider).addObserver(observer); + + Map<String, Object> properties = new LinkedHashMap<String, Object>(); + + properties.put(ClusterResourceProvider.CLUSTER_VERSION_PROPERTY_ID, "HDP-0.1"); + + // create the request + Request request = PropertyHelper.getUpdateRequest(properties); + + // update the cluster named Cluster102 + Predicate predicate = new PredicateBuilder().property(ClusterResourceProvider.CLUSTER_NAME_PROPERTY_ID).equals("Cluster102").toPredicate(); + provider.updateResources(request, predicate); + + // update the cluster where id == 103 + predicate = new PredicateBuilder().property(ClusterResourceProvider.CLUSTER_ID_PROPERTY_ID).equals(103L).toPredicate(); + provider.updateResources(request, predicate); + + ResourceProviderEvent lastEvent = observer.getLastEvent(); + Assert.assertNotNull(lastEvent); + Assert.assertEquals(Resource.Type.Cluster, lastEvent.getResourceType()); + Assert.assertEquals(ResourceProviderEvent.Type.Update, lastEvent.getType()); + Assert.assertEquals(request, lastEvent.getRequest()); + Assert.assertEquals(predicate, lastEvent.getPredicate()); + + // verify + verify(managementController, response); + } + + @Test + public void testDeleteClusterResources() throws Exception{ + Resource.Type type = Resource.Type.Cluster; + + AmbariManagementController managementController = createMock(AmbariManagementController.class); + RequestStatusResponse response = createNiceMock(RequestStatusResponse.class); + + Set<ClusterResponse> nameResponse = new HashSet<ClusterResponse>(); + nameResponse.add(new ClusterResponse(102L, "Cluster102", null, null)); + + // set expectations + managementController.deleteCluster(Matchers.clusterRequest(null, "Cluster102", null, null)); + managementController.deleteCluster(Matchers.clusterRequest(103L, null, null, null)); + + // replay + replay(managementController, response); + + ResourceProvider provider = AbstractResourceProvider.getResourceProvider( + type, + PropertyHelper.getPropertyIds(type), + PropertyHelper.getKeyPropertyIds(type), + managementController); + + TestObserver observer = new TestObserver(); + + ((ObservableResourceProvider)provider).addObserver(observer); + + // delete the cluster named Cluster102 + Predicate predicate = new PredicateBuilder().property(ClusterResourceProvider.CLUSTER_NAME_PROPERTY_ID).equals("Cluster102").toPredicate(); + provider.deleteResources(predicate); + + // delete the cluster where id == 103 + predicate = new PredicateBuilder().property(ClusterResourceProvider.CLUSTER_ID_PROPERTY_ID).equals(103L).toPredicate(); + provider.deleteResources(predicate); + + ResourceProviderEvent lastEvent = observer.getLastEvent(); + Assert.assertNotNull(lastEvent); + Assert.assertEquals(Resource.Type.Cluster, lastEvent.getResourceType()); + Assert.assertEquals(ResourceProviderEvent.Type.Delete, lastEvent.getType()); + Assert.assertEquals(predicate, lastEvent.getPredicate()); + Assert.assertNull(lastEvent.getRequest()); + + // verify + verify(managementController, response); + } + + @Test + public void testCreateServiceResources() throws Exception{ + Resource.Type type = Resource.Type.Service; + + AmbariManagementController managementController = createMock(AmbariManagementController.class); + RequestStatusResponse response = createNiceMock(RequestStatusResponse.class); + +// Set<ServiceRequest> requests = new HashSet<ServiceRequest>(); +// requests.add(Matchers.serviceRequest("Cluster100", "Service100", null, "DEPLOYED")); + managementController.createServices(anyObject(Set.class)); + + // replay + replay(managementController, response); + + ResourceProvider provider = AbstractResourceProvider.getResourceProvider( + type, + PropertyHelper.getPropertyIds(type), + PropertyHelper.getKeyPropertyIds(type), + managementController); + + // add the property map to a set for the request. add more maps for multiple creates + Set<Map<String, Object>> propertySet = new LinkedHashSet<Map<String, Object>>(); + + // Service 1: create a map of properties for the request + Map<String, Object> properties = new LinkedHashMap<String, Object>(); + + // add properties to the request map + properties.put(ServiceResourceProvider.SERVICE_CLUSTER_NAME_PROPERTY_ID, "Cluster100"); + properties.put(ServiceResourceProvider.SERVICE_SERVICE_NAME_PROPERTY_ID, "Service100"); + properties.put(ServiceResourceProvider.SERVICE_SERVICE_STATE_PROPERTY_ID, "DEPLOYED"); + + propertySet.add(properties); + + // create the request + Request request = PropertyHelper.getCreateRequest(propertySet); + + provider.createResources(request); + + // verify + verify(managementController, response); + } + + @Test + public void testGetServiceResources() throws Exception{ + Resource.Type type = Resource.Type.Service; + + AmbariManagementController managementController = createMock(AmbariManagementController.class); + + Set<ServiceResponse> allResponse = new HashSet<ServiceResponse>(); + allResponse.add(new ServiceResponse(100L, "Cluster100", "Service100", null, "HDP-0.1", "DEPLOYED")); + allResponse.add(new ServiceResponse(100L, "Cluster100", "Service101", null, "HDP-0.1", "DEPLOYED")); + allResponse.add(new ServiceResponse(100L, "Cluster100", "Service102", null, "HDP-0.1", "DEPLOYED")); + allResponse.add(new ServiceResponse(100L, "Cluster100", "Service103", null, "HDP-0.1", "DEPLOYED")); + allResponse.add(new ServiceResponse(100L, "Cluster100", "Service104", null, "HDP-0.1", "DEPLOYED")); + + Set<ServiceResponse> nameResponse = new HashSet<ServiceResponse>(); + nameResponse.add(new ServiceResponse(100L, "Cluster100", "Service102", null, "HDP-0.1", "DEPLOYED")); + + Set<ServiceResponse> stateResponse = new HashSet<ServiceResponse>(); + stateResponse.add(new ServiceResponse(100L, "Cluster100", "Service100", null, "HDP-0.1", "DEPLOYED")); + stateResponse.add(new ServiceResponse(100L, "Cluster100", "Service102", null, "HDP-0.1", "DEPLOYED")); + stateResponse.add(new ServiceResponse(100L, "Cluster100", "Service104", null, "HDP-0.1", "DEPLOYED")); + + // set expectations + expect(managementController.getServices(anyObject(Set.class))).andReturn(allResponse).once(); + expect(managementController.getServices(anyObject(Set.class))).andReturn(nameResponse).once(); + expect(managementController.getServices(anyObject(Set.class))).andReturn(stateResponse).once(); + + // replay + replay(managementController); + + ResourceProvider provider = AbstractResourceProvider.getResourceProvider( + type, + PropertyHelper.getPropertyIds(type), + PropertyHelper.getKeyPropertyIds(type), + managementController); + + Set<String> propertyIds = new HashSet<String>(); + + propertyIds.add(ServiceResourceProvider.SERVICE_CLUSTER_NAME_PROPERTY_ID); + propertyIds.add(ServiceResourceProvider.SERVICE_SERVICE_NAME_PROPERTY_ID); + + // create the request + Request request = PropertyHelper.getReadRequest(propertyIds); + // get all ... no predicate + Set<Resource> resources = provider.getResources(request, null); + + Assert.assertEquals(5, resources.size()); + Set<String> names = new HashSet<String>(); + for (Resource resource : resources) { + String clusterName = (String) resource.getPropertyValue(ServiceResourceProvider.SERVICE_CLUSTER_NAME_PROPERTY_ID); + Assert.assertEquals("Cluster100", clusterName); + names.add((String) resource.getPropertyValue(ServiceResourceProvider.SERVICE_SERVICE_NAME_PROPERTY_ID)); + } + // Make sure that all of the response objects got moved into resources + for (ServiceResponse serviceResponse : allResponse ) { + Assert.assertTrue(names.contains(serviceResponse.getServiceName())); + } + + // get service named Service102 + Predicate predicate = new PredicateBuilder().property(ServiceResourceProvider.SERVICE_SERVICE_NAME_PROPERTY_ID).equals("Service102").toPredicate(); + request = PropertyHelper.getReadRequest("ServiceInfo"); + resources = provider.getResources(request, predicate); + + Assert.assertEquals(1, resources.size()); + Assert.assertEquals("Service102", resources.iterator().next().getPropertyValue(ServiceResourceProvider.SERVICE_SERVICE_NAME_PROPERTY_ID)); + + // get services where state == "DEPLOYED" + predicate = new PredicateBuilder().property(ServiceResourceProvider.SERVICE_SERVICE_STATE_PROPERTY_ID).equals("DEPLOYED").toPredicate(); + request = PropertyHelper.getReadRequest(propertyIds); + resources = provider.getResources(request, predicate); + + Assert.assertEquals(3, resources.size()); + names = new HashSet<String>(); + for (Resource resource : resources) { + String clusterName = (String) resource.getPropertyValue(ServiceResourceProvider.SERVICE_CLUSTER_NAME_PROPERTY_ID); + Assert.assertEquals("Cluster100", clusterName); + names.add((String) resource.getPropertyValue(ServiceResourceProvider.SERVICE_SERVICE_NAME_PROPERTY_ID)); + } + // Make sure that all of the response objects got moved into resources + for (ServiceResponse serviceResponse : stateResponse ) { + Assert.assertTrue(names.contains(serviceResponse.getServiceName())); + } + + // verify + verify(managementController); + } + + @Test + public void testUpdateServiceResources() throws Exception{ + Resource.Type type = Resource.Type.Service; + + AmbariManagementController managementController = createMock(AmbariManagementController.class); + RequestStatusResponse response = createNiceMock(RequestStatusResponse.class); + + // set expectations + expect(managementController.updateServices(anyObject(Set.class))).andReturn(response).once(); + + // replay + replay(managementController, response); + + ResourceProvider provider = AbstractResourceProvider.getResourceProvider( + type, + PropertyHelper.getPropertyIds(type), + PropertyHelper.getKeyPropertyIds(type), + managementController); + + // add the property map to a set for the request. + Map<String, Object> properties = new LinkedHashMap<String, Object>(); + + properties.put(ServiceResourceProvider.SERVICE_SERVICE_STATE_PROPERTY_ID, "DEPLOYED"); + + // create the request + Request request = PropertyHelper.getUpdateRequest(properties); + + // update the service named Service102 + Predicate predicate = new PredicateBuilder().property(ServiceResourceProvider.SERVICE_CLUSTER_NAME_PROPERTY_ID).equals("Cluster100"). + and().property(ServiceResourceProvider.SERVICE_SERVICE_NAME_PROPERTY_ID).equals("Service102").toPredicate(); + provider.updateResources(request, predicate); + + // verify + verify(managementController, response); + } + + @Test + public void testCheckPropertyIds() { + Set<String> propertyIds = new HashSet<String>(); + propertyIds.add("foo"); + propertyIds.add("cat1/foo"); + propertyIds.add("cat2/bar"); + propertyIds.add("cat2/baz"); + propertyIds.add("cat3/sub1/bam"); + propertyIds.add("cat4/sub2/sub3/bat"); + propertyIds.add("cat5/subcat5/map"); + + Map<Resource.Type, String> keyPropertyIds = new HashMap<Resource.Type, String>(); + + AmbariManagementController managementController = createMock(AmbariManagementController.class); + + AbstractResourceProvider provider = + (AbstractResourceProvider) AbstractResourceProvider.getResourceProvider( + Resource.Type.Service, + propertyIds, + keyPropertyIds, + managementController); + + Set<String> unsupported = provider.checkPropertyIds(Collections.singleton("foo")); + Assert.assertTrue(unsupported.isEmpty()); + + // note that key is not in the set of known property ids. We allow it if its parent is a known property. + // this allows for Map type properties where we want to treat the entries as individual properties + Assert.assertTrue(provider.checkPropertyIds(Collections.singleton("cat5/subcat5/map/key")).isEmpty()); + + unsupported = provider.checkPropertyIds(Collections.singleton("bar")); + Assert.assertEquals(1, unsupported.size()); + Assert.assertTrue(unsupported.contains("bar")); + + unsupported = provider.checkPropertyIds(Collections.singleton("cat1/foo")); + Assert.assertTrue(unsupported.isEmpty()); + + unsupported = provider.checkPropertyIds(Collections.singleton("cat1")); + Assert.assertTrue(unsupported.isEmpty()); + } + + @Test + public void testGetPropertyIds() { + Set<String> propertyIds = new HashSet<String>(); + propertyIds.add("p1"); + propertyIds.add("foo"); + propertyIds.add("cat1/foo"); + propertyIds.add("cat2/bar"); + propertyIds.add("cat2/baz"); + propertyIds.add("cat3/sub1/bam"); + propertyIds.add("cat4/sub2/sub3/bat"); + + Map<Resource.Type, String> keyPropertyIds = new HashMap<Resource.Type, String>(); + + AmbariManagementController managementController = createMock(AmbariManagementController.class); + + AbstractResourceProvider provider = + (AbstractResourceProvider) AbstractResourceProvider.getResourceProvider( + Resource.Type.Service, + propertyIds, + keyPropertyIds, + managementController); + + Set<String> supportedPropertyIds = provider.getPropertyIds(); + Assert.assertTrue(supportedPropertyIds.containsAll(propertyIds)); + } + + + // ----- helper methods ---------------------------------------------------- + + public static class Matchers + { + public static ClusterRequest clusterRequest(Long clusterId, String clusterName, String stackVersion, Set<String> hostNames) + { + EasyMock.reportMatcher(new ClusterRequestMatcher(clusterId, clusterName, stackVersion, hostNames)); + return null; + } + + public static ServiceRequest serviceRequest(String clusterName, String serviceName, Map<String, String> configVersions, String desiredState) + { + EasyMock.reportMatcher(new ServiceRequestMatcher(clusterName, serviceName, configVersions, desiredState)); + return null; + } + } + + public static boolean eq(Object left, Object right) { + return left == null ? right == null : right != null && left.equals(right); + } + + + // ----- inner classes ----------------------------------------------------- + + public static class ClusterRequestMatcher extends ClusterRequest implements IArgumentMatcher { + + public ClusterRequestMatcher(Long clusterId, String clusterName, String stackVersion, Set<String> hostNames) { + super(clusterId, clusterName, stackVersion, hostNames); + } + + @Override + public boolean matches(Object o) { + return o instanceof ClusterRequest && + eq(((ClusterRequest) o).getClusterId(), getClusterId()) && + eq(((ClusterRequest) o).getClusterName(), getClusterName()) && + eq(((ClusterRequest) o).getStackVersion(), getStackVersion()) && + eq(((ClusterRequest) o).getHostNames(), getHostNames()); + } + + @Override + public void appendTo(StringBuffer stringBuffer) { + stringBuffer.append("ClusterRequestMatcher(" + "" + ")"); + } + } + + public static class ServiceRequestMatcher extends ServiceRequest implements IArgumentMatcher { + + public ServiceRequestMatcher(String clusterName, String serviceName, Map<String, String> configVersions, String desiredState) { + super(clusterName, serviceName, configVersions, desiredState); + } + + @Override + public boolean matches(Object o) { + return o instanceof ServiceRequest && + eq(((ServiceRequest) o).getClusterName(), getClusterName()) && + eq(((ServiceRequest) o).getServiceName(), getServiceName()) && + eq(((ServiceRequest) o).getConfigVersions(), getConfigVersions()) && + eq(((ServiceRequest) o).getDesiredState(), getDesiredState()); + + } + + @Override + public void appendTo(StringBuffer stringBuffer) { + stringBuffer.append("ClusterRequestMatcher(" + "" + ")"); + } + } + + public class TestObserver implements ResourceProviderObserver { + + ResourceProviderEvent lastEvent = null; + + @Override + public void update(ResourceProviderEvent event) { + lastEvent = event; + } + + public ResourceProviderEvent getLastEvent() { + return lastEvent; + } + } + +}
Added: incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/BaseProviderTest.java URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/BaseProviderTest.java?rev=1440042&view=auto ============================================================================== --- incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/BaseProviderTest.java (added) +++ incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/BaseProviderTest.java Tue Jan 29 18:15:16 2013 @@ -0,0 +1,261 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ambari.server.controller.internal; + +import org.apache.ambari.server.controller.spi.Request; +import org.apache.ambari.server.controller.spi.Resource; +import org.apache.ambari.server.controller.utilities.PropertyHelper; +import org.junit.Assert; +import org.junit.Test; + +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +/** + * Base provider tests. + */ +public class BaseProviderTest { + @Test + public void testGetProperties() { + Set<String> propertyIds = new HashSet<String>(); + propertyIds.add("foo"); + propertyIds.add("bar"); + propertyIds.add("cat1/prop1"); + propertyIds.add("cat2/prop2"); + propertyIds.add("cat3/subcat3/prop3"); + + BaseProvider provider = new TestProvider(propertyIds); + + Set<String> supportedPropertyIds = provider.getPropertyIds(); + Assert.assertTrue(supportedPropertyIds.containsAll(propertyIds)); + } + + @Test + public void testCheckPropertyIds() { + Set<String> propertyIds = new HashSet<String>(); + propertyIds.add("foo"); + propertyIds.add("bar"); + propertyIds.add("cat1/prop1"); + propertyIds.add("cat2/prop2"); + propertyIds.add("cat3/subcat3/prop3"); + propertyIds.add("cat4/subcat4/map"); + + BaseProvider provider = new TestProvider(propertyIds); + + Assert.assertTrue(provider.checkPropertyIds(propertyIds).isEmpty()); + + Assert.assertTrue(provider.checkPropertyIds(Collections.singleton("cat1")).isEmpty()); + Assert.assertTrue(provider.checkPropertyIds(Collections.singleton("cat2")).isEmpty()); + Assert.assertTrue(provider.checkPropertyIds(Collections.singleton("cat3")).isEmpty()); + Assert.assertTrue(provider.checkPropertyIds(Collections.singleton("cat3/subcat3")).isEmpty()); + Assert.assertTrue(provider.checkPropertyIds(Collections.singleton("cat4/subcat4/map")).isEmpty()); + + // note that key is not in the set of known property ids. We allow it if its parent is a known property. + // this allows for Map type properties where we want to treat the entries as individual properties + Assert.assertTrue(provider.checkPropertyIds(Collections.singleton("cat4/subcat4/map/key")).isEmpty()); + + propertyIds.add("badprop"); + propertyIds.add("badcat"); + + Set<String> unsupportedPropertyIds = provider.checkPropertyIds(propertyIds); + Assert.assertFalse(unsupportedPropertyIds.isEmpty()); + Assert.assertEquals(2, unsupportedPropertyIds.size()); + Assert.assertTrue(unsupportedPropertyIds.contains("badprop")); + Assert.assertTrue(unsupportedPropertyIds.contains("badcat")); + } + + @Test + public void testGetRequestPropertyIds() { + Set<String> providerPropertyIds = new HashSet<String>(); + providerPropertyIds.add("foo"); + providerPropertyIds.add("bar"); + providerPropertyIds.add("cat1/sub1"); + + BaseProvider provider = new TestProvider(providerPropertyIds); + + Request request = PropertyHelper.getReadRequest("foo"); + + Set<String> requestedPropertyIds = provider.getRequestPropertyIds(request, null); + + Assert.assertEquals(1, requestedPropertyIds.size()); + Assert.assertTrue(requestedPropertyIds.contains("foo")); + + request = PropertyHelper.getReadRequest("foo", "bar"); + + requestedPropertyIds = provider.getRequestPropertyIds(request, null); + + Assert.assertEquals(2, requestedPropertyIds.size()); + Assert.assertTrue(requestedPropertyIds.contains("foo")); + Assert.assertTrue(requestedPropertyIds.contains("bar")); + + request = PropertyHelper.getReadRequest("foo", "baz", "bar", "cat", "cat1/prop1"); + + requestedPropertyIds = provider.getRequestPropertyIds(request, null); + + Assert.assertEquals(2, requestedPropertyIds.size()); + Assert.assertTrue(requestedPropertyIds.contains("foo")); + Assert.assertTrue(requestedPropertyIds.contains("bar")); + + // ask for a property that isn't specified as supported, but its category is... the property + // should end up in the returned set for the case where the category is a Map property + request = PropertyHelper.getReadRequest("foo", "cat1/sub1/prop1"); + + requestedPropertyIds = provider.getRequestPropertyIds(request, null); + + Assert.assertEquals(2, requestedPropertyIds.size()); + Assert.assertTrue(requestedPropertyIds.contains("foo")); + Assert.assertTrue(requestedPropertyIds.contains("cat1/sub1/prop1")); + } + + @Test + public void testSetResourceProperty() { + Set<String> propertyIds = new HashSet<String>(); + propertyIds.add("p1"); + propertyIds.add("foo"); + propertyIds.add("cat1/foo"); + propertyIds.add("cat2/bar"); + propertyIds.add("cat2/baz"); + propertyIds.add("cat3/sub1/bam"); + propertyIds.add("cat4/sub2/sub3/bat"); + propertyIds.add("cat5/sub5"); + + Resource resource = new ResourceImpl(Resource.Type.Service); + + Assert.assertNull(resource.getPropertyValue("foo")); + + BaseProvider.setResourceProperty(resource, "foo", "value1", propertyIds); + Assert.assertEquals("value1", resource.getPropertyValue("foo")); + + BaseProvider.setResourceProperty(resource, "cat2/bar", "value2", propertyIds); + Assert.assertEquals("value2", resource.getPropertyValue("cat2/bar")); + + Assert.assertNull(resource.getPropertyValue("unsupported")); + BaseProvider.setResourceProperty(resource, "unsupported", "valueX", propertyIds); + Assert.assertNull(resource.getPropertyValue("unsupported")); + + // we should allow anything under the category cat5/sub5 + BaseProvider.setResourceProperty(resource, "cat5/sub5/prop5", "value5", propertyIds); + Assert.assertEquals("value5", resource.getPropertyValue("cat5/sub5/prop5")); + BaseProvider.setResourceProperty(resource, "cat5/sub5/sub5a/prop5a", "value5", propertyIds); + Assert.assertEquals("value5", resource.getPropertyValue("cat5/sub5/sub5a/prop5a")); + // we shouldn't allow anything under the category cat5/sub7 + BaseProvider.setResourceProperty(resource, "cat5/sub7/unsupported", "valueX", propertyIds); + Assert.assertNull(resource.getPropertyValue("cat5/sub7/unsupported")); + } + + @Test + public void testSetResourcePropertyWithMaps() { + Set<String> propertyIds = new HashSet<String>(); + propertyIds.add("cat1/emptyMapProperty"); + propertyIds.add("cat1/mapProperty"); + propertyIds.add("cat2/mapMapProperty"); + propertyIds.add("cat3/mapProperty3/key2"); + propertyIds.add("cat4/mapMapProperty4/subMap1/key3"); + propertyIds.add("cat4/mapMapProperty4/subMap2"); + + Resource resource = new ResourceImpl(Resource.Type.Service); + + // Adding an empty Map as a property should add the actual Map as a property + Map<String, String> emptyMapProperty = new HashMap<String, String>(); + BaseProvider.setResourceProperty(resource, "cat1/emptyMapProperty", emptyMapProperty, propertyIds); + Assert.assertTrue(resource.getPropertiesMap().containsKey("cat1/emptyMapProperty")); + + Map<String, String> mapProperty = new HashMap<String, String>(); + mapProperty.put("key1", "value1"); + mapProperty.put("key2", "value2"); + mapProperty.put("key3", "value3"); + + // Adding a property of type Map should add all of its keys as sub properties + // if the map property was requested + BaseProvider.setResourceProperty(resource, "cat1/mapProperty", mapProperty, propertyIds); + Assert.assertNull(resource.getPropertyValue("cat1/mapProperty")); + Assert.assertEquals("value1", resource.getPropertyValue("cat1/mapProperty/key1")); + Assert.assertEquals("value2", resource.getPropertyValue("cat1/mapProperty/key2")); + Assert.assertEquals("value3", resource.getPropertyValue("cat1/mapProperty/key3")); + + Map<String, Map<String, String>> mapMapProperty = new HashMap<String, Map<String, String>>(); + Map<String, String> mapSubProperty1 = new HashMap<String, String>(); + mapSubProperty1.put("key1", "value11"); + mapSubProperty1.put("key2", "value12"); + mapSubProperty1.put("key3", "value13"); + mapMapProperty.put("subMap1", mapSubProperty1); + Map<String, String> mapSubProperty2 = new HashMap<String, String>(); + mapSubProperty2.put("key1", "value21"); + mapSubProperty2.put("key2", "value22"); + mapSubProperty2.put("key3", "value23"); + mapMapProperty.put("subMap2", mapSubProperty2); + Map<String, String> mapSubProperty3 = new HashMap<String, String>(); + mapMapProperty.put("subMap3", mapSubProperty3); + + // Map of maps ... adding a property of type Map should add all of its keys as sub properties + // if the map property was requested + BaseProvider.setResourceProperty(resource, "cat2/mapMapProperty", mapMapProperty, propertyIds); + Assert.assertNull(resource.getPropertyValue("cat2/mapMapProperty")); + Assert.assertNull(resource.getPropertyValue("cat2/mapMapProperty/subMap1")); + Assert.assertNull(resource.getPropertyValue("cat2/mapMapProperty/subMap2")); + Assert.assertTrue(resource.getPropertiesMap().containsKey("cat2/mapMapProperty/subMap3")); + Assert.assertEquals("value11", resource.getPropertyValue("cat2/mapMapProperty/subMap1/key1")); + Assert.assertEquals("value12", resource.getPropertyValue("cat2/mapMapProperty/subMap1/key2")); + Assert.assertEquals("value13", resource.getPropertyValue("cat2/mapMapProperty/subMap1/key3")); + Assert.assertEquals("value21", resource.getPropertyValue("cat2/mapMapProperty/subMap2/key1")); + Assert.assertEquals("value22", resource.getPropertyValue("cat2/mapMapProperty/subMap2/key2")); + Assert.assertEquals("value23", resource.getPropertyValue("cat2/mapMapProperty/subMap2/key3")); + + Map<String, String> mapProperty3 = new HashMap<String, String>(); + mapProperty3.put("key1", "value1"); + mapProperty3.put("key2", "value2"); + mapProperty3.put("key3", "value3"); + + // Adding a property of type Map shouldn't add the map if it wasn't requested and + // should only add requested keys as sub properties ... + // only "cat3/mapProperty3/key2" was requested + BaseProvider.setResourceProperty(resource, "cat3/mapProperty3", mapProperty3, propertyIds); + Assert.assertNull(resource.getPropertyValue("cat3/mapProperty3")); + Assert.assertNull(resource.getPropertyValue("cat3/mapProperty3/key1")); + Assert.assertEquals("value2", resource.getPropertyValue("cat3/mapProperty3/key2")); + Assert.assertNull(resource.getPropertyValue("cat3/mapProperty3/key3")); + + Map<String, Map<String, String>> mapMapProperty4 = new HashMap<String, Map<String, String>>(); + mapMapProperty4.put("subMap1", mapSubProperty1); + mapMapProperty4.put("subMap2", mapSubProperty2); + // Map of maps ... adding a property of type Map shouldn't add the map if it wasn't requested and + // should only add requested keys as sub properties ... + // only "cat4/mapMapProperty4/subMap1/key3" and "cat4/mapMapProperty4/subMap2" are requested + BaseProvider.setResourceProperty(resource, "cat4/mapMapProperty4", mapMapProperty4, propertyIds); + Assert.assertNull(resource.getPropertyValue("cat4/mapMapProperty4")); + Assert.assertNull(resource.getPropertyValue("cat4/mapMapProperty4/subMap1")); + Assert.assertNull(resource.getPropertyValue("cat4/mapMapProperty4/subMap2")); + Assert.assertNull(resource.getPropertyValue("cat4/mapMapProperty4/subMap1/key1")); + Assert.assertNull(resource.getPropertyValue("cat4/mapMapProperty4/subMap1/key2")); + Assert.assertEquals("value13", resource.getPropertyValue("cat4/mapMapProperty4/subMap1/key3")); + Assert.assertEquals("value21", resource.getPropertyValue("cat4/mapMapProperty4/subMap2/key1")); + Assert.assertEquals("value22", resource.getPropertyValue("cat4/mapMapProperty4/subMap2/key2")); + Assert.assertEquals("value23", resource.getPropertyValue("cat4/mapMapProperty4/subMap2/key3")); + } + + static class TestProvider extends BaseProvider { + + public TestProvider(Set<String> propertyIds) { + super(propertyIds); + } + } +} Modified: incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ClusterControllerImplTest.java URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ClusterControllerImplTest.java?rev=1440042&r1=1440041&r2=1440042&view=diff ============================================================================== --- incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ClusterControllerImplTest.java (original) +++ incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ClusterControllerImplTest.java Tue Jan 29 18:15:16 2013 @@ -66,11 +66,6 @@ public class ClusterControllerImplTest { } @Override - public Set<String> getPropertyIds() { - return propertyProviderProperties; - } - - @Override public Set<String> checkPropertyIds(Set<String> propertyIds) { if (!propertyProviderProperties.containsAll(propertyIds)) { Set<String> unsupportedPropertyIds = new HashSet<String>(propertyIds); @@ -402,39 +397,39 @@ public class ClusterControllerImplTest { Assert.assertTrue(keyPropertyIds.containsAll(predicatePropertyIds)); } - @Test - public void testGetSchema() { - ProviderModule module = new TestProviderModule(); - - ClusterController controller = new ClusterControllerImpl(module); - Schema schema = controller.getSchema(Resource.Type.Host); - - ResourceProvider resourceProvider = module.getResourceProvider(Resource.Type.Host); - - Map<Resource.Type, String> keyPropertyIds = resourceProvider.getKeyPropertyIds(); - for (Map.Entry<Resource.Type, String> entry : keyPropertyIds.entrySet()) { - Assert.assertEquals(entry.getValue(), schema.getKeyPropertyId(entry.getKey())); - } - - Map<String, Set<String>> categories = schema.getCategoryProperties(); - for (String propertyId : resourceProvider.getPropertyIdsForSchema()) { - String category = PropertyHelper.getPropertyCategory(propertyId); - Set<String> properties = categories.get(category); - Assert.assertNotNull(properties); - Assert.assertTrue(properties.contains(PropertyHelper.getPropertyName(propertyId))); - } - - List<PropertyProvider> propertyProviders = module.getPropertyProviders(Resource.Type.Host); - - for (PropertyProvider propertyProvider : propertyProviders) { - for (String propertyId : propertyProvider.getPropertyIds()) { - String category = PropertyHelper.getPropertyCategory(propertyId); - Set<String> properties = categories.get(category); - Assert.assertNotNull(properties); - Assert.assertTrue(properties.contains(PropertyHelper.getPropertyName(propertyId))); - } - } - } +// @Test +// public void testGetSchema() { +// ProviderModule module = new TestProviderModule(); +// +// ClusterController controller = new ClusterControllerImpl(module); +// Schema schema = controller.getSchema(Resource.Type.Host); +// +// ResourceProvider resourceProvider = module.getResourceProvider(Resource.Type.Host); +// +// Map<Resource.Type, String> keyPropertyIds = resourceProvider.getKeyPropertyIds(); +// for (Map.Entry<Resource.Type, String> entry : keyPropertyIds.entrySet()) { +// Assert.assertEquals(entry.getValue(), schema.getKeyPropertyId(entry.getKey())); +// } +// +// Map<String, Set<String>> categories = schema.getCategoryProperties(); +// for (String propertyId : resourceProvider.getPropertyIdsForSchema()) { +// String category = PropertyHelper.getPropertyCategory(propertyId); +// Set<String> properties = categories.get(category); +// Assert.assertNotNull(properties); +// Assert.assertTrue(properties.contains(PropertyHelper.getPropertyName(propertyId))); +// } +// +// List<PropertyProvider> propertyProviders = module.getPropertyProviders(Resource.Type.Host); +// +// for (PropertyProvider propertyProvider : propertyProviders) { +// for (String propertyId : propertyProvider.getPropertyIds()) { +// String category = PropertyHelper.getPropertyCategory(propertyId); +// Set<String> properties = categories.get(category); +// Assert.assertNotNull(properties); +// Assert.assertTrue(properties.contains(PropertyHelper.getPropertyName(propertyId))); +// } +// } +// } private static class TestProviderModule implements ProviderModule { private Map<Resource.Type, ResourceProvider> providers = new HashMap<Resource.Type, ResourceProvider>(); @@ -506,11 +501,6 @@ public class ClusterControllerImplTest { } @Override - public Set<String> getPropertyIdsForSchema() { - return resourceProviderProperties; - } - - @Override public Set<String> checkPropertyIds(Set<String> propertyIds) { if (!resourceProviderProperties.containsAll(propertyIds)) { Set<String> unsupportedPropertyIds = new HashSet<String>(propertyIds); Modified: incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ResourceImplTest.java URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ResourceImplTest.java?rev=1440042&r1=1440041&r2=1440042&view=diff ============================================================================== --- incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ResourceImplTest.java (original) +++ incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ResourceImplTest.java Tue Jan 29 18:15:16 2013 @@ -68,6 +68,27 @@ public class ResourceImplTest { } @Test + public void testAddCategory() { + Resource resource = new ResourceImpl(Resource.Type.Cluster); + + resource.addCategory("c1"); + Assert.assertTrue(resource.getPropertiesMap().containsKey("c1")); + + resource.addCategory("c2/sub2"); + Assert.assertTrue(resource.getPropertiesMap().containsKey("c1")); + Assert.assertTrue(resource.getPropertiesMap().containsKey("c2")); + Assert.assertTrue(resource.getPropertiesMap().containsKey("c2/sub2")); + + resource.addCategory("c3/sub3/sub3a"); + Assert.assertTrue(resource.getPropertiesMap().containsKey("c1")); + Assert.assertTrue(resource.getPropertiesMap().containsKey("c2")); + Assert.assertTrue(resource.getPropertiesMap().containsKey("c2/sub2")); + Assert.assertTrue(resource.getPropertiesMap().containsKey("c3")); + Assert.assertTrue(resource.getPropertiesMap().containsKey("c3/sub3")); + Assert.assertTrue(resource.getPropertiesMap().containsKey("c3/sub3/sub3a")); + } + + @Test public void testCopyConstructor() { Resource resource = new ResourceImpl(Resource.Type.Cluster); Modified: incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/SchemaImplTest.java URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/SchemaImplTest.java?rev=1440042&r1=1440041&r2=1440042&view=diff ============================================================================== --- incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/SchemaImplTest.java (original) +++ incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/SchemaImplTest.java Tue Jan 29 18:15:16 2013 @@ -68,11 +68,6 @@ public class SchemaImplTest { } @Override - public Set<String> getPropertyIdsForSchema() { - return resourceProviderProperties; - } - - @Override public Map<Resource.Type, String> getKeyPropertyIds() { return keyPropertyIds; } @@ -104,11 +99,6 @@ public class SchemaImplTest { } @Override - public Set<String> getPropertyIds() { - return propertyProviderProperties; - } - - @Override public Set<String> checkPropertyIds(Set<String> propertyIds) { if (!propertyProviderProperties.containsAll(propertyIds)) { Set<String> unsupportedPropertyIds = new HashSet<String>(propertyIds); @@ -135,42 +125,42 @@ public class SchemaImplTest { @Test public void testGetKeyPropertyId() { - Schema schema = new SchemaImpl(resourceProvider, propertyProviders); + Schema schema = new SchemaImpl(resourceProvider); Assert.assertEquals(PropertyHelper.getPropertyId("c1", "p1"), schema.getKeyPropertyId(Resource.Type.Cluster)); Assert.assertEquals(PropertyHelper.getPropertyId("c1", "p2"), schema.getKeyPropertyId(Resource.Type.Host)); Assert.assertEquals(PropertyHelper.getPropertyId("c1", "p3"), schema.getKeyPropertyId(Resource.Type.Component)); } - @Test - public void testGetCategories() { - Schema schema = new SchemaImpl(resourceProvider, propertyProviders); - - Map<String, Set<String>> categories = schema.getCategoryProperties(); - Assert.assertEquals(4, categories.size()); - Assert.assertTrue(categories.containsKey("c1")); - Assert.assertTrue(categories.containsKey("c2")); - Assert.assertTrue(categories.containsKey("c3")); - Assert.assertTrue(categories.containsKey("c4")); - - Set<String> properties = categories.get("c1"); - Assert.assertEquals(3, properties.size()); - Assert.assertTrue(properties.contains("p1")); - Assert.assertTrue(properties.contains("p2")); - Assert.assertTrue(properties.contains("p3")); - - properties = categories.get("c2"); - Assert.assertEquals(1, properties.size()); - Assert.assertTrue(properties.contains("p4")); - - properties = categories.get("c3"); - Assert.assertEquals(2, properties.size()); - Assert.assertTrue(properties.contains("p5")); - Assert.assertTrue(properties.contains("p6")); - - properties = categories.get("c4"); - Assert.assertEquals(2, properties.size()); - Assert.assertTrue(properties.contains("p7")); - Assert.assertTrue(properties.contains("p8")); - } +// @Test +// public void testGetCategories() { +// Schema schema = new SchemaImpl(resourceProvider); +// +// Map<String, Set<String>> categories = schema.getCategoryProperties(); +// Assert.assertEquals(4, categories.size()); +// Assert.assertTrue(categories.containsKey("c1")); +// Assert.assertTrue(categories.containsKey("c2")); +// Assert.assertTrue(categories.containsKey("c3")); +// Assert.assertTrue(categories.containsKey("c4")); +// +// Set<String> properties = categories.get("c1"); +// Assert.assertEquals(3, properties.size()); +// Assert.assertTrue(properties.contains("p1")); +// Assert.assertTrue(properties.contains("p2")); +// Assert.assertTrue(properties.contains("p3")); +// +// properties = categories.get("c2"); +// Assert.assertEquals(1, properties.size()); +// Assert.assertTrue(properties.contains("p4")); +// +// properties = categories.get("c3"); +// Assert.assertEquals(2, properties.size()); +// Assert.assertTrue(properties.contains("p5")); +// Assert.assertTrue(properties.contains("p6")); +// +// properties = categories.get("c4"); +// Assert.assertEquals(2, properties.size()); +// Assert.assertTrue(properties.contains("p7")); +// Assert.assertTrue(properties.contains("p8")); +// } } Modified: incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/controller/utilities/PropertyHelperTest.java URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/controller/utilities/PropertyHelperTest.java?rev=1440042&r1=1440041&r2=1440042&view=diff ============================================================================== --- incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/controller/utilities/PropertyHelperTest.java (original) +++ incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/controller/utilities/PropertyHelperTest.java Tue Jan 29 18:15:16 2013 @@ -17,159 +17,31 @@ */ package org.apache.ambari.server.controller.utilities; -//import org.apache.ambari.server.controller.spi.Resource; -//import org.codehaus.jackson.map.ObjectMapper; -//import org.codehaus.jackson.type.TypeReference; -//import org.junit.Ignore; -//import org.junit.Test; -// -//import java.util.Collections; -//import java.util.Map; -//import java.util.TreeMap; +import org.apache.ambari.server.controller.spi.Request; +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashSet; +import java.util.Set; /** - * + * Property helper tests. */ public class PropertyHelperTest { -// private static final String JMX_PROPERTIES_FILE = "jmx_properties.json"; -// private static final String GANGLIA_PROPERTIES_FILE = "ganglia_properties.json"; -// -// @Ignore -// @Test -// public void testGetGPropertyIds() throws Exception { -// ObjectMapper mapper = new ObjectMapper(); -// -// Map<Resource.Type, Map<String, Map<String, PropertyHelper.Metric>>> gangliaMetrics = -// mapper.readValue(ClassLoader.getSystemResourceAsStream(GANGLIA_PROPERTIES_FILE), -// new TypeReference<Map<Resource.Type, Map<String, Map<String, PropertyHelper.Metric>>>>() {}); -// -// Map<Resource.Type, Map<String, Map<String, PropertyHelper.Metric>>> jmxMetrics = -// mapper.readValue(ClassLoader.getSystemResourceAsStream(JMX_PROPERTIES_FILE), -// new TypeReference<Map<Resource.Type, Map<String, Map<String, PropertyHelper.Metric>>>>() {}); -// -// System.out.println("{\n"); -// -// for (Resource.Type type : Resource.Type.values()) { -// -// Map<String, Map<String, PropertyHelper.Metric>> gMap = gangliaMetrics.get(type); -// -// if (gMap == null) { -// continue; -// } -// -// System.out.println(" \"" + type + "\":{\n"); -// -// Map<String, Map<String, PropertyHelper.Metric>> jMap = jmxMetrics.get(type); -// -// -// for (Map.Entry<String, Map<String, PropertyHelper.Metric>> entry: gMap.entrySet()) { -// String componentName = entry.getKey(); -// -// System.out.println(" \"" + componentName + "\":{\n"); -// -// -// Map<String, PropertyHelper.Metric> gMetricMap = entry.getValue(); -// Map<String, PropertyHelper.Metric> jMetricMap = jMap == null ? -// Collections.<String, PropertyHelper.Metric>emptyMap() : jMap.get(componentName); -// -// Map<String, PropertyHelper.Metric> newGMetricMap = new TreeMap<String, PropertyHelper.Metric>(); -// -// for (Map.Entry<String, PropertyHelper.Metric> metricEntry : gMetricMap.entrySet()) { -// -// String metricName = metricEntry.getKey(); -// PropertyHelper.Metric gMetric = metricEntry.getValue(); -// PropertyHelper.Metric jMetric = jMetricMap == null ? null : jMetricMap.get(metricName); -// -// boolean jmxPointInTime = jMetric != null && jMetric.isPointInTime(); -// -// newGMetricMap.put(metricName, new PropertyHelper.Metric(gMetric.getMetric(), -// jmxPointInTime ? false : gMetric.isPointInTime(), -// gMetric.isTemporal())); -// } -// -// for (Map.Entry<String, PropertyHelper.Metric> metricEntry : newGMetricMap.entrySet()) { -// String metricName = metricEntry.getKey(); -// PropertyHelper.Metric gMetric = metricEntry.getValue(); -// -// -// System.out.println(" \"" + metricName + "\":{\n" + -// " \"metric\" : \"" + gMetric.getMetric() + "\",\n" + -// " \"pointInTime\" : " + gMetric.isPointInTime() + ",\n" + -// " \"temporal\" : " + gMetric.isTemporal() + "\n" + -// " },"); -// -// } -// -// System.out.println(" },\n"); -// } -// System.out.println(" },\n"); -// } -// System.out.println("},\n"); -// } -// -// @Ignore -// @Test -// public void testGetJPropertyIds() throws Exception { -// ObjectMapper mapper = new ObjectMapper(); -// -// -// Map<Resource.Type, Map<String, Map<String, PropertyHelper.Metric>>> jmxMetrics = -// mapper.readValue(ClassLoader.getSystemResourceAsStream(JMX_PROPERTIES_FILE), -// new TypeReference<Map<Resource.Type, Map<String, Map<String, PropertyHelper.Metric>>>>() {}); -// -// System.out.println("{\n"); -// -// for (Resource.Type type : Resource.Type.values()) { -// -// Map<String, Map<String, PropertyHelper.Metric>> jMap = jmxMetrics.get(type); -// -// if (jMap == null) { -// continue; -// } -// -// System.out.println(" \"" + type + "\":{\n"); -// -// -// for (Map.Entry<String, Map<String, PropertyHelper.Metric>> entry: jMap.entrySet()) { -// String componentName = entry.getKey(); -// -// System.out.println(" \"" + componentName + "\":{\n"); -// -// -// Map<String, PropertyHelper.Metric> jMetricMap = entry.getValue(); -// -// Map<String, PropertyHelper.Metric> newJMetricMap = new TreeMap<String, PropertyHelper.Metric>(); -// -// for (Map.Entry<String, PropertyHelper.Metric> metricEntry : jMetricMap.entrySet()) { -// -// String metricName = metricEntry.getKey(); -// PropertyHelper.Metric jMetric = metricEntry.getValue(); -// -// -// newJMetricMap.put(metricName, new PropertyHelper.Metric(jMetric.getMetric(), -// jMetric.isPointInTime(), -// jMetric.isTemporal())); -// } -// -// for (Map.Entry<String, PropertyHelper.Metric> metricEntry : newJMetricMap.entrySet()) { -// String metricName = metricEntry.getKey(); -// PropertyHelper.Metric jMetric = metricEntry.getValue(); -// -// -// System.out.println(" \"" + metricName + "\":{\n" + -// " \"metric\" : \"" + jMetric.getMetric() + "\",\n" + -// " \"pointInTime\" : " + jMetric.isPointInTime() + ",\n" + -// " \"temporal\" : " + jMetric.isTemporal() + "\n" + -// " },"); -// -// } -// -// System.out.println(" },\n"); -// } -// System.out.println(" },\n"); -// } -// System.out.println("},\n"); -// } + @Test + public void testGetPropertyId() { + Assert.assertEquals("foo", PropertyHelper.getPropertyId("", "foo")); + Assert.assertEquals("foo", PropertyHelper.getPropertyId(null, "foo")); + Assert.assertEquals("foo", PropertyHelper.getPropertyId(null, "foo/")); + + Assert.assertEquals("cat", PropertyHelper.getPropertyId("cat", "")); + Assert.assertEquals("cat", PropertyHelper.getPropertyId("cat", null)); + Assert.assertEquals("cat", PropertyHelper.getPropertyId("cat/", null)); + + Assert.assertEquals("cat/foo", PropertyHelper.getPropertyId("cat", "foo")); + Assert.assertEquals("cat/sub/foo", PropertyHelper.getPropertyId("cat/sub", "foo")); + Assert.assertEquals("cat/sub/foo", PropertyHelper.getPropertyId("cat/sub", "foo/")); + } }
