Java Code Examples for org.wso2.carbon.apimgt.api.model.API#setEndpointConfig()

The following examples show how to use org.wso2.carbon.apimgt.api.model.API#setEndpointConfig() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OASParserUtilTest.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
@Test
public void testGenerateOASConfigForDefaultEndpoints() throws Exception {
    String endpoints = IOUtils.toString(
            getClass().getClassLoader().getResourceAsStream("definitions" + File.separator + "endpointsData.json"),
            "UTF-8");
    JSONObject jsonObject = new JSONObject(endpoints);

    APIIdentifier identifier = new APIIdentifier("provider", "name", "version");
    API api = new API(identifier);
    JsonNode prodNode;
    JsonNode sandNode;

    String defaultEndpoints = jsonObject.getJSONObject("default_endpoints").toString();
    api.setEndpointConfig(defaultEndpoints);
    prodNode = OASParserUtil.generateOASConfigForEndpoints(api, true);
    Assert.assertNotNull(prodNode);
    Assert.assertEquals(APIConstants.ENDPOINT_TYPE_DEFAULT,
            prodNode.get(APIConstants.X_WSO2_ENDPOINT_TYPE).asText());

    sandNode = OASParserUtil.generateOASConfigForEndpoints(api, false);
    Assert.assertNotNull(sandNode);
    Assert.assertEquals(APIConstants.ENDPOINT_TYPE_DEFAULT,
            prodNode.get(APIConstants.X_WSO2_ENDPOINT_TYPE).asText());
}
 
Example 2
Source File: APIGatewayManagerTest.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
@Test public void testPublishingNewRESTAPIWithCustomInSequenceToGateway()
        throws AxisFault, APIManagementException, XMLStreamException, RegistryException {
    API api = new API(apiIdentifier);
    api.setType("HTTP");
    api.setAsPublishedDefaultVersion(true);
    api.setImplementation("ENDPOINT");
    api.setEndpointConfig(prodEndpointConfig);
    api.setInSequence(inSequenceName);
    api.setAsDefaultVersion(true);
    api.setSwaggerDefinition(swaggerDefinition);
    api.setUUID(apiUUId);
    APITemplateBuilder apiTemplateBuilder = new APITemplateBuilderImpl(api);
    Set<String> environments = new HashSet<String>();
    environments.add(prodEnvironmentName);
    environments.add(null);
    api.setEnvironments(environments);
    Mockito.when(apiGatewayAdminClient.getApi(tenantDomain, apiIdentifier)).thenReturn(null);
    PowerMockito.when(APIUtil.getCustomSequence(inSequenceName, tenantID, "in", api.getId()))
            .thenReturn(AXIOMUtil.stringToOM(testSequenceDefinition));
    Assert.assertEquals(gatewayManager.publishToGateway(api, apiTemplateBuilder, tenantDomain).size(), 0);
}
 
Example 3
Source File: APIGatewayManagerTest.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
@Test public void testPublishingNewRESTAPIWithCustomOutSequenceToGateway()
        throws AxisFault, APIManagementException, XMLStreamException, RegistryException {
    API api = new API(apiIdentifier);
    api.setType("HTTP");
    api.setAsPublishedDefaultVersion(true);
    api.setImplementation("ENDPOINT");
    api.setEndpointConfig(prodEndpointConfig);
    api.setOutSequence(outSequenceName);
    api.setAsDefaultVersion(true);
    api.setSwaggerDefinition(swaggerDefinition);
    api.setUUID(apiUUId);
    APITemplateBuilder apiTemplateBuilder = new APITemplateBuilderImpl(api);
    Set<String> environments = new HashSet<String>();
    environments.add(prodEnvironmentName);
    environments.add(null);
    api.setEnvironments(environments);
    Mockito.when(apiGatewayAdminClient.getApi(tenantDomain, apiIdentifier)).thenReturn(null);
    PowerMockito.when(APIUtil.getCustomSequence(outSequenceName, tenantID, "out", api.getId()))
            .thenReturn(AXIOMUtil.stringToOM(testSequenceDefinition));
    Assert.assertEquals(gatewayManager.publishToGateway(api, apiTemplateBuilder, tenantDomain).size(), 0);
}
 
