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.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) {
|
||||
|
||||
@ -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();
|
||||
|
||||
Set<Class<?>> cmdClasses = new HashSet<Class<?>>();
|
||||
cmdClasses.add(ListApisCmd.class);
|
||||
_discoveryService.cacheResponseMap(cmdClasses);
|
||||
_discoveryService.s_apiAccessCheckers = (List<APIChecker>) mock(List.class);
|
||||
_discoveryService._apiAccessCheckers = (List<APIChecker>) mock(List.class);
|
||||
_discoveryService._services = (List<PluggableService>) mock(List.class);
|
||||
|
||||
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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user