From 06ed0a60150bf5ab6892050737e995ea0924f3e2 Mon Sep 17 00:00:00 2001 From: Tom Page Date: Fri, 26 Jun 2015 14:47:03 +0000 Subject: [PATCH] RM-2130 Post method invocation processor for QueryEngineResults. +review RM @taksoy git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/modules/recordsmanagement/DEV/ENFORCE@107246 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../PostMethodInvocationProcessor.java | 24 +++++ ...eResultsPostMethodInvocationProcessor.java | 67 ++++++++++++++ ...PostMethodInvocationProcessorUnitTest.java | 89 +++++++++++++++++++ 3 files changed, 180 insertions(+) create mode 100644 rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/interceptor/processor/QueryEngineResultsPostMethodInvocationProcessor.java create mode 100644 rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/classification/interceptor/processor/QueryEngineResultsPostMethodInvocationProcessorUnitTest.java diff --git a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/interceptor/processor/PostMethodInvocationProcessor.java b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/interceptor/processor/PostMethodInvocationProcessor.java index 775d03a169..523db04e28 100644 --- a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/interceptor/processor/PostMethodInvocationProcessor.java +++ b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/interceptor/processor/PostMethodInvocationProcessor.java @@ -98,6 +98,30 @@ public class PostMethodInvocationProcessor return result; } + /** + * Gets the processor from the available processors. + * + * @param clazz The class of the post invocation object. + * @return The suitable processor for the given class. + */ + protected BasePostMethodInvocationProcessor getProcessorForClass(Class clazz) + { + mandatory("clazz", clazz); + BasePostMethodInvocationProcessor result = null; + + Set, BasePostMethodInvocationProcessor>> processorsEntrySet = getProcessors().entrySet(); + for (Map.Entry, BasePostMethodInvocationProcessor> processorEntry : processorsEntrySet) + { + if (processorEntry.getKey().isAssignableFrom(clazz)) + { + result = processorEntry.getValue(); + break; + } + } + + return result; + } + /** * Processes the given object * diff --git a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/interceptor/processor/QueryEngineResultsPostMethodInvocationProcessor.java b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/interceptor/processor/QueryEngineResultsPostMethodInvocationProcessor.java new file mode 100644 index 0000000000..3705335115 --- /dev/null +++ b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/interceptor/processor/QueryEngineResultsPostMethodInvocationProcessor.java @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2005-2015 Alfresco Software Limited. + * + * This file is part of Alfresco + * + * Alfresco is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Alfresco is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + */ +package org.alfresco.module.org_alfresco_module_rm.classification.interceptor.processor; + +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +import org.alfresco.repo.search.impl.querymodel.QueryEngineResults; +import org.alfresco.service.cmr.search.ResultSet; + +/** + * A post method invocation processor for {@link QueryEngineResults}. + * + * @author Tom Page + * @since 3.0 + */ +public class QueryEngineResultsPostMethodInvocationProcessor extends BasePostMethodInvocationProcessor +{ + /** The post method invocation processor for {@link ResultSet ResultSets}. */ + private BasePostMethodInvocationProcessor resultSetProcessor; + + @Override + protected Class getClassName() + { + return QueryEngineResults.class; + } + + @SuppressWarnings("unchecked") + @Override + public T process(T object) + { + if (resultSetProcessor == null) + { + resultSetProcessor = getPostMethodInvocationProcessor().getProcessorForClass(ResultSet.class); + } + + QueryEngineResults queryEngineResults = getClassName().cast(object); + Map, ResultSet> resultsMap = queryEngineResults.getResults(); + Map, ResultSet> returnMap = new HashMap<>(); + for (Set key : resultsMap.keySet()) + { + ResultSet newResultSet = resultSetProcessor.process(resultsMap.get(key)); + if (newResultSet != null) + { + returnMap.put(key, newResultSet); + } + } + return (T) new QueryEngineResults(returnMap); + } +} diff --git a/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/classification/interceptor/processor/QueryEngineResultsPostMethodInvocationProcessorUnitTest.java b/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/classification/interceptor/processor/QueryEngineResultsPostMethodInvocationProcessorUnitTest.java new file mode 100644 index 0000000000..ed9ad9ef16 --- /dev/null +++ b/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/classification/interceptor/processor/QueryEngineResultsPostMethodInvocationProcessorUnitTest.java @@ -0,0 +1,89 @@ +/* + * Copyright (C) 2005-2015 Alfresco Software Limited. + * + * This file is part of Alfresco + * + * Alfresco is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Alfresco is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + */ +package org.alfresco.module.org_alfresco_module_rm.classification.interceptor.processor; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.mockito.MockitoAnnotations.initMocks; + +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +import com.google.common.collect.Sets; +import org.alfresco.repo.search.impl.querymodel.QueryEngineResults; +import org.alfresco.service.cmr.search.ResultSet; +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; + +/** + * Unit tests for {@link QueryEngineResultPostMethodInvocationProcessor}. + * + * @author Tom Page + * @since 3.0 + */ +public class QueryEngineResultsPostMethodInvocationProcessorUnitTest +{ + private static final Set KEY_1 = Sets.newHashSet("KEY_1"); + private static final Set KEY_2 = Sets.newHashSet("KEY_2"); + private static final Set KEY_3 = Sets.newHashSet("KEY_3"); + private static final Set KEY_4 = Sets.newHashSet("KEY_4"); + private static final ResultSet UNCLASSIFIED_RESULT_SET = mock(ResultSet.class); + private static final ResultSet CLASSIFIED_RESULT_SET = mock(ResultSet.class); + + @InjectMocks + private QueryEngineResultsPostMethodInvocationProcessor processor = new QueryEngineResultsPostMethodInvocationProcessor(); + @Mock + private PostMethodInvocationProcessor mockPostMethodInvocationProcessor; + @Mock + private ResultSetPostMethodInvocationProcessor mockResultSetPMIP; + + @Before + public void setUp() + { + initMocks(this); + + when(mockPostMethodInvocationProcessor.getProcessorForClass(ResultSet.class)).thenReturn(mockResultSetPMIP); + + when(mockResultSetPMIP.process(UNCLASSIFIED_RESULT_SET)).thenReturn(UNCLASSIFIED_RESULT_SET); + when(mockResultSetPMIP.process(CLASSIFIED_RESULT_SET)).thenReturn(null); + } + + /** Check that {@code process} filters out the classified result sets. */ + @Test + public void testProcess() + { + Map, ResultSet> resultsMap = new HashMap<>(); + resultsMap.put(KEY_1, UNCLASSIFIED_RESULT_SET); + resultsMap.put(KEY_2, CLASSIFIED_RESULT_SET); + resultsMap.put(KEY_3, UNCLASSIFIED_RESULT_SET); + resultsMap.put(KEY_4, CLASSIFIED_RESULT_SET); + QueryEngineResults queryEngineResults = new QueryEngineResults(resultsMap); + + QueryEngineResults returnedQueryEngineResults = processor.process(queryEngineResults); + + Map, ResultSet> expectedResultSet = new HashMap<>(); + expectedResultSet.put(KEY_1, UNCLASSIFIED_RESULT_SET); + expectedResultSet.put(KEY_3, UNCLASSIFIED_RESULT_SET); + assertEquals("Unexpected results from query.", expectedResultSet, returnedQueryEngineResults.getResults()); + } +}