Example 4
Source File: EndpointBckConfigContextTest.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
@Test
public void testEndpointBckConfigContext() throws Exception {
    API api = new API(new APIIdentifier("admin", "TestAPI", "1.0.0"));
    api.setStatus(APIConstants.CREATED);
    api.setContextTemplate("/");
    String url = "http://maps.googleapis.com/maps/api/geocode/json?address=Colombo";
    api.setUrl(url);
    api.setSandboxUrl(url);
    ConfigContext configcontext = new APIConfigContext(api);
    EndpointBckConfigContext endpointBckConfigContext = new EndpointBckConfigContext(configcontext, api);
    Assert.assertTrue(api.getEndpointConfig().contains(url));
    //setting an empty string as the endpoint config and checking the value which is returned
    api.setEndpointConfig("");
    String endpoint_config = "{\"production_endpoints\":{\"url\":\"" + api.getUrl() + "\", \"config\":null}," +
            "\"sandbox_endpoint\":{\"url\":\"" + api.getSandboxUrl() + "\"," +
            "\"config\":null},\"endpoint_type\":\"http\"}";
    EndpointBckConfigContext secondEndpointBckConfigContext = new EndpointBckConfigContext(configcontext, api);
    Assert.assertTrue(api.getEndpointConfig().contains(endpoint_config));
    //setting null as the endpoint config and checking the value which is returned
    api.setEndpointConfig(null);
    EndpointBckConfigContext thirdEndpointBckConfigContext = new EndpointBckConfigContext(configcontext, api);
    Assert.assertTrue(api.getEndpointConfig().contains(endpoint_config));
}
 
Example 5
Source File: APIGatewayManagerTest.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
@Test public void testPublishingExistingRESTAPIWithCustomInSequenceToGateway()
        throws AxisFault, APIManagementException, XMLStreamException, RegistryException {
    API api = new API(apiIdentifier);
    api.setType("HTTP");
    api.setAsPublishedDefaultVersion(true);
    api.setImplementation("ENDPOINT");
    api.setEndpointConfig(prodEndpointConfig);
    api.setInSequence(inSequenceName);
    api.setAsDefaultVersion(true);
    api.setSwaggerDefinition(swaggerDefinition);
    api.setUUID(apiUUId);
    APITemplateBuilder apiTemplateBuilder = new APITemplateBuilderImpl(api);
    Set<String> environments = new HashSet<String>();
    environments.add(prodEnvironmentName);
    environments.add(null);
    api.setEnvironments(environments);
    Mockito.when(apiGatewayAdminClient.getApi(tenantDomain, apiIdentifier)).thenReturn(apiData);
    PowerMockito.when(APIUtil.getCustomSequence(inSequenceName, tenantID, "in", api.getId()))
            .thenReturn(AXIOMUtil.stringToOM(testSequenceDefinition));
    Assert.assertEquals(gatewayManager.publishToGateway(api, apiTemplateBuilder, tenantDomain).size(), 0);
}
 
Example 6
Source File: APIGatewayManagerTest.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
@Test public void testPublishingExistingRESTAPIWithCustomOutSequenceToGateway()
        throws AxisFault, APIManagementException, XMLStreamException, RegistryException {
    API api = new API(apiIdentifier);
    api.setType("HTTP");
    api.setAsPublishedDefaultVersion(true);
    api.setImplementation("ENDPOINT");
    api.setEndpointConfig(prodEndpointConfig);
    api.setOutSequence(outSequenceName);
    api.setAsDefaultVersion(true);
    api.setSwaggerDefinition(swaggerDefinition);
    api.setUUID(apiUUId);
    APITemplateBuilder apiTemplateBuilder = new APITemplateBuilderImpl(api);
    Set<String> environments = new HashSet<String>();
    environments.add(prodEnvironmentName);
    environments.add(null);
    api.setEnvironments(environments);
    Mockito.when(apiGatewayAdminClient.getApi(tenantDomain, apiIdentifier)).thenReturn(apiData);
    PowerMockito.when(APIUtil.getCustomSequence(outSequenceName, tenantID, "out", api.getId()))
            .thenReturn(AXIOMUtil.stringToOM(testSequenceDefinition));
    Assert.assertEquals(gatewayManager.publishToGateway(api, apiTemplateBuilder, tenantDomain).size(), 0);
}
 
Example 7
Source File: EnvironmentConfigContextTest.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
@Test
public void testEnvironmentConfigContext() throws Exception {

    API api = new API(new APIIdentifier("admin", "TestAPI", "1.0.0"));
    api.setStatus(APIConstants.CREATED);
    api.setContextTemplate("/");
    String url = "http://maps.googleapis.com/maps/api/geocode/json?address=Colombo";
    String endpointConfig = "{\"production_endpoints\":{\"url\":\"" + url + "\", \"config\":null}," +
            "\"sandbox_endpoint\":{\"url\":\"" + url + "\",\"config\":null},\"endpoint_type\":\"http\"}";
    api.setEndpointConfig(endpointConfig);
    api.setUrl(url);
    api.setSandboxUrl(url);
    ConfigContext configcontext = new APIConfigContext(api);
    Environment environment = new Environment();
    environment.setType("production");
    EnvironmentConfigContext environmentConfigContext = new EnvironmentConfigContext(configcontext, environment);
    Assert.assertNotNull(environmentConfigContext.getContext().get("environment"));
    Assert.assertNotNull(environmentConfigContext.getContext().get("environmentType"));
}
 
