use of org.wso2.carbon.apimgt.impl.template.APITemplateBuilder in project carbon-apimgt by wso2.
the class WebSubConfigContextTest method testWithSecretConfigContextForAPI.
@Test
public void testWithSecretConfigContextForAPI() throws Exception {
API api = new API(new APIIdentifier("admin", "websubAPI", "1.0.0"));
api.setStatus(APIConstants.CREATED);
api.setContextTemplate("/websub");
api.setTransports(Constants.TRANSPORT_HTTP);
api.setEndpointSecured(false);
api.setUriTemplates(setAPIUriTemplates());
api.setType(APIConstants.APITransportType.WEBSUB.toString());
WebsubSubscriptionConfiguration webSubConfig = new WebsubSubscriptionConfiguration(true, "9207975e1fef9c41fab41645f81dbf0f", "SHA1", "x-hub-signature");
api.setWebsubSubscriptionConfiguration(webSubConfig);
Environment environment = new Environment();
environment.setType("production");
config = Mockito.mock(APIManagerConfiguration.class);
APIManagerConfigurationService apiManagerConfigurationService = new APIManagerConfigurationServiceImpl(config);
ServiceReferenceHolder.getInstance().setAPIManagerConfigurationService(apiManagerConfigurationService);
String templatePath = System.getProperty("user.dir") + File.separator + "src" + File.separator + "test" + File.separator + "resources" + File.separator;
System.setProperty("carbon.home", templatePath);
APITemplateBuilderImpl apiTemplateBuilder = new APITemplateBuilderImpl(api, null, null);
String updatedTemplate = apiTemplateBuilder.getConfigStringForTemplate(environment);
Assert.assertTrue("The websub velocity template is not updated correctly", updatedTemplate.contains("generated_signature"));
}
use of org.wso2.carbon.apimgt.impl.template.APITemplateBuilder in project carbon-apimgt by wso2.
the class WebSubConfigContextTest method testWithoutSecretConfigContextForAPI.
@Test
public void testWithoutSecretConfigContextForAPI() throws Exception {
API api = new API(new APIIdentifier("admin", "websubAPI", "1.0.0"));
api.setStatus(APIConstants.CREATED);
api.setContextTemplate("/websub");
api.setTransports(Constants.TRANSPORT_HTTP);
api.setEndpointSecured(false);
api.setUriTemplates(setAPIUriTemplates());
api.setType(APIConstants.APITransportType.WEBSUB.toString());
WebsubSubscriptionConfiguration webSubConfig = new WebsubSubscriptionConfiguration(false, "", "", "");
api.setWebsubSubscriptionConfiguration(webSubConfig);
Environment environment = new Environment();
environment.setType("production");
config = Mockito.mock(APIManagerConfiguration.class);
APIManagerConfigurationService apiManagerConfigurationService = new APIManagerConfigurationServiceImpl(config);
ServiceReferenceHolder.getInstance().setAPIManagerConfigurationService(apiManagerConfigurationService);
String templatePath = System.getProperty("user.dir") + File.separator + "src" + File.separator + "test" + File.separator + "resources" + File.separator;
System.setProperty("carbon.home", templatePath);
APITemplateBuilderImpl apiTemplateBuilder = new APITemplateBuilderImpl(api, null, null);
String updatedTemplate = apiTemplateBuilder.getConfigStringForTemplate(environment);
Assert.assertFalse("The websub velocity template is not updated correctly", updatedTemplate.contains("generated_signature"));
}
use of org.wso2.carbon.apimgt.impl.template.APITemplateBuilder in project carbon-apimgt by wso2.
the class TemplateBuilderUtilTest method addAddGqlWebSocketResourceEndpoints.
@Test
public void addAddGqlWebSocketResourceEndpoints() throws Exception {
APITemplateBuilder apiTemplateBuilder = Mockito.mock(APITemplateBuilder.class);
API api = new API(new APIIdentifier("admin", "API", "1.0"));
Set<URITemplate> uriTemplates = new HashSet<>();
URITemplate template = new URITemplate();
template.setAuthType("Any");
template.setHTTPVerb("POST");
template.setHttpVerbs("POST");
template.setUriTemplate(wildCardResource);
uriTemplates.add(template);
template = new URITemplate();
template.setUriTemplate(wildCardResource);
uriTemplates.add(template);
api.setUriTemplates(uriTemplates);
Map<String, Map<String, String>> perTopicMappings = new HashMap<>();
Map<String, String> endpoints = new HashMap<>();
endpoints.put(APIConstants.GATEWAY_ENV_TYPE_PRODUCTION, wsProdEndpoint);
endpoints.put(APIConstants.GATEWAY_ENV_TYPE_SANDBOX, wsSandEndpoint);
perTopicMappings.put(wildCardResource, endpoints);
WebSocketTopicMappingConfiguration webSocketTopicMappingConfiguration = new WebSocketTopicMappingConfiguration(perTopicMappings);
webSocketTopicMappingConfiguration.setResourceKey(wildCardResource, mappingWildCard);
api.setWebSocketTopicMappingConfiguration(webSocketTopicMappingConfiguration);
api.setType(APIConstants.GRAPHQL_API);
GatewayAPIDTO gatewayAPIDTO = new GatewayAPIDTO();
GatewayContentDTO dummyHttpProdEpContentDTO = new GatewayContentDTO();
dummyHttpProdEpContentDTO.setName("API--v1.0_APIproductionEndpoint");
dummyHttpProdEpContentDTO.setContent("dummy content");
GatewayContentDTO[] gatewayContentDTOS = new GatewayContentDTO[1];
gatewayContentDTOS[0] = dummyHttpProdEpContentDTO;
gatewayAPIDTO.setEndpointEntriesToBeAdd(gatewayContentDTOS);
String dummyProdEndpointConfig = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><endpoint name=\"" + wsProdEpName + "\">dummy prod content</endpoint>";
String dummySandboxEndpointConfig = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><endpoint name=\"" + wsSandEpName + "\">dummy sandbox content</endpoint>";
Mockito.when(apiTemplateBuilder.getConfigStringForWebSocketEndpointTemplate(APIConstants.GATEWAY_ENV_TYPE_PRODUCTION, mappingWildCard, wsProdEndpoint)).thenReturn(dummyProdEndpointConfig);
Mockito.when(apiTemplateBuilder.getConfigStringForWebSocketEndpointTemplate(APIConstants.GATEWAY_ENV_TYPE_SANDBOX, mappingWildCard, wsSandEndpoint)).thenReturn(dummySandboxEndpointConfig);
TemplateBuilderUtil.addWebSocketResourceEndpoints(api, apiTemplateBuilder, gatewayAPIDTO);
GatewayContentDTO[] endpointEntries = gatewayAPIDTO.getEndpointEntriesToBeAdd();
Assert.assertEquals(endpointEntries.length, 3);
boolean httpEpConfigPresent = false;
boolean wsProdEpConfigPresent = false;
boolean wsSandEPConfigPresent = false;
for (int i = 0; i < 3; i++) {
if (dummyHttpProdEpContentDTO.getName().equals(endpointEntries[i].getName())) {
httpEpConfigPresent = true;
Assert.assertEquals(endpointEntries[i].getContent(), dummyHttpProdEpContentDTO.getContent());
}
if (wsProdEpName.equals(endpointEntries[i].getName())) {
wsProdEpConfigPresent = true;
Assert.assertEquals(endpointEntries[i].getContent(), dummyProdEndpointConfig);
}
if (wsSandEpName.equals(endpointEntries[i].getName())) {
wsSandEPConfigPresent = true;
Assert.assertEquals(endpointEntries[i].getContent(), dummySandboxEndpointConfig);
}
}
Assert.assertTrue(wsProdEpConfigPresent);
Assert.assertTrue(wsSandEPConfigPresent);
Assert.assertTrue(httpEpConfigPresent);
}
use of org.wso2.carbon.apimgt.impl.template.APITemplateBuilder in project carbon-apimgt by wso2.
the class TemplateBuilderUtil method addWebSocketResourceEndpoints.
public static void addWebSocketResourceEndpoints(API api, APITemplateBuilder builder, GatewayAPIDTO gatewayAPIDTO) throws APITemplateException, XMLStreamException {
Set<URITemplate> uriTemplates = api.getUriTemplates();
Map<String, Map<String, String>> topicMappings = api.getWebSocketTopicMappingConfiguration().getMappings();
List<GatewayContentDTO> endpointsToAdd = new ArrayList<>();
for (URITemplate resource : uriTemplates) {
String topic = resource.getUriTemplate();
Map<String, String> endpoints = topicMappings.get(topic);
// Production and Sandbox endpoints
for (Map.Entry<String, String> endpointData : endpoints.entrySet()) {
if (!"resourceKey".equals(endpointData.getKey())) {
String endpointType = endpointData.getKey();
String endpointUrl = endpointData.getValue();
String endpointConfigContext = builder.getConfigStringForWebSocketEndpointTemplate(endpointType, getWebsocketResourceKey(topic), endpointUrl);
GatewayContentDTO endpoint = new GatewayContentDTO();
// For WS APIs, resource type is not applicable,
// so we can just use the uriTemplate/uriMapping to identify the resource
endpoint.setName(getEndpointName(endpointConfigContext));
endpoint.setContent(endpointConfigContext);
endpointsToAdd.add(endpoint);
}
}
// once through resources is enough.
if (APIConstants.GRAPHQL_API.equals(api.getType())) {
break;
}
}
gatewayAPIDTO.setEndpointEntriesToBeAdd(addGatewayContentsToList(endpointsToAdd, gatewayAPIDTO.getEndpointEntriesToBeAdd()));
}
Aggregations