From 7e06541c80f6f25d2f0ff9db9ccc938252919b70 Mon Sep 17 00:00:00 2001 From: Nirothipan Date: Fri, 13 Oct 2017 13:03:37 +0530 Subject: [PATCH] code clean up and few more test cases for APIPUblisherUtil Class --- .../webapp/publisher/APIPublisherUtil.java | 2 +- .../InvalidConfigurationStateException.java | 46 ----------------- ...PublisherConfigurationFailedException.java | 16 ------ .../APIResourceManagementException.java | 38 ++------------ .../APIManagerPublisherException.java | 17 ------- .../publisher/APIPublisherServiceTest.java | 39 +++++---------- .../publisher/APIPublisherUtilTest.java | 49 ++++++++++++++++--- .../publisher/BaseAPIPublisherTest.java | 4 +- .../utils/{Api.java => MockApi.java} | 4 +- .../publisher/utils/MockServletContext.java | 2 +- .../webapp/publisher/utils/TestUtils.java | 48 ++++++++++++++++++ .../conf/etc/webapp-publisher-config.xml | 2 +- 12 files changed, 114 insertions(+), 153 deletions(-) rename components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/test/java/org/wso2/carbon/apimgt/webapp/publisher/utils/{Api.java => MockApi.java} (96%) create mode 100644 components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/test/java/org/wso2/carbon/apimgt/webapp/publisher/utils/TestUtils.java diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/APIPublisherUtil.java b/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/APIPublisherUtil.java index a3f8608de4..e1f2195282 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/APIPublisherUtil.java +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/APIPublisherUtil.java @@ -276,7 +276,7 @@ public class APIPublisherUtil { if(null != resourcesList) { for (ApiUriTemplate template : templates) { String fullPaath = ""; - if( template.getUriTemplate() != AnnotationProcessor.WILD_CARD ) { + if (!template.getUriTemplate().equals(AnnotationProcessor.WILD_CARD)) { fullPaath = apiConfig.getContext() + template.getUriTemplate(); } else{ diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/InvalidConfigurationStateException.java b/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/InvalidConfigurationStateException.java index d3022f11d1..7604fe88a6 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/InvalidConfigurationStateException.java +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/InvalidConfigurationStateException.java @@ -23,53 +23,7 @@ public class InvalidConfigurationStateException extends RuntimeException { private static final long serialVersionUID = -3151279311329070397L; - private String errorMessage; - private int errorCode; - - public InvalidConfigurationStateException(int errorCode, String message) { - super(message); - this.errorCode = errorCode; - } - - public InvalidConfigurationStateException(int errorCode, String message, Throwable cause) { - super(message, cause); - this.errorCode = errorCode; - } - - public int getErrorCode() { - return errorCode; - } - - - public String getErrorMessage() { - return errorMessage; - } - - public void setErrorMessage(String errorMessage) { - this.errorMessage = errorMessage; - } - - public InvalidConfigurationStateException(String msg, Exception nestedEx) { - super(msg, nestedEx); - setErrorMessage(msg); - } - - public InvalidConfigurationStateException(String message, Throwable cause) { - super(message, cause); - setErrorMessage(message); - } - public InvalidConfigurationStateException(String msg) { super(msg); - setErrorMessage(msg); } - - public InvalidConfigurationStateException() { - super(); - } - - public InvalidConfigurationStateException(Throwable cause) { - super(cause); - } - } diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/WebappPublisherConfigurationFailedException.java b/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/WebappPublisherConfigurationFailedException.java index e5b3252f8e..cc73f0baa3 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/WebappPublisherConfigurationFailedException.java +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/WebappPublisherConfigurationFailedException.java @@ -25,20 +25,4 @@ public class WebappPublisherConfigurationFailedException extends Exception { public WebappPublisherConfigurationFailedException(String msg, Exception nestedEx) { super(msg, nestedEx); } - - public WebappPublisherConfigurationFailedException(String message, Throwable cause) { - super(message, cause); - } - - public WebappPublisherConfigurationFailedException(String msg) { - super(msg); - } - - public WebappPublisherConfigurationFailedException() { - super(); - } - - public WebappPublisherConfigurationFailedException(Throwable cause) { - super(cause); - } } diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/config/APIResourceManagementException.java b/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/config/APIResourceManagementException.java index 1007c50026..b8fe08cfcb 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/config/APIResourceManagementException.java +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/config/APIResourceManagementException.java @@ -1,38 +1,10 @@ package org.wso2.carbon.apimgt.webapp.publisher.config; -public class APIResourceManagementException extends Exception{ - private static final long serialVersionUID = -3151279311929070297L; +public class APIResourceManagementException extends Exception { + private static final long serialVersionUID = -3151279311929070297L; - private String errorMessage; - public String getErrorMessage() { - return errorMessage; - } - - public void setErrorMessage(String errorMessage) { - this.errorMessage = errorMessage; - } - - public APIResourceManagementException(String msg, Exception nestedEx) { - super(msg, nestedEx); - setErrorMessage(msg); - } - - public APIResourceManagementException(String message, Throwable cause) { - super(message, cause); - setErrorMessage(message); - } - - public APIResourceManagementException(String msg) { - super(msg); - setErrorMessage(msg); - } - - public APIResourceManagementException() { - super(); - } - - public APIResourceManagementException(Throwable cause) { - super(cause); - } + public APIResourceManagementException(String msg, Exception nestedEx) { + super(msg, nestedEx); + } } diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/exception/APIManagerPublisherException.java b/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/exception/APIManagerPublisherException.java index b98aef3c9a..aaa24b1fa7 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/exception/APIManagerPublisherException.java +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/exception/APIManagerPublisherException.java @@ -25,24 +25,7 @@ public class APIManagerPublisherException extends Exception { private static final long serialVersionUID = -8933142342423122660L; - public APIManagerPublisherException(String msg, Exception nestedEx) { - super(msg, nestedEx); - } - - public APIManagerPublisherException(String message, Throwable cause) { - super(message, cause); - } - - public APIManagerPublisherException(String msg) { - super(msg); - } - - public APIManagerPublisherException() { - super(); - } - public APIManagerPublisherException(Throwable cause) { super(cause); } - } diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/test/java/org/wso2/carbon/apimgt/webapp/publisher/APIPublisherServiceTest.java b/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/test/java/org/wso2/carbon/apimgt/webapp/publisher/APIPublisherServiceTest.java index 2a5b533eaf..87d27c5e52 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/test/java/org/wso2/carbon/apimgt/webapp/publisher/APIPublisherServiceTest.java +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/test/java/org/wso2/carbon/apimgt/webapp/publisher/APIPublisherServiceTest.java @@ -33,10 +33,10 @@ import org.wso2.carbon.apimgt.integration.generated.client.publisher.model.APIIn import org.wso2.carbon.apimgt.integration.generated.client.publisher.model.APIList; import org.wso2.carbon.apimgt.webapp.publisher.config.WebappPublisherConfig; import org.wso2.carbon.apimgt.webapp.publisher.dto.ApiScope; -import org.wso2.carbon.apimgt.webapp.publisher.dto.ApiUriTemplate; import org.wso2.carbon.apimgt.webapp.publisher.exception.APIManagerPublisherException; import org.wso2.carbon.apimgt.webapp.publisher.internal.APIPublisherDataHolder; -import org.wso2.carbon.apimgt.webapp.publisher.utils.Api; +import org.wso2.carbon.apimgt.webapp.publisher.utils.MockApi; +import org.wso2.carbon.apimgt.webapp.publisher.utils.TestUtils; import java.lang.reflect.Field; import java.util.*; @@ -46,8 +46,7 @@ import static org.mockito.Mockito.doReturn; /** * This is the test class for {@link APIPublisherServiceImpl} */ -public class -APIPublisherServiceTest extends BaseAPIPublisherTest { +public class APIPublisherServiceTest extends BaseAPIPublisherTest { private static final Log log = LogFactory.getLog(APIPublisherServiceTest.class); private APIPublisherServiceImpl apiPublisherService = new APIPublisherServiceImpl(); @@ -78,7 +77,7 @@ APIPublisherServiceTest extends BaseAPIPublisherTest { PublisherClient publisherClient = APIPublisherDataHolder.getInstance().getIntegrationClientService(). getPublisherClient(); doReturn(publisherClient).when(integrationClientService).getPublisherClient(); - APIsApi apIsApi = Mockito.mock(Api.class, Mockito.CALLS_REAL_METHODS); + APIsApi apIsApi = Mockito.mock(MockApi.class, Mockito.CALLS_REAL_METHODS); doReturn(apIsApi).when(publisherClient).getApi(); API api = Mockito.mock(API.class, Mockito.CALLS_REAL_METHODS); api.setStatus("CREATED"); @@ -87,7 +86,7 @@ APIPublisherServiceTest extends BaseAPIPublisherTest { } @Test(description = "createAPIListWithNoApi | will fail if there are any exceptions") - private void publishWithNoAPIListCreated() throws APIManagerPublisherException { + public void publishWithNoAPIListCreated() throws APIManagerPublisherException { APIConfig apiConfig = new APIConfig(); setApiConfigs(apiConfig, "testAPI-2"); APIPublisherDataHolder apiPublisherDataHolder = Mockito.mock(APIPublisherDataHolder.getInstance(). @@ -98,7 +97,7 @@ APIPublisherServiceTest extends BaseAPIPublisherTest { PublisherClient publisherClient = APIPublisherDataHolder.getInstance().getIntegrationClientService(). getPublisherClient(); doReturn(publisherClient).when(integrationClientService).getPublisherClient(); - APIsApi apIsApi = Mockito.mock(Api.class, Mockito.CALLS_REAL_METHODS); + APIsApi apIsApi = Mockito.mock(MockApi.class, Mockito.CALLS_REAL_METHODS); doReturn(apIsApi).when(publisherClient).getApi(); API api = Mockito.mock(API.class, Mockito.CALLS_REAL_METHODS); api.setStatus("CREATED"); @@ -116,7 +115,7 @@ APIPublisherServiceTest extends BaseAPIPublisherTest { } @Test(description = "createAPIList | will fail if there are any exceptions") - private void publishWithAPIListCreated() throws APIManagerPublisherException { + public void publishWithAPIListCreated() throws APIManagerPublisherException { APIConfig apiConfig = new APIConfig(); setApiConfigs(apiConfig, "testAPI-3"); APIPublisherDataHolder apiPublisherDataHolder = Mockito.mock(APIPublisherDataHolder.getInstance(). @@ -127,7 +126,7 @@ APIPublisherServiceTest extends BaseAPIPublisherTest { PublisherClient publisherClient = APIPublisherDataHolder.getInstance().getIntegrationClientService(). getPublisherClient(); doReturn(publisherClient).when(integrationClientService).getPublisherClient(); - APIsApi apIsApi = Mockito.mock(Api.class, Mockito.CALLS_REAL_METHODS); + APIsApi apIsApi = Mockito.mock(MockApi.class, Mockito.CALLS_REAL_METHODS); doReturn(apIsApi).when(publisherClient).getApi(); API api = Mockito.mock(API.class, Mockito.CALLS_REAL_METHODS); api.setStatus("CREATED"); @@ -149,7 +148,7 @@ APIPublisherServiceTest extends BaseAPIPublisherTest { } @Test(description = "publish API with scope added | will fail if there are any exceptions") - private void publishWithAPIScope() throws APIManagerPublisherException { + public void publishWithAPIScope() throws APIManagerPublisherException { APIConfig apiConfig = new APIConfig(); setApiConfigs(apiConfig, "testAPI-4"); Set scopes = new HashSet<>(); @@ -175,24 +174,8 @@ APIPublisherServiceTest extends BaseAPIPublisherTest { Map apiScopes = new HashMap<>(); Set scopes = new HashSet<>(apiScopes.values()); apiConfig.setScopes(scopes); - setAPIURITemplates(apiConfig); - } - - private void setAPIURITemplates(APIConfig apiConfig) { - Set uriTemplates = new LinkedHashSet<>(); - ApiUriTemplate template = new ApiUriTemplate(); - template.setAuthType("Application & Application User"); - template.setHttpVerb("POST"); - template.setResourceURI("https://localhost:9443/api/device-mgt/windows/v1.0/admin/devices/reboot"); - template.setUriTemplate("/reboot"); - ApiScope scope = new ApiScope(); - scope.setKey("perm:windows:reboot"); - scope.setName("Reboot"); - scope.setRoles("/permission/admin/device-mgt/devices/owning-device/operations/windows/reboot"); - scope.setDescription("Lock reset on Windows devices"); - template.setScope(scope); - uriTemplates.add(template); - apiConfig.setUriTemplates(uriTemplates); + TestUtils util = new TestUtils(); + util.setAPIURITemplates(apiConfig, "/reboot"); } private void initializeOAuthApplication() throws NoSuchFieldException, IllegalAccessException { diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/test/java/org/wso2/carbon/apimgt/webapp/publisher/APIPublisherUtilTest.java b/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/test/java/org/wso2/carbon/apimgt/webapp/publisher/APIPublisherUtilTest.java index ca1bc1fba4..45b8d71257 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/test/java/org/wso2/carbon/apimgt/webapp/publisher/APIPublisherUtilTest.java +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/test/java/org/wso2/carbon/apimgt/webapp/publisher/APIPublisherUtilTest.java @@ -18,35 +18,40 @@ package org.wso2.carbon.apimgt.webapp.publisher; import org.testng.Assert; -import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; import org.wso2.carbon.apimgt.webapp.publisher.config.APIResource; import org.wso2.carbon.apimgt.webapp.publisher.config.APIResourceConfiguration; import org.wso2.carbon.apimgt.webapp.publisher.config.WebappPublisherConfig; import org.wso2.carbon.apimgt.webapp.publisher.dto.ApiScope; +import org.wso2.carbon.apimgt.webapp.publisher.dto.ApiUriTemplate; import org.wso2.carbon.apimgt.webapp.publisher.exception.APIManagerPublisherException; import org.wso2.carbon.apimgt.webapp.publisher.utils.MockServletContext; +import org.wso2.carbon.apimgt.webapp.publisher.utils.TestUtils; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.registry.core.exceptions.RegistryException; import org.wso2.carbon.registry.core.jdbc.realm.InMemoryRealmService; import org.wso2.carbon.user.api.RealmConfiguration; import org.wso2.carbon.user.api.UserRealm; import org.wso2.carbon.user.api.UserStoreException; -import org.wso2.carbon.utils.multitenancy.MultitenantConstants; import javax.servlet.ServletContext; import java.util.ArrayList; import java.util.List; +import java.util.Set; import static org.wso2.carbon.apimgt.webapp.publisher.APIPublisherUtil.buildApiConfig; +import static org.wso2.carbon.apimgt.webapp.publisher.APIPublisherUtil.getApiEndpointUrl; +import static org.wso2.carbon.apimgt.webapp.publisher.APIPublisherUtil.setResourceAuthTypes; /** * This is the test class for {@link APIPublisherUtil} */ public class APIPublisherUtilTest extends BaseAPIPublisherTest { + private static final String AUTH_TYPE_NON_SECURED = "None"; + @BeforeTest public void initialConfigs() throws WebappPublisherConfigurationFailedException, org.wso2.carbon.user.core.UserStoreException, RegistryException { @@ -55,7 +60,7 @@ public class APIPublisherUtilTest extends BaseAPIPublisherTest { } @Test(description = "test buildAPIConfig method and ensures an APIConfig is created") - private void buildApiConfigTest() throws UserStoreException, RegistryException { + public void buildApiConfigTest() throws UserStoreException, RegistryException { try { startTenantFlowAsTestTenant(); ServletContext servletContext = new MockServletContext(); @@ -71,7 +76,7 @@ public class APIPublisherUtilTest extends BaseAPIPublisherTest { @Test(description = "test buildAPIConfig method as SuperTenant and ensures" + " an APIConfig is created") - private void buildApiConfigAsSuperTenat() throws UserStoreException { + public void buildApiConfigAsSuperTenant() throws UserStoreException { ServletContext servletContext = new MockServletContext(); APIResourceConfiguration apiDef = new APIResourceConfiguration(); List resources = new ArrayList<>(); @@ -82,7 +87,7 @@ public class APIPublisherUtilTest extends BaseAPIPublisherTest { @Test(description = "test buildAPIConfig with API tags specified and ensures " + "an APIConfig is created") - private void buildApiConfigTestWithTags() throws UserStoreException { + public void buildApiConfigTestWithTags() throws UserStoreException { ServletContext servletContext = new MockServletContext(); APIResourceConfiguration apiDef = new APIResourceConfiguration(); List resources = new ArrayList<>(); @@ -96,7 +101,7 @@ public class APIPublisherUtilTest extends BaseAPIPublisherTest { @Test(description = "test buildAPIConfig method with API scopes specified and " + "ensures an APIConfig is created") - private void buildApiConfigTestWithScope() throws UserStoreException, APIManagerPublisherException { + public void buildApiConfigTestWithScope() throws UserStoreException, APIManagerPublisherException { ServletContext servletContext = new MockServletContext(); APIResourceConfiguration apiDef = new APIResourceConfiguration(); List resources = new ArrayList<>(); @@ -111,6 +116,38 @@ public class APIPublisherUtilTest extends BaseAPIPublisherTest { Assert.assertNotNull(apiConfig, "API configuration is null."); } + @Test(description = "test method for setResourceAuthTypes") + public void testSetResourceAuthTypes() throws UserStoreException { + ServletContext servletContext = new MockServletContext(); + APIResourceConfiguration apiDef = new APIResourceConfiguration(); + List resources = new ArrayList<>(); + apiDef.setResources(resources); + APIConfig apiConfig = buildApiConfig(servletContext, apiDef); + apiConfig.setContext("/*"); + TestUtils util = new TestUtils(); + util.setAPIURITemplates(apiConfig, "/*"); + Assert.assertNotNull(apiConfig, "API configuration is null."); + setResourceAuthTypes(servletContext, apiConfig); + Set templates = apiConfig.getUriTemplates(); + Assert.assertEquals(templates.iterator().next().getAuthType(), AUTH_TYPE_NON_SECURED, "Resource " + + "auth type is not properly set"); + } + + @Test(description = "test the method getApiEndpointUrl") + public void testGetApiEndpointUrl() { + String context = "/reboot"; + String url = getApiEndpointUrl(context); + Assert.assertEquals(url, "https://localhost:9445/reboot", "Expected url " + + "is not same as actual url"); + } + + @Test(expectedExceptions = WebappPublisherConfigurationFailedException.class, description = + "this tests the method convertToDocument with a undefined file name and ensures an " + + "exception occurs ") + public void testConvertToDocumentForException() throws WebappPublisherConfigurationFailedException { + WebappPublisherUtil.convertToDocument(null); + } + private void setUserRealm() throws RegistryException, org.wso2.carbon.user.core.UserStoreException { RealmConfiguration configuration = new RealmConfiguration(); UserRealm userRealm = new InMemoryRealmService().getUserRealm(configuration); diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/test/java/org/wso2/carbon/apimgt/webapp/publisher/BaseAPIPublisherTest.java b/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/test/java/org/wso2/carbon/apimgt/webapp/publisher/BaseAPIPublisherTest.java index e935b98e2a..229d11a970 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/test/java/org/wso2/carbon/apimgt/webapp/publisher/BaseAPIPublisherTest.java +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/test/java/org/wso2/carbon/apimgt/webapp/publisher/BaseAPIPublisherTest.java @@ -28,7 +28,7 @@ import org.wso2.carbon.apimgt.integration.client.publisher.PublisherClient; import org.wso2.carbon.apimgt.integration.client.service.IntegrationClientService; import org.wso2.carbon.apimgt.integration.generated.client.publisher.api.APIsApi; import org.wso2.carbon.apimgt.webapp.publisher.internal.APIPublisherDataHolder; -import org.wso2.carbon.apimgt.webapp.publisher.utils.Api; +import org.wso2.carbon.apimgt.webapp.publisher.utils.MockApi; import org.wso2.carbon.base.MultitenantConstants; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.context.internal.OSGiDataHolder; @@ -87,7 +87,7 @@ public abstract class BaseAPIPublisherTest { PublisherClient publisherClient = Mockito.mock(PublisherClient.class, Mockito.CALLS_REAL_METHODS); doReturn(publisherClient).when(integrationClientService).getPublisherClient(); - APIsApi api = new Api(); + APIsApi api = new MockApi(); Field field = PublisherClient.class.getDeclaredField("api"); field.setAccessible(true); field.set(publisherClient, api); diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/test/java/org/wso2/carbon/apimgt/webapp/publisher/utils/Api.java b/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/test/java/org/wso2/carbon/apimgt/webapp/publisher/utils/MockApi.java similarity index 96% rename from components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/test/java/org/wso2/carbon/apimgt/webapp/publisher/utils/Api.java rename to components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/test/java/org/wso2/carbon/apimgt/webapp/publisher/utils/MockApi.java index 820da060a9..f9e22de3e8 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/test/java/org/wso2/carbon/apimgt/webapp/publisher/utils/Api.java +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/test/java/org/wso2/carbon/apimgt/webapp/publisher/utils/MockApi.java @@ -25,9 +25,9 @@ import org.wso2.carbon.apimgt.integration.generated.client.publisher.model.FileI import java.io.File; /** - * Class to create Api for testing. + * Class to create MockApi for testing. */ -public class Api implements APIsApi { +public class MockApi implements APIsApi { @Override public void apisApiIdDelete(String apiId, String ifMatch, String ifUnmodifiedSince) { diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/test/java/org/wso2/carbon/apimgt/webapp/publisher/utils/MockServletContext.java b/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/test/java/org/wso2/carbon/apimgt/webapp/publisher/utils/MockServletContext.java index d88dac206a..c85d274a41 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/test/java/org/wso2/carbon/apimgt/webapp/publisher/utils/MockServletContext.java +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/test/java/org/wso2/carbon/apimgt/webapp/publisher/utils/MockServletContext.java @@ -130,7 +130,7 @@ public class MockServletContext implements ServletContext { @Override public String getInitParameter(String s) { - return "true"; + return "/*"; } @Override diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/test/java/org/wso2/carbon/apimgt/webapp/publisher/utils/TestUtils.java b/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/test/java/org/wso2/carbon/apimgt/webapp/publisher/utils/TestUtils.java new file mode 100644 index 0000000000..0f925940a8 --- /dev/null +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/test/java/org/wso2/carbon/apimgt/webapp/publisher/utils/TestUtils.java @@ -0,0 +1,48 @@ +/* +* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +* +* WSO2 Inc. licenses this file to you under the Apache License, +* Version 2.0 (the "License"); you may not use this file except +* in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, +* software distributed under the License is distributed on an +* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +* KIND, either express or implied. See the License for the +* specific language governing permissions and limitations +* under the License. +*/ +package org.wso2.carbon.apimgt.webapp.publisher.utils; + +import org.wso2.carbon.apimgt.webapp.publisher.APIConfig; +import org.wso2.carbon.apimgt.webapp.publisher.dto.ApiScope; +import org.wso2.carbon.apimgt.webapp.publisher.dto.ApiUriTemplate; + +import java.util.LinkedHashSet; +import java.util.Set; + +/** + * Contains util methods for webAppPublisher tests. + */ +public class TestUtils { + + public void setAPIURITemplates(APIConfig apiConfig, String uriTemplate) { + Set uriTemplates = new LinkedHashSet<>(); + ApiUriTemplate template = new ApiUriTemplate(); + template.setAuthType("Application & Application User"); + template.setHttpVerb("POST"); + template.setResourceURI("https://localhost:9443/api/device-mgt/windows/v1.0/admin/devices/reboot"); + template.setUriTemplate(uriTemplate); + ApiScope scope = new ApiScope(); + scope.setKey("perm:windows:reboot"); + scope.setName("Reboot"); + scope.setRoles("/permission/admin/device-mgt/devices/owning-device/operations/windows/reboot"); + scope.setDescription("Lock reset on Windows devices"); + template.setScope(scope); + uriTemplates.add(template); + apiConfig.setUriTemplates(uriTemplates); + } +} diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/test/resources/carbon-home/repository/conf/etc/webapp-publisher-config.xml b/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/test/resources/carbon-home/repository/conf/etc/webapp-publisher-config.xml index 6e03eb73bd..9e0fe964b8 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/test/resources/carbon-home/repository/conf/etc/webapp-publisher-config.xml +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/test/resources/carbon-home/repository/conf/etc/webapp-publisher-config.xml @@ -24,7 +24,7 @@ - https://${iot.core.host}:${iot.core.https.port} + https://localhost:9445 true