Example 8
Source File: APIGatewayManagerTest.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
@Test public void testFailureWhilePublishingAPIUpdateToGateway() throws Exception {
    API api = new API(apiIdentifier);
    api.setType("HTTP");
    api.setAsPublishedDefaultVersion(true);
    api.setImplementation("ENDPOINT");
    api.setEndpointConfig(prodEndpointConfig);
    api.setInSequence(inSequenceName);
    api.setAsDefaultVersion(true);
    api.setSwaggerDefinition(swaggerDefinition);
    api.setUUID(apiUUId);
    APITemplateBuilder apiTemplateBuilder = new APITemplateBuilderImpl(api);
    Set<String> environments = new HashSet<String>();
    environments.add(prodEnvironmentName);
    environments.add(null);
    OMElement inSequence = AXIOMUtil.stringToOM(testSequenceDefinition);
    api.setEnvironments(environments);
    Mockito.when(apiGatewayAdminClient.getApi(tenantDomain, apiIdentifier)).thenReturn(apiData);
    PowerMockito.when(APIUtil.getCustomSequence(inSequenceName, tenantID, "in", api.getId()))
            .thenReturn(inSequence);
    PowerMockito.when(APIUtil.isProductionEndpointsExists(Mockito.anyString())).thenReturn(true);
    PowerMockito.when(APIUtil.isSequenceDefined(Mockito.anyString())).thenReturn(true);
    //Test API deployment failure when custom sequence update failed
    Map<String, String> failedEnvironmentsMap = gatewayManager
            .publishToGateway(api, apiTemplateBuilder, tenantDomain);
    Assert.assertEquals(failedEnvironmentsMap.size(), 1);
    Assert.assertTrue(failedEnvironmentsMap.keySet().contains(prodEnvironmentName));
}
 
Example 9
Source File: EndpointConfigContextTest.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
@Test (expected = APITemplateException.class)
public void testEndpointConfigContext() throws Exception {

    API api = new API(new APIIdentifier("admin", "TestAPI", "1.0.0"));
    api.setStatus(APIConstants.CREATED);
    api.setContextTemplate("/");
    String url = "http://maps.googleapis.com/maps/api/geocode/json?address=Colombo";
    String endpointConfig = "{\"production_endpoints\":{\"url\":\"" + url + "\", \"config\":null}," +
            "\"sandbox_endpoint\":{\"url\":\"" + url + "\",\"config\":null},\"endpoint_type\":\"http\"}";
    api.setEndpointConfig(endpointConfig);
    api.setUrl(url);
    api.setSandboxUrl(url);
    ConfigContext configcontext = new APIConfigContext(api);
    EndpointConfigContext endpointConfigContext = new EndpointConfigContext(configcontext, api);
    endpointConfigContext.validate();
    Assert.assertNotNull(endpointConfigContext.getContext().get("endpoint_config"));
    //set an empty string and check the validation
    endpointConfig = "";
    api.setEndpointConfig(endpointConfig);
    endpointConfigContext = new EndpointConfigContext(configcontext, api);
    endpointConfigContext.validate();
    //set a null value and check the validation
    endpointConfig = null;
    api.setEndpointConfig(endpointConfig);
    endpointConfigContext = new EndpointConfigContext(configcontext, api);
    endpointConfigContext.validate();
    //set invalid value and check the validation
    String invalidEndpointConfig = "\"production_endpoints\"{\"url\":\"" + url + "\", \"config\":null}," +
            "\"sandbox_endpoint\":{\"url\":\"" + url + "\",\"config\":null},\"endpoint_type\":\"http\"";
    api.setEndpointConfig(invalidEndpointConfig);
    endpointConfigContext = new EndpointConfigContext(configcontext, api);
    endpointConfigContext.validate();
}
 
