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

The following examples show how to use org.wso2.carbon.apimgt.api.model.API#setStatus() . 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: APIExecutorTestCase.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
@Test
public void testExecuteWithDeprecated() throws Exception {
    Mockito.when(genericArtifact.isLCItemChecked(0, APIConstants.API_LIFE_CYCLE)).thenReturn(true);
    List<API> apiList = new ArrayList<API>();
    APIIdentifier apiIdTemp = Mockito.mock(APIIdentifier.class);

    Mockito.when(apiIdTemp.getProviderName()).thenReturn(USER_NAME);
    Mockito.when(apiIdTemp.getVersion()).thenReturn("1.0.0");
    Mockito.when(apiIdTemp.getApiName()).thenReturn(API_NAME);
    API apiTemp = new API(apiIdTemp);

    apiTemp.setStatus(APIConstants.PUBLISHED);
    apiList.add(apiTemp);
    Mockito.when(apiProvider.getAPIsByProvider(USER_NAME)).thenReturn(apiList);

    APIExecutor apiExecutor = new APIExecutor();
    boolean isExecuted = apiExecutor.execute(requestContext, "CREATED", "PUBLISHED");
    Assert.assertTrue(isExecuted);
}
 
Example 2
Source File: APIMgtDAOTest.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
@Test
 public void testAddAndConvertNullThrottlingTiers() throws APIManagementException {

     //Adding an API with a null THROTTLING_TIER should automatically convert it to Unlimited
     APIIdentifier apiIdentifier = new APIIdentifier("testAddAndGetApi", "testAddAndGetApi", "1.0.0");
     API api = new API(apiIdentifier);
     api.setContext("/testAddAndGetApi");
     api.setContextTemplate("/testAddAndGetApi/{version}");
     Set<URITemplate> uriTemplates = new HashSet<URITemplate>();
     uriTemplates.add(getUriTemplate("/abc", "GET", "Any", "read", null));
     api.setUriTemplates(uriTemplates);
     api.setScopes(getScopes());
     api.setStatus(APIConstants.PUBLISHED);
     api.setAsDefaultVersion(true);
     int apiId = apiMgtDAO.addAPI(api, -1234);
     apiMgtDAO.addURITemplates(apiId, api, -1234);
     HashMap<String, String> result1 = apiMgtDAO.getURITemplatesPerAPIAsString(apiIdentifier);
     Assert.assertTrue(result1.containsKey("/abc::GET::Any::Unlimited::abcd defgh fff"));

     //Change the inserted throttling tier back to Null and test the convertNullThrottlingTier method
     updateThrottlingTierToNull();
     apiMgtDAO.convertNullThrottlingTiers();
     HashMap<String, String> result2 = apiMgtDAO.getURITemplatesPerAPIAsString(apiIdentifier);
     Assert.assertTrue(result2.containsKey("/abc::GET::Any::Unlimited::abcd defgh fff"));
}
 
Example 3
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 4
Source File: ResponseCacheConfigContextTest.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
@Test
public void testResponseCacheConfigContext() throws Exception {
    API api = new API(new APIIdentifier("admin", "TestAPI", "1.0.0"));
    api.setStatus(APIConstants.CREATED);
    api.setContextTemplate("/");
    api.setResponseCache(APIConstants.DISABLED);
    ConfigContext configcontext = new APIConfigContext(api);
    ResponseCacheConfigContext responseCacheConfigContext = new ResponseCacheConfigContext(configcontext, api);
    Assert.assertFalse((Boolean) responseCacheConfigContext.getContext().get("responseCacheEnabled"));

    api.setResponseCache(APIConstants.ENABLED);
    api.setCacheTimeout(100);
    responseCacheConfigContext = new ResponseCacheConfigContext(configcontext, api);
    Assert.assertTrue((Boolean) responseCacheConfigContext.getContext().get("responseCacheEnabled"));
    Assert.assertEquals(100, responseCacheConfigContext.getContext().get("responseCacheTimeOut"));
}
 
Example 5
Source File: TransportConfigContextTest.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
@Test
public void testTransportConfigContext() throws Exception {

    API api = new API(new APIIdentifier("admin", "TestAPI", "1.0.0"));
    api.setStatus(APIConstants.CREATED);
    api.setContextTemplate("/");
    api.setTransports(Constants.TRANSPORT_HTTP);
    ConfigContext configcontext = new APIConfigContext(api);
    TransportConfigContext transportConfigContext = new TransportConfigContext(configcontext, api);
    transportConfigContext.validate();
    Assert.assertTrue(Constants.TRANSPORT_HTTP.equalsIgnoreCase
            (transportConfigContext.getContext().get("transport").toString()));
    api.setTransports(Constants.TRANSPORT_HTTP + "," + Constants.TRANSPORT_HTTPS);
    configcontext = new APIConfigContext(api);
    transportConfigContext = new TransportConfigContext(configcontext, api);
    Assert.assertTrue(StringUtils.EMPTY.equalsIgnoreCase
            (transportConfigContext.getContext().get("transport").toString()));
}
 
Example 6
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 7
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 8
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 9
Source File: TemplateUtilContextTest.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
@Test
public void testTemplateUtilContext() throws Exception {
    API api = new API(new APIIdentifier("admin", "TestAPI", "1.0.0"));
    api.setStatus(APIConstants.CREATED);
    api.setContextTemplate("/");
    ConfigContext configcontext = new APIConfigContext(api);
    TemplateUtilContext templateUtilContext = new TemplateUtilContext(configcontext);
    String xmlSampleText = "<data>TemplateUtilContextTest Class</data>";
    String xmlEscapedText = "&lt;data&gt;TemplateUtilContextTest Class&lt;/data&gt;";
    String result = templateUtilContext.escapeXml(xmlSampleText);
    Assert.assertTrue("Failed to escape XML tags in the provided string : " + xmlSampleText,
            xmlEscapedText.equalsIgnoreCase(result));
    Assert.assertNotNull(templateUtilContext.getContext().get("util"));
}
 
