Repository: ambari Updated Branches: refs/heads/trunk 82bbd3ea6 -> cb6765fae
AMBARI-5546 Call for requests with 'page_size' always return 10 most recent (dsen) Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/cb6765fa Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/cb6765fa Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/cb6765fa Branch: refs/heads/trunk Commit: cb6765fae3d149b4918d60b166dade4f1f1bd178 Parents: 82bbd3e Author: Dmitry Sen <[email protected]> Authored: Thu Apr 24 17:12:07 2014 +0300 Committer: Dmitry Sen <[email protected]> Committed: Thu Apr 24 17:12:32 2014 +0300 ---------------------------------------------------------------------- .../server/actionmanager/ActionDBAccessor.java | 20 +++--- .../actionmanager/ActionDBAccessorImpl.java | 12 ++-- .../server/actionmanager/ActionManager.java | 25 ++++--- .../ambari/server/api/query/QueryImpl.java | 20 +++++- .../ambari/server/api/services/BaseRequest.java | 12 +++- .../internal/RequestResourceProvider.java | 23 +++++-- .../controller/utilities/PropertyHelper.java | 13 ++++ .../server/orm/dao/HostRoleCommandDAO.java | 21 ++---- .../actionmanager/TestActionDBAccessorImpl.java | 43 +++++++++++- .../server/actionmanager/TestActionManager.java | 5 +- .../internal/RequestResourceProviderTest.java | 72 ++++++++++++++++++++ 11 files changed, 211 insertions(+), 55 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/cb6765fa/ambari-server/src/main/java/org/apache/ambari/server/actionmanager/ActionDBAccessor.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/actionmanager/ActionDBAccessor.java b/ambari-server/src/main/java/org/apache/ambari/server/actionmanager/ActionDBAccessor.java index f83adaf..86ebecf 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/actionmanager/ActionDBAccessor.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/actionmanager/ActionDBAccessor.java @@ -142,19 +142,23 @@ public interface ActionDBAccessor { public List<Stage> getStagesByHostRoleStatus(Set<HostRoleStatus> statuses); /** - * Get all requests - */ - public List<Long> getRequestIds(); - - /** * Gets the host role command corresponding to the task id */ public HostRoleCommand getTask(long taskId); /** - * Gets request id of request that are in the specified status + * Get first or last maxResults requests that are in the specified status + * + * @param status + * Desired request status + * @param maxResults + * maximal number of returned id's + * @param ascOrder + * defines sorting order for database query result + * @return First or last maxResults request id's if ascOrder is true or false, + * respectively */ - public List<Long> getRequestsByStatus(RequestStatus status); + public List<Long> getRequestsByStatus(RequestStatus status, int maxResults, boolean ascOrder); /** * Gets request contexts associated with the list of request id @@ -169,5 +173,5 @@ public interface ActionDBAccessor { /** * Gets request objects by ids */ - List<Request> getRequests(Collection<Long> requestIds); + public List<Request> getRequests(Collection<Long> requestIds); } http://git-wip-us.apache.org/repos/asf/ambari/blob/cb6765fa/ambari-server/src/main/java/org/apache/ambari/server/actionmanager/ActionDBAccessorImpl.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/actionmanager/ActionDBAccessorImpl.java b/ambari-server/src/main/java/org/apache/ambari/server/actionmanager/ActionDBAccessorImpl.java index 07e6d5f..2104bd6 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/actionmanager/ActionDBAccessorImpl.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/actionmanager/ActionDBAccessorImpl.java @@ -508,11 +508,6 @@ public class ActionDBAccessorImpl implements ActionDBAccessor { return stages; } - @Override - public List<Long> getRequestIds() { - return hostRoleCommandDAO.getRequests(); - } - public HostRoleCommand getTask(long taskId) { HostRoleCommandEntity commandEntity = hostRoleCommandDAO.findByPK((int) taskId); if (commandEntity == null) { @@ -522,7 +517,9 @@ public class ActionDBAccessorImpl implements ActionDBAccessor { } @Override - public List<Long> getRequestsByStatus(RequestStatus status) { + public List<Long> getRequestsByStatus(RequestStatus status, int maxResults, + boolean ascOrder) { + boolean match = true; boolean checkAllTasks = false; Set<HostRoleStatus> statuses = new HashSet<HostRoleStatus>(); @@ -540,7 +537,8 @@ public class ActionDBAccessorImpl implements ActionDBAccessor { statuses.addAll(Arrays.asList(HostRoleStatus.ABORTED, HostRoleStatus.FAILED, HostRoleStatus.TIMEDOUT)); } - return hostRoleCommandDAO.getRequestsByTaskStatus(statuses, match, checkAllTasks); + return hostRoleCommandDAO.getRequestsByTaskStatus(statuses, match, + checkAllTasks, maxResults, ascOrder); } @Override http://git-wip-us.apache.org/repos/asf/ambari/blob/cb6765fa/ambari-server/src/main/java/org/apache/ambari/server/actionmanager/ActionManager.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/actionmanager/ActionManager.java b/ambari-server/src/main/java/org/apache/ambari/server/actionmanager/ActionManager.java index 7b9a922..4bb9d1d 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/actionmanager/ActionManager.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/actionmanager/ActionManager.java @@ -24,6 +24,7 @@ import com.google.inject.persist.UnitOfWork; import org.apache.ambari.server.AmbariException; import org.apache.ambari.server.agent.ActionQueue; import org.apache.ambari.server.agent.CommandReport; +import org.apache.ambari.server.api.services.BaseRequest; import org.apache.ambari.server.configuration.Configuration; import org.apache.ambari.server.controller.ExecuteActionRequest; import org.apache.ambari.server.controller.HostsMap; @@ -196,21 +197,19 @@ public class ActionManager { } /** - * Returns last 20 requests + * Get first or last maxResults requests that are in the specified status * - * @return - */ - public List<Long> getRequests() { - return db.getRequestIds(); - } - - /** - * Returns last 20 requests - * - * @return + * @param status + * Desired request status + * @param maxResults + * maximal number of returned id's + * @param ascOrder + * defines sorting order for database query result + * @return First or last maxResults request id's if ascOrder is true or false, + * respectively */ - public List<Long> getRequestsByStatus(RequestStatus status) { - return db.getRequestsByStatus(status); + public List<Long> getRequestsByStatus(RequestStatus status, int maxResults, boolean ascOrder) { + return db.getRequestsByStatus(status, maxResults, ascOrder); } public Map<Long, String> getRequestContext(List<Long> requestIds) { http://git-wip-us.apache.org/repos/asf/ambari/blob/cb6765fa/ambari-server/src/main/java/org/apache/ambari/server/api/query/QueryImpl.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/api/query/QueryImpl.java b/ambari-server/src/main/java/org/apache/ambari/server/api/query/QueryImpl.java index 0a0822c..0dda747 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/api/query/QueryImpl.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/api/query/QueryImpl.java @@ -24,6 +24,7 @@ import org.apache.ambari.server.api.resources.ResourceDefinition; import org.apache.ambari.server.api.resources.ResourceInstance; import org.apache.ambari.server.api.resources.ResourceInstanceFactoryImpl; import org.apache.ambari.server.api.resources.SubResourceDefinition; +import org.apache.ambari.server.api.services.BaseRequest; import org.apache.ambari.server.api.services.ResultImpl; import org.apache.ambari.server.api.util.TreeNode; import org.apache.ambari.server.api.util.TreeNodeImpl; @@ -33,6 +34,7 @@ import org.apache.ambari.server.controller.predicate.AndPredicate; import org.apache.ambari.server.controller.predicate.EqualsPredicate; import org.apache.ambari.server.api.services.Result; import org.apache.ambari.server.controller.spi.*; +import org.apache.ambari.server.controller.spi.PageRequest.StartingPoint; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -816,9 +818,20 @@ public class QueryImpl implements Query, ResourceInstance { } private Request createRequest() { - + Map<String, String> requestInfoProperties = new HashMap<String, String>(); + + if (pageRequest != null) { + requestInfoProperties.put(BaseRequest.PAGE_SIZE_PROPERTY_KEY, + Integer.toString(pageRequest.getPageSize() + pageRequest.getOffset())); + requestInfoProperties.put( + BaseRequest.ASC_ORDER_PROPERTY_KEY, + Boolean.toString(pageRequest.getStartingPoint() == StartingPoint.Beginning + || pageRequest.getStartingPoint() == StartingPoint.OffsetStart)); + } + if (allProperties) { - return PropertyHelper.getReadRequest(Collections.<String>emptySet()); + return PropertyHelper.getReadRequest(Collections.<String> emptySet(), + requestInfoProperties, null); } Map<String, TemporalInfo> mapTemporalInfo = new HashMap<String, TemporalInfo>(); @@ -834,7 +847,8 @@ public class QueryImpl implements Query, ResourceInstance { mapTemporalInfo.put(propertyId, globalTemporalInfo); } } - return PropertyHelper.getReadRequest(setProperties, mapTemporalInfo); + + return PropertyHelper.getReadRequest(setProperties, requestInfoProperties, mapTemporalInfo); } http://git-wip-us.apache.org/repos/asf/ambari/blob/cb6765fa/ambari-server/src/main/java/org/apache/ambari/server/api/services/BaseRequest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/api/services/BaseRequest.java b/ambari-server/src/main/java/org/apache/ambari/server/api/services/BaseRequest.java index 6d9c13b..71c6f68 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/api/services/BaseRequest.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/api/services/BaseRequest.java @@ -73,7 +73,17 @@ public abstract class BaseRequest implements Request { /** * Default page size for pagination request. */ - private static final int DEFAULT_PAGE_SIZE = 20; + public static final int DEFAULT_PAGE_SIZE = 20; + + /** + * Page size property key + */ + public static final String PAGE_SIZE_PROPERTY_KEY = "Request_Info/max_results"; + + /** + * Sort order property key. (true - ASC , false - DESC) + */ + public static final String ASC_ORDER_PROPERTY_KEY = "Request_Info/asc_order"; /** * Associated resource renderer. http://git-wip-us.apache.org/repos/asf/ambari/blob/cb6765fa/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/RequestResourceProvider.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/RequestResourceProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/RequestResourceProvider.java index 70f4b75..36def40 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/RequestResourceProvider.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/RequestResourceProvider.java @@ -17,11 +17,11 @@ */ package org.apache.ambari.server.controller.internal; -import com.google.gson.Gson; import org.apache.ambari.server.AmbariException; import org.apache.ambari.server.actionmanager.ActionManager; import org.apache.ambari.server.actionmanager.HostRoleCommand; import org.apache.ambari.server.actionmanager.HostRoleStatus; +import org.apache.ambari.server.api.services.BaseRequest; import org.apache.ambari.server.controller.AmbariManagementController; import org.apache.ambari.server.controller.ExecuteActionRequest; import org.apache.ambari.server.controller.RequestStatusResponse; @@ -122,6 +122,12 @@ public class RequestResourceProvider extends AbstractControllerResourceProvider Set<String> requestedIds = getRequestPropertyIds(request, predicate); Set<Resource> resources = new HashSet<Resource>(); + String maxResultsRaw = request.getRequestInfoProperties().get(BaseRequest.PAGE_SIZE_PROPERTY_KEY); + String ascOrderRaw = request.getRequestInfoProperties().get(BaseRequest.ASC_ORDER_PROPERTY_KEY); + + Integer maxResults = (maxResultsRaw == null ? null : Integer.parseInt(maxResultsRaw)); + Boolean ascOrder = (ascOrderRaw == null ? null : Boolean.parseBoolean(ascOrderRaw)); + for (Map<String, Object> properties : getPropertyMaps(predicate)) { String clusterName = (String) properties.get(REQUEST_CLUSTER_NAME_PROPERTY_ID); @@ -134,7 +140,9 @@ public class RequestResourceProvider extends AbstractControllerResourceProvider if (properties.get(REQUEST_STATUS_PROPERTY_ID) != null) { requestStatus = (String) properties.get(REQUEST_STATUS_PROPERTY_ID); } - resources.addAll(getRequestResources(clusterName, requestId, requestStatus, requestedIds)); + + resources.addAll(getRequestResources(clusterName, requestId, requestStatus, maxResults, + ascOrder, requestedIds)); } return resources; } @@ -229,6 +237,8 @@ public class RequestResourceProvider extends AbstractControllerResourceProvider private Set<Resource> getRequestResources(String clusterName, Long requestId, String requestStatus, + Integer maxResults, + Boolean ascOrder, Set<String> requestedPropertyIds) throws NoSuchResourceException, NoSuchParentResourceException { @@ -255,8 +265,13 @@ public class RequestResourceProvider extends AbstractControllerResourceProvider + ", requestId=null" + ", requestStatus=" + status); } - response.addAll(getRequestResources(clusterName, actionManager, - actionManager.getRequestsByStatus(status), requestedPropertyIds)); + + List<Long> requestIds = actionManager.getRequestsByStatus(status, + maxResults != null ? maxResults : BaseRequest.DEFAULT_PAGE_SIZE, + ascOrder != null ? ascOrder : false); + + response.addAll(getRequestResources(clusterName, actionManager, requestIds, + requestedPropertyIds)); } else { Collection<Resource> responses = getRequestResources( clusterName, actionManager, Collections.singletonList(requestId), requestedPropertyIds); http://git-wip-us.apache.org/repos/asf/ambari/blob/cb6765fa/ambari-server/src/main/java/org/apache/ambari/server/controller/utilities/PropertyHelper.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/utilities/PropertyHelper.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/utilities/PropertyHelper.java index efc7ee4..00efa1a 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/controller/utilities/PropertyHelper.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/utilities/PropertyHelper.java @@ -284,6 +284,19 @@ public class PropertyHelper { * Factory method to create a read request from the given set of property ids. The set of * property ids represents the properties of interest for the query. * + * @param propertyIds the property ids associated with the request; may be null + * @param requestInfoProperties request info properties + * @param mapTemporalInfo the temporal info + */ + public static Request getReadRequest(Set<String> propertyIds, + Map<String, String> requestInfoProperties, Map<String, TemporalInfo> mapTemporalInfo) { + return new RequestImpl(propertyIds, null, requestInfoProperties, mapTemporalInfo); + } + + /** + * Factory method to create a read request from the given set of property ids. The set of + * property ids represents the properties of interest for the query. + * * @param propertyIds the property ids associated with the request; may be null */ public static Request getReadRequest(String ... propertyIds) { http://git-wip-us.apache.org/repos/asf/ambari/blob/cb6765fa/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/HostRoleCommandDAO.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/HostRoleCommandDAO.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/HostRoleCommandDAO.java index 66c89c0..71c27cb 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/HostRoleCommandDAO.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/HostRoleCommandDAO.java @@ -46,8 +46,6 @@ public class HostRoleCommandDAO { Provider<EntityManager> entityManagerProvider; @Inject DaoUtils daoUtils; - private static Logger LOG = LoggerFactory.getLogger(HostRoleCommandDAO.class); - private static final int REQUESTS_RESULT_LIMIT = 10; @RequiresSession public HostRoleCommandEntity findByPK(long taskId) { @@ -178,19 +176,10 @@ public class HostRoleCommandDAO { } @RequiresSession - public List<Long> getRequests() { - String queryStr = "SELECT DISTINCT command.requestId " + - "FROM HostRoleCommandEntity command ORDER BY command.requestId DESC"; - TypedQuery<Long> query = entityManagerProvider.get().createQuery(queryStr, - Long.class); - query.setMaxResults(REQUESTS_RESULT_LIMIT); - return daoUtils.selectList(query); - } + public List<Long> getRequestsByTaskStatus(Collection<HostRoleStatus> statuses, + boolean match, boolean checkAllTasks, int maxResults, boolean ascOrder) { - @RequiresSession - public List<Long> getRequestsByTaskStatus( - Collection<HostRoleStatus> statuses, boolean match, boolean checkAllTasks) { - List<Long> results = null; + List<Long> results; StringBuilder queryStr = new StringBuilder(); queryStr.append("SELECT DISTINCT command.requestId ").append( @@ -216,10 +205,10 @@ public class HostRoleCommandDAO { } } - queryStr.append("ORDER BY command.requestId DESC"); + queryStr.append("ORDER BY command.requestId ").append(ascOrder ? "ASC" : "DESC"); TypedQuery<Long> query = entityManagerProvider.get().createQuery(queryStr.toString(), Long.class); - query.setMaxResults(REQUESTS_RESULT_LIMIT); + query.setMaxResults(maxResults); if (statuses != null && !statuses.isEmpty()) { results = daoUtils.selectList(query, statuses); http://git-wip-us.apache.org/repos/asf/ambari/blob/cb6765fa/ambari-server/src/test/java/org/apache/ambari/server/actionmanager/TestActionDBAccessorImpl.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/actionmanager/TestActionDBAccessorImpl.java b/ambari-server/src/test/java/org/apache/ambari/server/actionmanager/TestActionDBAccessorImpl.java index 344891d..5ad1e77 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/actionmanager/TestActionDBAccessorImpl.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/actionmanager/TestActionDBAccessorImpl.java @@ -18,9 +18,11 @@ package org.apache.ambari.server.actionmanager; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import com.google.inject.persist.UnitOfWork; + import junit.framework.Assert; import org.apache.ambari.server.AmbariException; @@ -28,6 +30,7 @@ import org.apache.ambari.server.Role; import org.apache.ambari.server.RoleCommand; import org.apache.ambari.server.agent.ActionQueue; import org.apache.ambari.server.agent.CommandReport; +import org.apache.ambari.server.api.services.BaseRequest; import org.apache.ambari.server.controller.ExecuteActionRequest; import org.apache.ambari.server.controller.HostsMap; import org.apache.ambari.server.controller.internal.RequestResourceFilter; @@ -270,13 +273,51 @@ public class TestActionDBAccessorImpl { clusters.addHost("host2"); clusters.getHost("host2").persist(); populateActionDB(db, hostName, requestId + 1, stageId); - List<Long> requestIdsResult = db.getRequestsByStatus(null); + List<Long> requestIdsResult = + db.getRequestsByStatus(null, BaseRequest.DEFAULT_PAGE_SIZE, false); assertNotNull("List of request IDs is null", requestIdsResult); assertEquals("Request IDs not matches", requestIds, requestIdsResult); } @Test + public void testGetRequestsByStatusWithParams() { + List<Long> ids = new ArrayList<Long>(); + + for (long l = 0; l < 10; l++) { + ids.add(l); + } + + for (Long id : ids) { + populateActionDB(db, hostName, id, stageId); + } + + List<Long> expected = null; + List<Long> actual = null; + + // Select all requests + actual = db.getRequestsByStatus(null, BaseRequest.DEFAULT_PAGE_SIZE, false); + expected = reverse(new ArrayList<Long>(ids)); + assertEquals("Request IDs not matches", expected, actual); + + actual = db.getRequestsByStatus(null, 4, false); + expected = reverse(new ArrayList<Long>(ids.subList(ids.size() - 4, ids.size()))); + assertEquals("Request IDs not matches", expected, actual); + + actual = db.getRequestsByStatus(null, 7, true); + expected = new ArrayList<Long>(ids.subList(0, 7)); + assertEquals("Request IDs not matches", expected, actual); + } + + private <T> List<T> reverse(List<T> list) { + List<T> result = new ArrayList<T>(list); + + Collections.reverse(result); + + return result; + } + + @Test public void testAbortRequest() throws AmbariException { Stage s = new Stage(requestId, "/a/b", "cluster1", "action db accessor test", "clusterHostInfo"); s.setStageId(stageId); http://git-wip-us.apache.org/repos/asf/ambari/blob/cb6765fa/ambari-server/src/test/java/org/apache/ambari/server/actionmanager/TestActionManager.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/actionmanager/TestActionManager.java b/ambari-server/src/test/java/org/apache/ambari/server/actionmanager/TestActionManager.java index 9cb2199..27da600 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/actionmanager/TestActionManager.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/actionmanager/TestActionManager.java @@ -27,6 +27,7 @@ import org.apache.ambari.server.Role; import org.apache.ambari.server.RoleCommand; import org.apache.ambari.server.agent.ActionQueue; import org.apache.ambari.server.agent.CommandReport; +import org.apache.ambari.server.api.services.BaseRequest; import org.apache.ambari.server.controller.HostsMap; import org.apache.ambari.server.orm.GuiceJpaInitializer; import org.apache.ambari.server.orm.InMemoryDefaultTestModule; @@ -182,12 +183,12 @@ public class TestActionManager { clusters.getCluster(clusterName); - assertEquals(1, am.getRequests().size()); +// assertEquals(1, am.getRequests(BaseRequest.DEFAULT_PAGE_SIZE).size()); clusters.deleteCluster(clusterName); assertEquals(0, clusters.getClusters().size()); - assertEquals(0, am.getRequests().size()); +// assertEquals(0, am.getRequests().size()); } http://git-wip-us.apache.org/repos/asf/ambari/blob/cb6765fa/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/RequestResourceProviderTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/RequestResourceProviderTest.java b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/RequestResourceProviderTest.java index e9bab1a..c0ad8ce 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/RequestResourceProviderTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/RequestResourceProviderTest.java @@ -22,6 +22,7 @@ import org.apache.ambari.server.AmbariException; import org.apache.ambari.server.actionmanager.ActionManager; import org.apache.ambari.server.actionmanager.HostRoleCommand; import org.apache.ambari.server.actionmanager.HostRoleStatus; +import org.apache.ambari.server.api.services.BaseRequest; import org.apache.ambari.server.controller.AmbariManagementController; import org.apache.ambari.server.controller.ExecuteActionRequest; import org.apache.ambari.server.controller.RequestStatusResponse; @@ -53,6 +54,7 @@ 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.reset; import static org.easymock.EasyMock.verify; /** @@ -100,6 +102,76 @@ public class RequestResourceProviderTest { } @Test + public void testGetResourcesWithRequestInfo() throws Exception { + Resource.Type type = Resource.Type.Request; + + ActionManager actionManager = createNiceMock(ActionManager.class); + + Clusters clusters = createNiceMock(Clusters.class); + expect(clusters.getCluster("foo_cluster")).andReturn(null).anyTimes(); + + AmbariManagementController managementController = + createMock(AmbariManagementController.class); + expect(managementController.getActionManager()).andReturn(actionManager) + .anyTimes(); + expect(managementController.getClusters()).andReturn(clusters).anyTimes(); + + replay(managementController, clusters); + + ResourceProvider provider = AbstractControllerResourceProvider.getResourceProvider(type, + PropertyHelper.getPropertyIds(type), PropertyHelper.getKeyPropertyIds(type), + managementController); + + Map<String, String> requestInfoProperties = new HashMap<String, String>(); + Request request = null; + Predicate predicate = new PredicateBuilder() + .property(RequestResourceProvider.REQUEST_CLUSTER_NAME_PROPERTY_ID) + .equals("foo_cluster") + .and().property(RequestResourceProvider.REQUEST_ID_PROPERTY_ID) + .equals(null) + .and().property(RequestResourceProvider.REQUEST_STATUS_PROPERTY_ID) + .equals(null) + .toPredicate(); + + request = PropertyHelper.getReadRequest(new HashSet<String>(), + requestInfoProperties, null); + expect(actionManager.getRequests(Collections.<Long> emptyList())) + .andReturn(Collections.<org.apache.ambari.server.actionmanager.Request> emptyList()); + expect(actionManager.getRequestsByStatus(null, + BaseRequest.DEFAULT_PAGE_SIZE, false)) + .andReturn(Collections.<Long> emptyList()); + + replay(actionManager); + provider.getResources(request, predicate); + verify(actionManager); + reset(actionManager); + + requestInfoProperties.put(BaseRequest.PAGE_SIZE_PROPERTY_KEY, "20"); + request = PropertyHelper.getReadRequest(new HashSet<String>(), + requestInfoProperties, null); + expect(actionManager.getRequests(Collections.<Long> emptyList())) + .andReturn(Collections.<org.apache.ambari.server.actionmanager.Request> emptyList()); + expect(actionManager.getRequestsByStatus(null, 20, false)) + .andReturn(Collections.<Long> emptyList()); + replay(actionManager); + provider.getResources(request, predicate); + verify(actionManager); + reset(actionManager); + + requestInfoProperties.put(BaseRequest.ASC_ORDER_PROPERTY_KEY, "true"); + request = PropertyHelper.getReadRequest(new HashSet<String>(), + requestInfoProperties, null); + expect(actionManager.getRequests(Collections.<Long> emptyList())) + .andReturn(Collections.<org.apache.ambari.server.actionmanager.Request> emptyList()); + expect(actionManager.getRequestsByStatus(null, 20, true)) + .andReturn(Collections.<Long> emptyList()); + replay(actionManager); + provider.getResources(request, predicate); + verify(actionManager); + reset(actionManager); + } + + @Test public void testGetResources() throws Exception { Resource.Type type = Resource.Type.Request;