Example 10
Source File: SecurityConfigContextTest.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
@Test
public void testSecurityConfigContextPerEndpointSandbox() throws Exception {

    String json = "{\"endpoint_security\":{\n" +
            "  \"sandbox\":{\n" +
            "    \"enabled\":true,\n" +
            "    \"type\":\"DIGEST\",\n" +
            "    \"username\":\"admin\",\n" +
            "    \"password\":\"admin123#QA\"\n" +
            "  }\n" +
            "  }\n" +
            "}";
    API api = new API(new APIIdentifier("admin", "TestAPI", "1.0.0"));
    api.setStatus(APIConstants.CREATED);
    api.setContextTemplate("/");
    api.setTransports(Constants.TRANSPORT_HTTP);
    api.setEndpointConfig(json);
    ConfigContext configcontext = new APIConfigContext(api);
    Mockito.when(apiManagerConfiguration.getFirstProperty(APIConstants.API_SECUREVAULT_ENABLE)).thenReturn("true");
    SecurityConfigContext securityConfigContext =
            new SecurityConfigContextWrapper(configcontext, api, apiManagerConfiguration);
    securityConfigContext.validate();
    VelocityContext velocityContext = securityConfigContext.getContext();
    Assert.assertNotNull(velocityContext.get("endpoint_security"));
    Map<String, EndpointSecurityModel> endpointSecurityModelMap =
            (Map<String, EndpointSecurityModel>) velocityContext.get("endpoint_security");
    EndpointSecurityModel sandbox = endpointSecurityModelMap.get("sandbox");
    Assert.assertTrue("Property enabled cannot be false.", sandbox.isEnabled());
    Assert.assertTrue("Property type cannot be other.", sandbox.getType().equalsIgnoreCase("digest"));
    Assert.assertTrue("Property username does not match.", "admin".equals(sandbox.getUsername()));
    Assert.assertTrue("Property base64value does not match. ",
            new String(Base64.encodeBase64("admin:admin123#QA".getBytes()))
                    .equalsIgnoreCase(sandbox.getBase64EncodedPassword()));
    Assert.assertTrue("Property securevault_alias does not match.",
            "admin--TestAPI1.0.0--sandbox".equalsIgnoreCase(sandbox.getAlias()));
    Assert.assertTrue("Property isSecureVaultEnabled cannot be false. ",
            velocityContext.get("isSecureVaultEnabled").equals(true));
    EndpointSecurityModel production = endpointSecurityModelMap.get("production");
    Assert.assertFalse("Property enabled cannot be true.", production.isEnabled());
}
 
Example 11
Source File: SecurityConfigContextTest.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
@Test
public void testSecurityConfigContextPerEndpointProductionType() throws Exception {

    String json = "{\"endpoint_security\":{\n" +
            "  \"production\":{\n" +
            "    \"enabled\":true,\n" +
            "    \"type\":\"BASIC\",\n" +
            "    \"username\":\"admin\",\n" +
            "    \"password\":\"admin123#QA\"\n" +
            "  }\n" +
            "  }\n" +
            "}";
    API api = new API(new APIIdentifier("admin", "TestAPI", "1.0.0"));
    api.setStatus(APIConstants.CREATED);
    api.setContextTemplate("/");
    api.setTransports(Constants.TRANSPORT_HTTP);
    api.setEndpointConfig(json);
    ConfigContext configcontext = new APIConfigContext(api);
    Mockito.when(apiManagerConfiguration.getFirstProperty(APIConstants.API_SECUREVAULT_ENABLE)).thenReturn("true");
    SecurityConfigContext securityConfigContext =
            new SecurityConfigContextWrapper(configcontext, api, apiManagerConfiguration);
    securityConfigContext.validate();
    VelocityContext velocityContext = securityConfigContext.getContext();
    Assert.assertNotNull(velocityContext.get("endpoint_security"));
    Map<String, EndpointSecurityModel> endpointSecurityModelMap =
            (Map<String, EndpointSecurityModel>) velocityContext.get("endpoint_security");
    EndpointSecurityModel production = endpointSecurityModelMap.get("production");
    Assert.assertTrue("Property enabled cannot be false.", production.isEnabled());
    Assert.assertTrue("Property type cannot be other.", production.getType().equalsIgnoreCase("basic"));
    Assert.assertTrue("Property username does not match.", "admin".equals(production.getUsername()));
    Assert.assertTrue("Property base64value does not match. ",
            new String(Base64.encodeBase64("admin:admin123#QA".getBytes()))
                    .equalsIgnoreCase(production.getBase64EncodedPassword()));
    Assert.assertTrue("Property securevault_alias does not match.",
            "admin--TestAPI1.0.0--production".equalsIgnoreCase(production.getAlias()));
    Assert.assertTrue("Property isSecureVaultEnabled cannot be false. ",
            velocityContext.get("isSecureVaultEnabled").equals(true));
    EndpointSecurityModel sandbox = endpointSecurityModelMap.get("sandbox");
    Assert.assertFalse("Property enabled cannot be true.", sandbox.isEnabled());
}
 
