mirror of
https://github.com/apache/cloudstack.git
synced 2025-10-26 08:42:29 +01:00
ApiDiscovery: Fix tests and make constructor light weight, let spring run init()
Signed-off-by: Rohit Yadav <bhaisaab@apache.org>
This commit is contained in:
parent
d296a8fa65
commit
44316f7398
@ -24,6 +24,7 @@ import java.util.List;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
import javax.annotation.PostConstruct;
|
||||||
import javax.ejb.Local;
|
import javax.ejb.Local;
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
@ -54,19 +55,24 @@ import com.google.gson.annotations.SerializedName;
|
|||||||
public class ApiDiscoveryServiceImpl implements ApiDiscoveryService {
|
public class ApiDiscoveryServiceImpl implements ApiDiscoveryService {
|
||||||
private static final Logger s_logger = Logger.getLogger(ApiDiscoveryServiceImpl.class);
|
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;
|
private static Map<String, ApiDiscoveryResponse> s_apiNameDiscoveryResponseMap = null;
|
||||||
|
|
||||||
@Inject List<PluggableService> _services;
|
|
||||||
|
|
||||||
protected ApiDiscoveryServiceImpl() {
|
protected ApiDiscoveryServiceImpl() {
|
||||||
super();
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
void init() {
|
||||||
if (s_apiNameDiscoveryResponseMap == null) {
|
if (s_apiNameDiscoveryResponseMap == null) {
|
||||||
long startTime = System.nanoTime();
|
long startTime = System.nanoTime();
|
||||||
s_apiNameDiscoveryResponseMap = new HashMap<String, ApiDiscoveryResponse>();
|
s_apiNameDiscoveryResponseMap = new HashMap<String, ApiDiscoveryResponse>();
|
||||||
//TODO: Fix and use PluggableService to get the classes
|
//TODO: Fix and use PluggableService to get the classes
|
||||||
Set<Class<?>> cmdClasses = ReflectUtil.getClassesWithAnnotation(APICommand.class,
|
Set<Class<?>> cmdClasses = ReflectUtil.getClassesWithAnnotation(APICommand.class,
|
||||||
new String[]{"org.apache.cloudstack.api", "com.cloud.api"});
|
new String[]{"org.apache.cloudstack.api", "com.cloud.api"});
|
||||||
|
for(PluggableService service: _services)
|
||||||
|
cmdClasses.addAll(service.getCommands());
|
||||||
cacheResponseMap(cmdClasses);
|
cacheResponseMap(cmdClasses);
|
||||||
long endTime = System.nanoTime();
|
long endTime = System.nanoTime();
|
||||||
s_logger.info("Api Discovery Service: Annotation, docstrings, api relation graph processed in " + (endTime - startTime) / 1000000.0 + " ms");
|
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))
|
if (!s_apiNameDiscoveryResponseMap.containsKey(name))
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
for (APIChecker apiChecker : s_apiAccessCheckers) {
|
for (APIChecker apiChecker : _apiAccessCheckers) {
|
||||||
try {
|
try {
|
||||||
apiChecker.checkAccess(user, name);
|
apiChecker.checkAccess(user, name);
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
@ -192,7 +198,7 @@ public class ApiDiscoveryServiceImpl implements ApiDiscoveryService {
|
|||||||
} else {
|
} else {
|
||||||
for (String apiName : s_apiNameDiscoveryResponseMap.keySet()) {
|
for (String apiName : s_apiNameDiscoveryResponseMap.keySet()) {
|
||||||
boolean isAllowed = true;
|
boolean isAllowed = true;
|
||||||
for (APIChecker apiChecker : s_apiAccessCheckers) {
|
for (APIChecker apiChecker : _apiAccessCheckers) {
|
||||||
try {
|
try {
|
||||||
apiChecker.checkAccess(user, apiName);
|
apiChecker.checkAccess(user, apiName);
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
|
|||||||
@ -22,6 +22,7 @@ import com.cloud.user.UserVO;
|
|||||||
import java.util.*;
|
import java.util.*;
|
||||||
import javax.naming.ConfigurationException;
|
import javax.naming.ConfigurationException;
|
||||||
|
|
||||||
|
import com.cloud.utils.component.PluggableService;
|
||||||
import org.apache.cloudstack.acl.APIChecker;
|
import org.apache.cloudstack.acl.APIChecker;
|
||||||
import org.apache.cloudstack.api.APICommand;
|
import org.apache.cloudstack.api.APICommand;
|
||||||
import org.apache.cloudstack.api.command.user.discovery.ListApisCmd;
|
import org.apache.cloudstack.api.command.user.discovery.ListApisCmd;
|
||||||
@ -35,9 +36,9 @@ import static org.junit.Assert.*;
|
|||||||
import static org.mockito.Mockito.*;
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
public class ApiDiscoveryTest {
|
public class ApiDiscoveryTest {
|
||||||
|
|
||||||
private static ApiDiscoveryServiceImpl _discoveryService = new ApiDiscoveryServiceImpl();
|
|
||||||
private static APIChecker _apiChecker = mock(APIChecker.class);
|
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 Class<?> testCmdClass = ListApisCmd.class;
|
||||||
private static User testUser;
|
private static User testUser;
|
||||||
@ -54,13 +55,18 @@ public class ApiDiscoveryTest {
|
|||||||
testApiAsync = false;
|
testApiAsync = false;
|
||||||
testUser = new UserVO();
|
testUser = new UserVO();
|
||||||
|
|
||||||
Set<Class<?>> cmdClasses = new HashSet<Class<?>>();
|
_discoveryService._apiAccessCheckers = (List<APIChecker>) mock(List.class);
|
||||||
cmdClasses.add(ListApisCmd.class);
|
_discoveryService._services = (List<PluggableService>) mock(List.class);
|
||||||
_discoveryService.cacheResponseMap(cmdClasses);
|
|
||||||
_discoveryService.s_apiAccessCheckers = (List<APIChecker>) mock(List.class);
|
|
||||||
|
|
||||||
when(_apiChecker.checkAccess(any(User.class), anyString())).thenReturn(true);
|
when(_apiChecker.checkAccess(any(User.class), anyString())).thenReturn(true);
|
||||||
when(_discoveryService.s_apiAccessCheckers.iterator()).thenReturn(Arrays.asList(_apiChecker).iterator());
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user