use of org.wso2.carbon.apimgt.core.configuration.models.ServiceDiscoveryConfigurations in project carbon-apimgt by wso2.
the class ServiceDiscoveryConfigBuilderTestCase method testServiceDiscoveryConfigBuilder.
@Test(description = "Test all the methods in ServiceDiscoveryConfigBuilder")
public void testServiceDiscoveryConfigBuilder() throws Exception {
ConfigProvider configProvider = Mockito.mock(ConfigProvider.class);
ServiceDiscoveryConfigurations sdConfig = Mockito.mock(ServiceDiscoveryConfigurations.class);
Mockito.when(configProvider.getConfigurationObject(ServiceDiscoveryConfigurations.class)).thenReturn(sdConfig);
ServiceDiscoveryConfigBuilder.build(configProvider);
Assert.assertNotNull(ServiceDiscoveryConfigBuilder.getServiceDiscoveryConfiguration());
ServiceDiscoveryConfigBuilder.clearServiceDiscoveryConfig();
Assert.assertNull(ServiceDiscoveryConfigBuilder.getServiceDiscoveryConfiguration());
}
use of org.wso2.carbon.apimgt.core.configuration.models.ServiceDiscoveryConfigurations in project carbon-apimgt by wso2.
the class ServiceDiscoveryInPublisherTestCase method testDiscoverServiceEndpointsWhenDisabled.
@Test
public void testDiscoverServiceEndpointsWhenDisabled() throws Exception {
ServiceDiscoveryConfigurations sdConfig = new ServiceDiscoveryConfigurations();
sdConfig.setEnabled(false);
PowerMockito.mockStatic(ServiceDiscoveryConfigBuilder.class);
PowerMockito.when(ServiceDiscoveryConfigBuilder.getServiceDiscoveryConfiguration()).thenReturn(sdConfig);
APIPublisherImpl apiPublisher = getApiPublisherImpl();
List<Endpoint> endpoints = apiPublisher.discoverServiceEndpoints();
Assert.assertTrue(endpoints.isEmpty());
}
use of org.wso2.carbon.apimgt.core.configuration.models.ServiceDiscoveryConfigurations in project carbon-apimgt by wso2.
the class ServiceDiscoveryInPublisherTestCase method testDiscoverServiceEndpoints.
@Test
public void testDiscoverServiceEndpoints() throws Exception {
/* Create a impl config without any namespace or criteria */
ServiceDiscoveryImplConfig sdImplConfig = new ServiceDiscoveryImplConfig();
sdImplConfig.setImplClass("org.wso2.carbon.apimgt.core.impl.ServiceDiscovererTestClass");
sdImplConfig.setImplParameters(new HashMap<>());
/* Create a impl config with a namespace set */
Map<String, String> implParamWithNamespace = new HashMap<>();
implParamWithNamespace.put(APIMgtConstants.ServiceDiscoveryConstants.NAMESPACE, "dev");
ServiceDiscoveryImplConfig sdImplConfigWithNamespace = new ServiceDiscoveryImplConfig();
sdImplConfigWithNamespace.setImplClass("org.wso2.carbon.apimgt.core.impl.ServiceDiscovererTestClass");
sdImplConfigWithNamespace.setImplParameters(implParamWithNamespace);
/* Create a impl config with a criteria set */
Map<String, String> implParamWithCriteria = new HashMap<>();
implParamWithCriteria.put(APIMgtConstants.ServiceDiscoveryConstants.CRITERIA, "app=web");
ServiceDiscoveryImplConfig sdImplConfigWitCriteria = new ServiceDiscoveryImplConfig();
sdImplConfigWitCriteria.setImplClass("org.wso2.carbon.apimgt.core.impl.ServiceDiscovererTestClass");
sdImplConfigWitCriteria.setImplParameters(implParamWithCriteria);
/* Create a impl config with both a namespace and a criteria is set */
Map<String, String> implParamWithNamespaceAndCriteria = new HashMap<>();
implParamWithNamespaceAndCriteria.put(APIMgtConstants.ServiceDiscoveryConstants.NAMESPACE, "dev");
implParamWithNamespaceAndCriteria.put(APIMgtConstants.ServiceDiscoveryConstants.CRITERIA, "app=web");
ServiceDiscoveryImplConfig sdImplConfigWithNamespaceNCriteria = new ServiceDiscoveryImplConfig();
sdImplConfigWithNamespaceNCriteria.setImplClass("org.wso2.carbon.apimgt.core.impl.ServiceDiscovererTestClass");
sdImplConfigWithNamespaceNCriteria.setImplParameters(implParamWithNamespaceAndCriteria);
/* Create the implementationsList adding all four impl configs */
List<ServiceDiscoveryImplConfig> implementationsList = new ArrayList<>();
implementationsList.add(sdImplConfig);
implementationsList.add(sdImplConfigWithNamespace);
implementationsList.add(sdImplConfigWitCriteria);
implementationsList.add(sdImplConfigWithNamespaceNCriteria);
/* Create the final configuration */
ServiceDiscoveryConfigurations sdConfig = new ServiceDiscoveryConfigurations();
sdConfig.setEnabled(true);
sdConfig.setImplementationsList(implementationsList);
PowerMockito.mockStatic(ServiceDiscoveryConfigBuilder.class);
PowerMockito.when(ServiceDiscoveryConfigBuilder.getServiceDiscoveryConfiguration()).thenReturn(sdConfig);
/* Check whether all four types of #listServices methods have been called */
APIPublisherImpl apiPublisher = getApiPublisherImpl();
List<Endpoint> endpoints = apiPublisher.discoverServiceEndpoints();
Assert.assertEquals(endpoints.size(), 4);
}
use of org.wso2.carbon.apimgt.core.configuration.models.ServiceDiscoveryConfigurations in project carbon-apimgt by wso2.
the class APIPublisherImpl method discoverServiceEndpoints.
/**
* Discover and return a list of service endpoints, of all systems that were included in the config
*
* @return {@code List<Endpoint>} list of all service endpoints
* @throws APIManagementException If an error occurred while discovering services
*/
@Override
public List<Endpoint> discoverServiceEndpoints() throws APIManagementException {
List<Endpoint> discoveredEndpointList = new ArrayList();
try {
ServiceDiscoveryConfigurations serviceDiscoveryConfig = ServiceDiscoveryConfigBuilder.getServiceDiscoveryConfiguration();
// If service discovery not enabled - do not proceed
if (!serviceDiscoveryConfig.isServiceDiscoveryEnabled()) {
log.error("Service Discovery not enabled");
return discoveredEndpointList;
}
List<ServiceDiscoveryImplConfig> implConfigList = serviceDiscoveryConfig.getImplementationsList();
for (ServiceDiscoveryImplConfig implConfig : implConfigList) {
// Every implConfig has two elements. The implClass and the implParameters.
/* Get the implClass instance */
String implClassName = implConfig.getImplClass();
Class implClass = Class.forName(implClassName);
ServiceDiscoverer serviceDiscoverer = (ServiceDiscoverer) implClass.newInstance();
/* Pass the implParameters to the above instance */
serviceDiscoverer.init(implConfig.getImplParameters());
/*
* The .init() method above sets the filtering parameters (if provided)
* to the ServiceDiscoverer impl class instance.
*
* Let's check whether those filtering parameters : "namespace" and/or "criteria" are set,
* and call ServiceDiscoverer Impl class's #listServices method accordingly
*/
String namespaceFilter = serviceDiscoverer.getNamespaceFilter();
Map<String, String> criteriaFilter = serviceDiscoverer.getCriteriaFilter();
List<Endpoint> subDiscoveredEndpointList;
if (namespaceFilter == null && criteriaFilter == null) {
// both not set
subDiscoveredEndpointList = serviceDiscoverer.listServices();
} else if (namespaceFilter != null && criteriaFilter != null) {
// both set
subDiscoveredEndpointList = serviceDiscoverer.listServices(namespaceFilter, criteriaFilter);
} else if (namespaceFilter != null) {
// only "namespace" is set
subDiscoveredEndpointList = serviceDiscoverer.listServices(namespaceFilter);
} else {
// remaining -> only "criteria" is set
subDiscoveredEndpointList = serviceDiscoverer.listServices(criteriaFilter);
}
if (subDiscoveredEndpointList != null) {
discoveredEndpointList.addAll(subDiscoveredEndpointList);
}
}
} catch (ServiceDiscoveryException e) {
String msg = "Error while Discovering Service Endpoints";
throw new APIManagementException(msg, e, e.getErrorHandler());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
String msg = "Error while Loading Service Discovery Impl Class";
throw new APIManagementException(msg, e, ExceptionCodes.ERROR_LOADING_SERVICE_DISCOVERY_IMPL_CLASS);
}
return discoveredEndpointList;
}
Aggregations