Example 12
Source File: EndpointBckConfigContext.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
public EndpointBckConfigContext(ConfigContext context, API api) {
    super(context);
    //this.api = api;
    //check if endpoint_config not set
    String endpoint_config = api.getEndpointConfig();
    if(endpoint_config == null || "".equals(endpoint_config)){
        // Without setting the context make the endpoint_config json of api
        // The following config will be picked up by EndpointConfigContext
        endpoint_config = "{\"production_endpoints\":{\"url\":\""+ api.getUrl()+"\", \"config\":null},\"sandbox_endpoint\":{\"url\":\""+api.getSandboxUrl()+"\",\"config\":null},\"endpoint_type\":\"http\"}";
        api.setEndpointConfig(endpoint_config);
    }
}
 
Example 13
Source File: APIGatewayManagerTest.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
@Test public void testPublishingExistingRESTAPIToGateway()
        throws AxisFault, APIManagementException, XMLStreamException, RegistryException {

    API api = new API(apiIdentifier);
    api.setType("HTTP");
    api.setAsPublishedDefaultVersion(true);
    api.setAsDefaultVersion(true);
    api.setSwaggerDefinition(swaggerDefinition);
    api.setUUID(apiUUId);
    APITemplateBuilder apiTemplateBuilder = new APITemplateBuilderImpl(api);
    Set<String> environments = new HashSet<String>();
    environments.add(prodEnvironmentName);
    environments.add(null);
    api.setEnvironments(environments);
    Mockito.when(apiGatewayAdminClient.getApi(tenantDomain, apiIdentifier)).thenReturn(apiData);
    Mockito.when(apiGatewayAdminClient.getDefaultApi(tenantDomain, apiIdentifier)).thenReturn(defaultAPIdata);

    //Test deleting existing API from production environment, if matching producntion endpoint configuration is
    // not found in API's endpoint config
    api.setEndpointConfig(sandBoxEndpointConfig);
    Assert.assertEquals(gatewayManager.publishToGateway(api, apiTemplateBuilder, tenantDomain).size(), 0);

    //Test updating 'INLINE' type REST API to gateway
    api.setImplementation("INLINE");
    api.setEndpointConfig(prodEndpointConfig);
    Assert.assertEquals(gatewayManager.publishToGateway(api, apiTemplateBuilder, tenantDomain).size(), 0);

    //Test updating 'ENDPOINT' type REST API to gateway
    api.setImplementation("ENDPOINT");
    Assert.assertEquals(gatewayManager.publishToGateway(api, apiTemplateBuilder, tenantDomain).size(), 0);

    //Test updating default version of the API
    Mockito.when(apiGatewayAdminClient.getDefaultApi(tenantDomain, apiIdentifier)).thenReturn(defaultAPIdata);
    Assert.assertEquals(gatewayManager.publishToGateway(api, apiTemplateBuilder, tenantDomain).size(), 0);

    //Test deploying API, if secure vault is enabled
    api.setEndpointSecured(true);
    Mockito.when(config.getFirstProperty(APIConstants.API_SECUREVAULT_ENABLE)).thenReturn("true");
    Assert.assertEquals(gatewayManager.publishToGateway(api, apiTemplateBuilder, tenantDomain).size(), 0);
}
 
Example 14
Source File: APIGatewayManagerTest.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
@Test public void testPublishingNewRESTAPIWithAPIFaultSequence()
        throws AxisFault, APIManagementException, XMLStreamException, RegistryException {
    API api = new API(apiIdentifier);
    api.setType("HTTP");
    api.setAsPublishedDefaultVersion(true);
    api.setImplementation("ENDPOINT");
    api.setEndpointConfig(prodEndpointConfig);
    api.setFaultSequence(faultSequenceName);
    api.setAsDefaultVersion(true);
    api.setSwaggerDefinition(swaggerDefinition);
    api.setUUID(apiUUId);
    APITemplateBuilder apiTemplateBuilder = new APITemplateBuilderImpl(api);
    Set<String> environments = new HashSet<String>();
    environments.add(prodEnvironmentName);
    environments.add(null);
    api.setEnvironments(environments);
    Mockito.when(apiGatewayAdminClient.getApi(tenantDomain, apiIdentifier)).thenReturn(null);
    PowerMockito.when(APIUtil.getCustomSequence(faultSequenceName, tenantID, "fault", api.getId()))
            .thenReturn(AXIOMUtil.stringToOM(testSequenceDefinition));

    //Test deploying per API defined fault sequence with API
    PowerMockito.when(APIUtil.isPerAPISequence(faultSequenceName, tenantID, apiIdentifier, "fault"))
            .thenReturn(true);
    Assert.assertEquals(gatewayManager.publishToGateway(api, apiTemplateBuilder, tenantDomain).size(), 0);

    //Test deploying newly defined global sequence with API
    PowerMockito.when(APIUtil.isPerAPISequence(faultSequenceName, tenantID, apiIdentifier, "fault"))
            .thenReturn(false);
    Assert.assertEquals(gatewayManager.publishToGateway(api, apiTemplateBuilder, tenantDomain).size(), 0);

    //If fault sequence has not been defined for the API, omit deploying/updating and remove if already exists
    api.setFaultSequence(null);
    Assert.assertEquals(gatewayManager.publishToGateway(api, apiTemplateBuilder, tenantDomain).size(), 0);
}
 