Example 10
Source File: BAMMediatorConfigContextTest.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetContext() throws Exception {
    API api = new API(new APIIdentifier("admin", "TestAPI", "1.0.0"));
    api.setStatus(APIConstants.CREATED);
    api.setContextTemplate("/");
    PowerMockito.mockStatic(APIUtil.class);
    PowerMockito.when(APIUtil.isAnalyticsEnabled()).thenReturn(false);
    ConfigContext configcontext = new APIConfigContext(api);
    BAMMediatorConfigContext bamMediatorConfigContext = new BAMMediatorConfigContext(configcontext);
    Assert.assertFalse((Boolean) bamMediatorConfigContext.getContext().get("statsEnabled"));
    PowerMockito.when(APIUtil.isAnalyticsEnabled()).thenReturn(true);
    bamMediatorConfigContext = new BAMMediatorConfigContext(configcontext);
    Assert.assertTrue((Boolean) bamMediatorConfigContext.getContext().get("statsEnabled"));
}
 
Example 11
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 12
Source File: APIConsumerImplWrapper.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
public API getAPI(APIIdentifier identifier) throws APIManagementException {
    API api = new API(identifier);
    if("published_api".equals(identifier.getApiName())) {
        api.setStatus(APIConstants.PUBLISHED);
    } else {
        api.setStatus(APIConstants.CREATED);
    }
    return  api;
}
 
Example 13
Source File: SecurityConfigContextTest.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
@Test
public void testSecurityConfigContext() throws Exception {

    API api = new API(new APIIdentifier("admin", "TestAPI", "1.0.0"));
    api.setStatus(APIConstants.CREATED);
    api.setContextTemplate("/");
    api.setTransports(Constants.TRANSPORT_HTTP);
    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 14
Source File: JwtConfigContextTest.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
@Test
public void testJwtConfigContext() throws Exception {
    API api = new API(new APIIdentifier("admin", "TestAPI", "1.0.0"));
    api.setStatus(APIConstants.CREATED);
    api.setContextTemplate("/");
    ConfigContext configcontext = new APIConfigContext(api);
    JwtConfigContext jwtConfigContext = new JwtConfigContext(configcontext);
    Assert.assertNotNull(jwtConfigContext.getContext());
}
 
Example 15
Source File: APIConfigContextTest.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetContext() throws Exception {
    API api = new API(new APIIdentifier("admin", "TestAPI", "1.0.0"));
    api.setStatus(APIConstants.BLOCKED);
    api.setContextTemplate("/");
    APIConfigContext configContext = new APIConfigContext(api);
    boolean isBlocked = (Boolean) configContext.getContext().get("apiIsBlocked");
    Assert.assertTrue(isBlocked);
}
 
Example 16
Source File: HandlerConfigContextTest.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
@Test
public void testHandlerConfigContex() throws Exception {
    API api = new API(new APIIdentifier("admin", "TestAPI", "1.0.0"));
    api.setStatus(APIConstants.CREATED);
    api.setContextTemplate("/");
    ConfigContext configcontext = new APIConfigContext(api);
    List<HandlerConfig> handlers = new ArrayList<HandlerConfig>();
    HandlerConfigContex handlerConfigContex = new HandlerConfigContex(configcontext, handlers);
    Assert.assertNotNull(handlerConfigContex.getContext().get("handlers"));
}
 
Example 17
Source File: APIMgtDAOTest.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
@Test
public void testAddAndGetApi() throws Exception{
    APIIdentifier apiIdentifier = new APIIdentifier("testAddAndGetApi",
            "testAddAndGetApi", "1.0.0");
    API api = new API(apiIdentifier);
    api.setContext("/testAddAndGetApi");
    api.setContextTemplate("/testAddAndGetApi/{version}");
    api.setUriTemplates(getUriTemplateSet());
    api.setScopes(getScopes());
    api.setStatus(APIConstants.PUBLISHED);
    api.setAsDefaultVersion(true);
    int apiID = apiMgtDAO.addAPI(api, -1234);
    apiMgtDAO.addURITemplates(apiID, api, -1234);
    apiMgtDAO.updateAPI(api);
    apiMgtDAO.updateURITemplates(api, -1234);
    Set<APIStore> apiStoreSet = new HashSet<APIStore>();
    APIStore apiStore = new APIStore();
    apiStore.setDisplayName("wso2");
    apiStore.setEndpoint("http://localhost:9433/store");
    apiStore.setName("wso2");
    apiStore.setType("wso2");
    apiStoreSet.add(apiStore);
    apiMgtDAO.addExternalAPIStoresDetails(apiIdentifier,apiStoreSet);
    assertTrue(apiMgtDAO.getExternalAPIStoresDetails(apiIdentifier).size()>0);
    apiMgtDAO.deleteExternalAPIStoresDetails(apiIdentifier, apiStoreSet);
    apiMgtDAO.updateExternalAPIStoresDetails(apiIdentifier, Collections.<APIStore>emptySet());
    assertTrue(apiMgtDAO.getExternalAPIStoresDetails(apiIdentifier).size()==0);
    apiMgtDAO.deleteAPI(apiIdentifier);
}
 
Example 18
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 19
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));
}