Search in sources :

Example 6 with APITemplateBuilder

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"));
}
Also used : APIManagerConfiguration(org.wso2.carbon.apimgt.impl.APIManagerConfiguration) APIManagerConfigurationService(org.wso2.carbon.apimgt.impl.APIManagerConfigurationService) APIManagerConfigurationServiceImpl(org.wso2.carbon.apimgt.impl.APIManagerConfigurationServiceImpl) Environment(org.wso2.carbon.apimgt.api.model.Environment) API(org.wso2.carbon.apimgt.api.model.API) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) WebsubSubscriptionConfiguration(org.wso2.carbon.apimgt.api.model.WebsubSubscriptionConfiguration) APITemplateBuilderImpl(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.APITemplateBuilderImpl) Test(org.junit.Test)

Example 7 with APITemplateBuilder

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"));
}
Also used : APIManagerConfiguration(org.wso2.carbon.apimgt.impl.APIManagerConfiguration) APIManagerConfigurationService(org.wso2.carbon.apimgt.impl.APIManagerConfigurationService) APIManagerConfigurationServiceImpl(org.wso2.carbon.apimgt.impl.APIManagerConfigurationServiceImpl) Environment(org.wso2.carbon.apimgt.api.model.Environment) API(org.wso2.carbon.apimgt.api.model.API) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) WebsubSubscriptionConfiguration(org.wso2.carbon.apimgt.api.model.WebsubSubscriptionConfiguration) APITemplateBuilderImpl(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.APITemplateBuilderImpl) Test(org.junit.Test)

Example 8 with APITemplateBuilder

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);
}
Also used : GatewayAPIDTO(org.wso2.carbon.apimgt.api.gateway.GatewayAPIDTO) HashMap(java.util.HashMap) WebSocketTopicMappingConfiguration(org.wso2.carbon.apimgt.api.model.WebSocketTopicMappingConfiguration) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) APITemplateBuilder(org.wso2.carbon.apimgt.impl.template.APITemplateBuilder) API(org.wso2.carbon.apimgt.api.model.API) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) HashMap(java.util.HashMap) Map(java.util.Map) GatewayContentDTO(org.wso2.carbon.apimgt.api.gateway.GatewayContentDTO) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 9 with APITemplateBuilder

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()));
}
Also used : URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) GatewayContentDTO(org.wso2.carbon.apimgt.api.gateway.GatewayContentDTO)

Aggregations

GatewayAPIDTO (org.wso2.carbon.apimgt.api.gateway.GatewayAPIDTO)5 GatewayContentDTO (org.wso2.carbon.apimgt.api.gateway.GatewayContentDTO)5 API (org.wso2.carbon.apimgt.api.model.API)5 APITemplateBuilder (org.wso2.carbon.apimgt.impl.template.APITemplateBuilder)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Test (org.junit.Test)3 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)3 URITemplate (org.wso2.carbon.apimgt.api.model.URITemplate)3 APITemplateBuilderImpl (org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.APITemplateBuilderImpl)3 HashSet (java.util.HashSet)2 JSONObject (org.json.simple.JSONObject)2 ClientCertificateDTO (org.wso2.carbon.apimgt.api.dto.ClientCertificateDTO)2 Environment (org.wso2.carbon.apimgt.api.model.Environment)2 WebsubSubscriptionConfiguration (org.wso2.carbon.apimgt.api.model.WebsubSubscriptionConfiguration)2 APIManagerConfiguration (org.wso2.carbon.apimgt.impl.APIManagerConfiguration)2 APIManagerConfigurationService (org.wso2.carbon.apimgt.impl.APIManagerConfigurationService)2 APIManagerConfigurationServiceImpl (org.wso2.carbon.apimgt.impl.APIManagerConfigurationServiceImpl)2 APIDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIDTO)2 ArrayList (java.util.ArrayList)1