Example 15
Source File: SecurityConfigContextTest.java    From carbon-apimgt with Apache License 2.0 4 votes vote down vote up
@Test
public void testSecurityConfigContextPerEndpointBothType() throws Exception {

    String json = "{\"endpoint_security\":{\n" +
            "  \"production\":{\n" +
            "    \"enabled\":true,\n" +
            "    \"type\":\"BASIC\",\n" +
            "    \"username\":\"admin\",\n" +
            "    \"password\":\"admin123#QA\"\n" +
            "  },\n" +
            "  \"sandbox\":{\n" +
            "    \"enabled\":true,\n" +
            "    \"type\":\"DIGEST\",\n" +
            "    \"username\":\"admin\",\n" +
            "    \"password\":\"admin123\"\n" +
            "  }\n" +
            "  }\n" +
            "}";
    API api = new API(new APIIdentifier("admin", "TestAPI", "1.0.0"));
    api.setStatus(APIConstants.CREATED);
    api.setContextTemplate("/");
    api.setTransports(Constants.TRANSPORT_HTTP);
    api.setEndpointConfig(json);
    ConfigContext configcontext = new APIConfigContext(api);
    Mockito.when(apiManagerConfiguration.getFirstProperty(APIConstants.API_SECUREVAULT_ENABLE)).thenReturn("true");
    SecurityConfigContext securityConfigContext =
            new SecurityConfigContextWrapper(configcontext, api, apiManagerConfiguration);
    securityConfigContext.validate();
    VelocityContext velocityContext = securityConfigContext.getContext();
    Assert.assertNotNull(velocityContext.get("endpoint_security"));
    Map<String, EndpointSecurityModel> endpointSecurityModelMap =
            (Map<String, EndpointSecurityModel>) velocityContext.get("endpoint_security");
    EndpointSecurityModel production = endpointSecurityModelMap.get("production");
    Assert.assertTrue("Property enabled cannot be false.", production.isEnabled());
    Assert.assertTrue("Property type cannot be other.", production.getType().equalsIgnoreCase("basic"));
    Assert.assertTrue("Property username does not match.", "admin".equals(production.getUsername()));
    Assert.assertTrue("Property base64value does not match. ",
            new String(Base64.encodeBase64("admin:admin123#QA".getBytes()))
                    .equalsIgnoreCase(production.getBase64EncodedPassword()));
    Assert.assertTrue("Property securevault_alias does not match.",
            "admin--TestAPI1.0.0--production".equalsIgnoreCase(production.getAlias()));
    EndpointSecurityModel sandbox = endpointSecurityModelMap.get("sandbox");
    Assert.assertTrue("Property enabled cannot be false.", sandbox.isEnabled());
    Assert.assertTrue("Property type cannot be other.", sandbox.getType().equalsIgnoreCase("digest"));
    Assert.assertTrue("Property username does not match.", "admin".equals(sandbox.getUsername()));
    Assert.assertTrue("Property base64value does not match. ",
            new String(Base64.encodeBase64("admin:admin123".getBytes()))
                    .equalsIgnoreCase(sandbox.getBase64EncodedPassword()));
    Assert.assertTrue("Property securevault_alias does not match.",
            "admin--TestAPI1.0.0--sandbox".equalsIgnoreCase(sandbox.getAlias()));
    Assert.assertTrue("Property isSecureVaultEnabled cannot be false. ",
            velocityContext.get("isSecureVaultEnabled").equals(true));
}
 
