Updated Branches: refs/heads/master d296a8fa6 -> 44316f739
ApiDiscovery: Fix tests and make constructor light weight, let spring run init() Signed-off-by: Rohit Yadav <[email protected]> Project: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/commit/44316f73 Tree: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/tree/44316f73 Diff: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/diff/44316f73 Branch: refs/heads/master Commit: 44316f739841000d33efffcc1517945de0486246 Parents: d296a8f Author: Rohit Yadav <[email protected]> Authored: Wed Feb 6 20:07:27 2013 +0530 Committer: Rohit Yadav <[email protected]> Committed: Wed Feb 6 20:07:27 2013 +0530 ---------------------------------------------------------------------- .../discovery/ApiDiscoveryServiceImpl.java | 16 +++++++++---- .../cloudstack/discovery/ApiDiscoveryTest.java | 18 ++++++++++----- 2 files changed, 23 insertions(+), 11 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/44316f73/plugins/api/discovery/src/org/apache/cloudstack/discovery/ApiDiscoveryServiceImpl.java ---------------------------------------------------------------------- diff --git a/plugins/api/discovery/src/org/apache/cloudstack/discovery/ApiDiscoveryServiceImpl.java b/plugins/api/discovery/src/org/apache/cloudstack/discovery/ApiDiscoveryServiceImpl.java index 7689ba4..087b166 100644 --- a/plugins/api/discovery/src/org/apache/cloudstack/discovery/ApiDiscoveryServiceImpl.java +++ b/plugins/api/discovery/src/org/apache/cloudstack/discovery/ApiDiscoveryServiceImpl.java @@ -24,6 +24,7 @@ import java.util.List; import java.util.Map; import java.util.Set; +import javax.annotation.PostConstruct; import javax.ejb.Local; import javax.inject.Inject; @@ -54,19 +55,24 @@ import com.google.gson.annotations.SerializedName; public class ApiDiscoveryServiceImpl implements ApiDiscoveryService { private static final Logger s_logger = Logger.getLogger(ApiDiscoveryServiceImpl.class); - @Inject protected List<APIChecker> s_apiAccessCheckers = null; + @Inject protected List<APIChecker> _apiAccessCheckers = null; + @Inject protected List<PluggableService> _services = null; private static Map<String, ApiDiscoveryResponse> s_apiNameDiscoveryResponseMap = null; - @Inject List<PluggableService> _services; - protected ApiDiscoveryServiceImpl() { super(); + } + + @PostConstruct + void init() { if (s_apiNameDiscoveryResponseMap == null) { long startTime = System.nanoTime(); s_apiNameDiscoveryResponseMap = new HashMap<String, ApiDiscoveryResponse>(); //TODO: Fix and use PluggableService to get the classes Set<Class<?>> cmdClasses = ReflectUtil.getClassesWithAnnotation(APICommand.class, new String[]{"org.apache.cloudstack.api", "com.cloud.api"}); + for(PluggableService service: _services) + cmdClasses.addAll(service.getCommands()); cacheResponseMap(cmdClasses); long endTime = System.nanoTime(); s_logger.info("Api Discovery Service: Annotation, docstrings, api relation graph processed in " + (endTime - startTime) / 1000000.0 + " ms"); @@ -180,7 +186,7 @@ public class ApiDiscoveryServiceImpl implements ApiDiscoveryService { if (!s_apiNameDiscoveryResponseMap.containsKey(name)) return null; - for (APIChecker apiChecker : s_apiAccessCheckers) { + for (APIChecker apiChecker : _apiAccessCheckers) { try { apiChecker.checkAccess(user, name); } catch (Exception ex) { @@ -192,7 +198,7 @@ public class ApiDiscoveryServiceImpl implements ApiDiscoveryService { } else { for (String apiName : s_apiNameDiscoveryResponseMap.keySet()) { boolean isAllowed = true; - for (APIChecker apiChecker : s_apiAccessCheckers) { + for (APIChecker apiChecker : _apiAccessCheckers) { try { apiChecker.checkAccess(user, apiName); } catch (Exception ex) { http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/44316f73/plugins/api/discovery/test/org/apache/cloudstack/discovery/ApiDiscoveryTest.java ---------------------------------------------------------------------- diff --git a/plugins/api/discovery/test/org/apache/cloudstack/discovery/ApiDiscoveryTest.java b/plugins/api/discovery/test/org/apache/cloudstack/discovery/ApiDiscoveryTest.java index 3b526dd..320d20a 100644 --- a/plugins/api/discovery/test/org/apache/cloudstack/discovery/ApiDiscoveryTest.java +++ b/plugins/api/discovery/test/org/apache/cloudstack/discovery/ApiDiscoveryTest.java @@ -22,6 +22,7 @@ import com.cloud.user.UserVO; import java.util.*; import javax.naming.ConfigurationException; +import com.cloud.utils.component.PluggableService; import org.apache.cloudstack.acl.APIChecker; import org.apache.cloudstack.api.APICommand; import org.apache.cloudstack.api.command.user.discovery.ListApisCmd; @@ -35,9 +36,9 @@ import static org.junit.Assert.*; import static org.mockito.Mockito.*; public class ApiDiscoveryTest { - - private static ApiDiscoveryServiceImpl _discoveryService = new ApiDiscoveryServiceImpl(); private static APIChecker _apiChecker = mock(APIChecker.class); + private static PluggableService _pluggableService = mock(PluggableService.class); + private static ApiDiscoveryServiceImpl _discoveryService = new ApiDiscoveryServiceImpl(); private static Class<?> testCmdClass = ListApisCmd.class; private static User testUser; @@ -54,13 +55,18 @@ public class ApiDiscoveryTest { testApiAsync = false; testUser = new UserVO(); + _discoveryService._apiAccessCheckers = (List<APIChecker>) mock(List.class); + _discoveryService._services = (List<PluggableService>) mock(List.class); + + when(_apiChecker.checkAccess(any(User.class), anyString())).thenReturn(true); + when(_pluggableService.getCommands()).thenReturn(new ArrayList<Class<?>>()); + when(_discoveryService._apiAccessCheckers.iterator()).thenReturn(Arrays.asList(_apiChecker).iterator()); + when(_discoveryService._services.iterator()).thenReturn(Arrays.asList(_pluggableService).iterator()); + Set<Class<?>> cmdClasses = new HashSet<Class<?>>(); cmdClasses.add(ListApisCmd.class); + _discoveryService.init(); _discoveryService.cacheResponseMap(cmdClasses); - _discoveryService.s_apiAccessCheckers = (List<APIChecker>) mock(List.class); - - when(_apiChecker.checkAccess(any(User.class), anyString())).thenReturn(true); - when(_discoveryService.s_apiAccessCheckers.iterator()).thenReturn(Arrays.asList(_apiChecker).iterator()); } @Test
