Merge pull request #1061 from Nirothipan/appPubliserTests

Adding test cases for APIPublisherUtil Class
revert-70aa11f8
sinthuja 7 years ago committed by GitHub
commit fcf62f085b

@ -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{

@ -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);
}
}

@ -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);
}
}

@ -3,36 +3,8 @@ package org.wso2.carbon.apimgt.webapp.publisher.config;
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);
}
}

@ -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);
}
}

@ -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<ApiScope> scopes = new HashSet<>();
@ -175,24 +174,8 @@ APIPublisherServiceTest extends BaseAPIPublisherTest {
Map<String, ApiScope> apiScopes = new HashMap<>();
Set<ApiScope> scopes = new HashSet<>(apiScopes.values());
apiConfig.setScopes(scopes);
setAPIURITemplates(apiConfig);
}
private void setAPIURITemplates(APIConfig apiConfig) {
Set<ApiUriTemplate> 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 {

@ -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<APIResource> 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<APIResource> 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<APIResource> 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<APIResource> 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<ApiUriTemplate> 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);

@ -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);

@ -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) {

@ -130,7 +130,7 @@ public class MockServletContext implements ServletContext {
@Override
public String getInitParameter(String s) {
return "true";
return "/*";
}
@Override

@ -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<ApiUriTemplate> 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);
}
}

@ -24,7 +24,7 @@
<WebappPublisherConfigs>
<!-- This host is used to define the host address which is used to publish APIs -->
<Host>https://${iot.core.host}:${iot.core.https.port}</Host>
<Host>https://localhost:9445</Host>
<!-- If it is true, the APIs of this instance will be published to the defined host -->
<PublishAPI>true</PublishAPI>

Loading…
Cancel
Save