Example 16
Source File: APIGatewayManagerTest.java    From carbon-apimgt with Apache License 2.0 4 votes vote down vote up
@Test public void testFailureWhilePublishingNewAPIToGateway() throws Exception {
    API api = new API(apiIdentifier);
    api.setType("HTTP");
    api.setAsPublishedDefaultVersion(true);
    api.setImplementation("ENDPOINT");
    api.setEndpointConfig(prodEndpointConfig);
    api.setInSequence(inSequenceName);
    api.setAsDefaultVersion(true);
    api.setSwaggerDefinition(swaggerDefinition);
    api.setUUID(apiUUId);
    APITemplateBuilder apiTemplateBuilder = new APITemplateBuilderImpl(api);
    Set<String> environments = new HashSet<String>();
    environments.add(prodEnvironmentName);
    environments.add(null);
    OMElement inSequence = AXIOMUtil.stringToOM(testSequenceDefinition);
    OMElement faultSequence = AXIOMUtil.stringToOM(testSequenceDefinition);
    api.setEnvironments(environments);
    Mockito.when(apiGatewayAdminClient.getApi(tenantDomain, apiIdentifier)).thenReturn(null);
    PowerMockito.when(APIUtil.getCustomSequence(inSequenceName, tenantID, "in", api.getId()))
            .thenReturn(inSequence);
    PowerMockito.when(APIUtil.isProductionEndpointsExists(Mockito.anyString())).thenReturn(true);
    PowerMockito.when(APIUtil.isSequenceDefined(Mockito.anyString())).thenReturn(true);

    Map<String, String> failedEnvironmentsMap = gatewayManager
            .publishToGateway(api, apiTemplateBuilder, tenantDomain);
    Assert.assertEquals(failedEnvironmentsMap.size(), 1);
    Assert.assertTrue(failedEnvironmentsMap.keySet().contains(prodEnvironmentName));

    //Test failure to deploy API when setting secure vault property failed
    Mockito.doThrow(new APIManagementException("Failed to set secure vault property for the tenant"))
            .when(apiGatewayAdminClient).setSecureVaultProperty(api, tenantDomain);
    api.setEndpointSecured(true);
    Mockito.when(config.getFirstProperty(APIConstants.API_SECUREVAULT_ENABLE)).thenReturn("true");
    Mockito.when(config.getFirstProperty(APIConstants.ENABLE_MTLS_FOR_APIS)).thenReturn("true");
    Map<String, String> failedEnvironmentsMap2 = gatewayManager
            .publishToGateway(api, apiTemplateBuilder, tenantDomain);
    Assert.assertEquals(failedEnvironmentsMap2.size(), 1);
    Assert.assertTrue(failedEnvironmentsMap2.keySet().contains(prodEnvironmentName ));

    //Test failure to deploy API when fault sequence deployment failed
    api.setFaultSequence(faultSequenceName);
    PowerMockito.when(APIUtil.getCustomSequence(faultSequenceName, tenantID, "fault", api.getId()))
            .thenReturn(faultSequence);
    Map<String, String> failedEnvironmentsMap3 = gatewayManager
            .publishToGateway(api, apiTemplateBuilder, tenantDomain);
    Assert.assertEquals(failedEnvironmentsMap3.size(), 1);
    Assert.assertTrue(failedEnvironmentsMap3.keySet().contains(prodEnvironmentName));

    //Test throwing AxisFault when Gateway client initialisation failed
    Mockito.doThrow(new AxisFault("Error occurred while deploying sequence")).when(apiGatewayAdminClient)
            .getApi(tenantDomain, apiIdentifier);
    Map<String, String> failedEnvironmentsMap4 = gatewayManager
            .publishToGateway(api, apiTemplateBuilder, tenantDomain);
    Assert.assertEquals(failedEnvironmentsMap4.size(), 1);
    Assert.assertTrue(failedEnvironmentsMap4.keySet().contains(prodEnvironmentName));
}
 
Example 17
Source File: SecurityConfigContextTest.java    From carbon-apimgt with Apache License 2.0 4 votes vote down vote up
@Test
public void testSecurityConfigContextIgnoringEndpointConfig() throws Exception {

    String json = "{\"endpoint_security\":{\n" +
            "  \"sandbox\":{\n" +
            "    \"enabled\":true,\n" +
            "    \"type\":\"DIGEST\",\n" +
            "    \"username\":\"admin\",\n" +
            "    \"password\":\"admin123#QA\"\n" +
            "  }\n" +
            "  }\n" +
            "}";

    API api = new API(new APIIdentifier("admin", "TestAPI", "1.0.0"));
    api.setStatus(APIConstants.CREATED);
    api.setContextTemplate("/");
    api.setTransports(Constants.TRANSPORT_HTTP);
    api.setEndpointConfig(json);
    api.setEndpointUTUsername("admin");
    api.setEndpointUTPassword("admin123");
    api.setEndpointSecured(true);
    api.setEndpointAuthDigest(true);
    ConfigContext configcontext = new APIConfigContext(api);
    Mockito.when(apiManagerConfiguration.getFirstProperty(APIConstants.API_SECUREVAULT_ENABLE)).thenReturn("true");
    SecurityConfigContext securityConfigContext =
            new SecurityConfigContextWrapper(configcontext, api, apiManagerConfiguration);
    securityConfigContext.validate();
    VelocityContext velocityContext = securityConfigContext.getContext();
    Assert.assertNotNull(velocityContext.get("endpoint_security"));
    Map<String, EndpointSecurityModel> endpointSecurityModelMap =
            (Map<String, EndpointSecurityModel>) velocityContext.get("endpoint_security");
    for (Map.Entry<String, EndpointSecurityModel> endpointSecurityModelEntry : endpointSecurityModelMap
            .entrySet()) {
        Assert.assertTrue("Property isEndpointSecured cannot be false.",
                endpointSecurityModelEntry.getValue().isEnabled());
        Assert.assertTrue("Property isEndpointAuthDigest cannot be false.",
                endpointSecurityModelEntry.getValue().getType().contains("digest"));
        Assert.assertTrue("Property username does not match.",
                "admin".equals(endpointSecurityModelEntry.getValue().getUsername()));
        Assert.assertTrue("Property base64unpw does not match. ",
                new String(Base64.encodeBase64("admin:admin123".getBytes()))
                        .equalsIgnoreCase(endpointSecurityModelEntry.getValue().getBase64EncodedPassword()));
        Assert.assertTrue("Property securevault_alias does not match.",
                "admin--TestAPI1.0.0".equalsIgnoreCase(endpointSecurityModelEntry.getValue().getAlias()));
    }
    Assert.assertTrue("Property isSecureVaultEnabled cannot be false. ",
            velocityContext.get("isSecureVaultEnabled").equals(true));
}
 
Example 18
Source File: APIGatewayManagerTest.java    From carbon-apimgt with Apache License 2.0 4 votes vote down vote up
@Test public void testPublishingNewAPIToGateway()
        throws Exception {

    API api = new API(apiIdentifier);
    api.setType("HTTP");
    api.setAsPublishedDefaultVersion(true);
    api.setAsDefaultVersion(true);
    APITemplateBuilder apiTemplateBuilder = new APITemplateBuilderImpl(api);
    Set<String> environments = new HashSet<String>();
    environments.add(prodEnvironmentName);
    environments.add(null);
    Mockito.when(apiGatewayAdminClient.getApi(tenantDomain, apiIdentifier)).thenReturn(null);

    //Test when environments are not defined for API
    Assert.assertEquals(gatewayManager.publishToGateway(api, apiTemplateBuilder, tenantDomain).size(), 0);

    //Test adding LocalEntry
    api.setEnvironments(environments);
    api.setEndpointConfig(sandBoxEndpointConfig);
    api.setSwaggerDefinition(swaggerDefinition);
    api.setUUID(apiUUId);

    //Test when API's environment endpoint configuration is not available
    Assert.assertEquals(gatewayManager.publishToGateway(api, apiTemplateBuilder, tenantDomain).size(), 0);

    //Test deploying 'INLINE' type REST API to gateway
    api.setImplementation("INLINE");
    api.setEndpointConfig(prodEndpointConfig);
    Assert.assertEquals(gatewayManager.publishToGateway(api, apiTemplateBuilder, tenantDomain).size(), 0);

    //Test deploying 'ENDPOINT' type REST API to gateway
    api.setImplementation("ENDPOINT");
    Assert.assertEquals(gatewayManager.publishToGateway(api, apiTemplateBuilder, tenantDomain).size(), 0);

    //Test deploying default version of the API and updating the existing default API
    Mockito.when(apiGatewayAdminClient.getDefaultApi(tenantDomain, apiIdentifier)).thenReturn(defaultAPIdata);
    Assert.assertEquals(gatewayManager.publishToGateway(api, apiTemplateBuilder, tenantDomain).size(), 0);

    //Test deploying API, if secure vault is enabled
    api.setEndpointSecured(true);
    Mockito.when(config.getFirstProperty(APIConstants.API_SECUREVAULT_ENABLE)).thenReturn("true");
    Assert.assertEquals(gatewayManager.publishToGateway(api, apiTemplateBuilder, tenantDomain).size(), 0);

    //Test publishing WebSocket API
    api.setType("WS");
    Assert.assertEquals(gatewayManager.publishToGateway(api, apiTemplateBuilder, tenantDomain).size(), 0);

}