Merge pull request #1054 from Nirothipan/appPubliserTests

APIPubisherUtil class initial Tests
revert-70aa11f8
sinthuja 7 years ago committed by GitHub
commit 6c79e67746

@ -89,7 +89,7 @@ APIPublisherServiceTest extends BaseAPIPublisherTest {
@Test(description = "createAPIListWithNoApi | will fail if there are any exceptions") @Test(description = "createAPIListWithNoApi | will fail if there are any exceptions")
private void publishWithNoAPIListCreated() throws APIManagerPublisherException { private void publishWithNoAPIListCreated() throws APIManagerPublisherException {
APIConfig apiConfig = new APIConfig(); APIConfig apiConfig = new APIConfig();
setApiConfigs(apiConfig, "testAPI-3"); setApiConfigs(apiConfig, "testAPI-2");
APIPublisherDataHolder apiPublisherDataHolder = Mockito.mock(APIPublisherDataHolder.getInstance(). APIPublisherDataHolder apiPublisherDataHolder = Mockito.mock(APIPublisherDataHolder.getInstance().
getClass(), Mockito.CALLS_REAL_METHODS); getClass(), Mockito.CALLS_REAL_METHODS);
IntegrationClientService integrationClientService = Mockito.mock(IntegrationClientServiceImpl. IntegrationClientService integrationClientService = Mockito.mock(IntegrationClientServiceImpl.
@ -118,7 +118,7 @@ APIPublisherServiceTest extends BaseAPIPublisherTest {
@Test(description = "createAPIList | will fail if there are any exceptions") @Test(description = "createAPIList | will fail if there are any exceptions")
private void publishWithAPIListCreated() throws APIManagerPublisherException { private void publishWithAPIListCreated() throws APIManagerPublisherException {
APIConfig apiConfig = new APIConfig(); APIConfig apiConfig = new APIConfig();
setApiConfigs(apiConfig, "testAPI-2"); setApiConfigs(apiConfig, "testAPI-3");
APIPublisherDataHolder apiPublisherDataHolder = Mockito.mock(APIPublisherDataHolder.getInstance(). APIPublisherDataHolder apiPublisherDataHolder = Mockito.mock(APIPublisherDataHolder.getInstance().
getClass(), Mockito.CALLS_REAL_METHODS); getClass(), Mockito.CALLS_REAL_METHODS);
IntegrationClientService integrationClientService = Mockito.mock(IntegrationClientServiceImpl. IntegrationClientService integrationClientService = Mockito.mock(IntegrationClientServiceImpl.
@ -134,7 +134,7 @@ APIPublisherServiceTest extends BaseAPIPublisherTest {
doReturn(api).when(apIsApi).apisPost(Mockito.any(), Mockito.anyString()); doReturn(api).when(apIsApi).apisPost(Mockito.any(), Mockito.anyString());
APIList apiList = Mockito.mock(APIList.class, Mockito.CALLS_REAL_METHODS); APIList apiList = Mockito.mock(APIList.class, Mockito.CALLS_REAL_METHODS);
APIInfo apiInfo = new APIInfo(); APIInfo apiInfo = new APIInfo();
apiInfo.setName("testAPI-2"); apiInfo.setName("testAPI-3");
apiInfo.setVersion("1.0.0"); apiInfo.setVersion("1.0.0");
apiInfo.setId("test-one"); apiInfo.setId("test-one");
List<APIInfo> apiInfoList = new ArrayList<>(); List<APIInfo> apiInfoList = new ArrayList<>();
@ -148,6 +148,18 @@ APIPublisherServiceTest extends BaseAPIPublisherTest {
apiPublisherService.publishAPI(apiConfig); apiPublisherService.publishAPI(apiConfig);
} }
@Test(description = "publish API with scope added | will fail if there are any exceptions")
private void publishWithAPIScope() throws APIManagerPublisherException {
APIConfig apiConfig = new APIConfig();
setApiConfigs(apiConfig, "testAPI-4");
Set<ApiScope> scopes = new HashSet<>();
ApiScope apiScope = new ApiScope();
apiScope.setDescription("testing");
scopes.add(apiScope);
apiConfig.setScopes(scopes);
apiPublisherService.publishAPI(apiConfig);
}
private void setApiConfigs(APIConfig apiConfig, String name) { private void setApiConfigs(APIConfig apiConfig, String name) {
apiConfig.setName(name); apiConfig.setName(name);
apiConfig.setContext("api/device-mgt/windows/v1.g0/admin/devices"); apiConfig.setContext("api/device-mgt/windows/v1.g0/admin/devices");

@ -0,0 +1,126 @@
/*
* 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;
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.exception.APIManagerPublisherException;
import org.wso2.carbon.apimgt.webapp.publisher.utils.MockServletContext;
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 static org.wso2.carbon.apimgt.webapp.publisher.APIPublisherUtil.buildApiConfig;
/**
* This is the test class for {@link APIPublisherUtil}
*/
public class APIPublisherUtilTest extends BaseAPIPublisherTest {
@BeforeTest
public void initialConfigs() throws WebappPublisherConfigurationFailedException,
org.wso2.carbon.user.core.UserStoreException, RegistryException {
WebappPublisherConfig.init();
setUserRealm();
}
@Test(description = "test buildAPIConfig method and ensures an APIConfig is created")
private void buildApiConfigTest() throws UserStoreException, RegistryException {
try {
startTenantFlowAsTestTenant();
ServletContext servletContext = new MockServletContext();
APIResourceConfiguration apiDef = new APIResourceConfiguration();
List<APIResource> resources = new ArrayList<>();
apiDef.setResources(resources);
APIConfig apiConfig = buildApiConfig(servletContext, apiDef);
Assert.assertNotNull(apiConfig, "API configuration is null.");
} finally {
PrivilegedCarbonContext.endTenantFlow();
}
}
@Test(description = "test buildAPIConfig method as SuperTenant and ensures" +
" an APIConfig is created")
private void buildApiConfigAsSuperTenat() throws UserStoreException {
ServletContext servletContext = new MockServletContext();
APIResourceConfiguration apiDef = new APIResourceConfiguration();
List<APIResource> resources = new ArrayList<>();
apiDef.setResources(resources);
APIConfig apiConfig = buildApiConfig(servletContext, apiDef);
Assert.assertNotNull(apiConfig, "API configuration is null.");
}
@Test(description = "test buildAPIConfig with API tags specified and ensures " +
"an APIConfig is created")
private void buildApiConfigTestWithTags() throws UserStoreException {
ServletContext servletContext = new MockServletContext();
APIResourceConfiguration apiDef = new APIResourceConfiguration();
List<APIResource> resources = new ArrayList<>();
APIResource apiResource = new APIResource();
resources.add(apiResource);
apiDef.setResources(resources);
apiDef.setTags(new String[]{"windows", "device_management"});
APIConfig apiConfig = buildApiConfig(servletContext, apiDef);
Assert.assertNotNull(apiConfig, "API configuration is null.");
}
@Test(description = "test buildAPIConfig method with API scopes specified and " +
"ensures an APIConfig is created")
private void buildApiConfigTestWithScope() throws UserStoreException, APIManagerPublisherException {
ServletContext servletContext = new MockServletContext();
APIResourceConfiguration apiDef = new APIResourceConfiguration();
List<APIResource> resources = new ArrayList<>();
APIResource apiResource = new APIResource();
ApiScope apiScope = new ApiScope();
apiScope.setDescription("testing");
apiResource.setScope(apiScope);
resources.add(apiResource);
apiDef.setResources(resources);
apiDef.setTags(new String[]{"windows", "device_management"});
APIConfig apiConfig = buildApiConfig(servletContext, apiDef);
Assert.assertNotNull(apiConfig, "API configuration is null.");
}
private void setUserRealm() throws RegistryException, org.wso2.carbon.user.core.UserStoreException {
RealmConfiguration configuration = new RealmConfiguration();
UserRealm userRealm = new InMemoryRealmService().getUserRealm(configuration);
PrivilegedCarbonContext.getThreadLocalCarbonContext().setUserRealm(userRealm);
}
private void startTenantFlowAsTestTenant() throws org.wso2.carbon.user.core.UserStoreException, RegistryException {
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(1212);
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain("test.com");
setUserRealm();
}
}

@ -0,0 +1,285 @@
/*
* 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 javax.servlet.*;
import javax.servlet.descriptor.JspConfigDescriptor;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Enumeration;
import java.util.EventListener;
import java.util.Map;
import java.util.Set;
public class MockServletContext implements ServletContext {
@Override
public ServletContext getContext(String s) {
return null;
}
@Override
public String getContextPath() {
return null;
}
@Override
public int getMajorVersion() {
return 0;
}
@Override
public int getMinorVersion() {
return 0;
}
@Override
public int getEffectiveMajorVersion() {
return 0;
}
@Override
public int getEffectiveMinorVersion() {
return 0;
}
@Override
public String getMimeType(String s) {
return null;
}
@Override
public Set<String> getResourcePaths(String s) {
return null;
}
@Override
public URL getResource(String s) throws MalformedURLException {
return null;
}
@Override
public InputStream getResourceAsStream(String s) {
return null;
}
@Override
public RequestDispatcher getRequestDispatcher(String s) {
return null;
}
@Override
public RequestDispatcher getNamedDispatcher(String s) {
return null;
}
@Override
public Servlet getServlet(String s) throws ServletException {
return null;
}
@Override
public Enumeration<Servlet> getServlets() {
return null;
}
@Override
public Enumeration<String> getServletNames() {
return null;
}
@Override
public void log(String s) {
}
@Override
public void log(Exception e, String s) {
}
@Override
public void log(String s, Throwable throwable) {
}
@Override
public String getRealPath(String s) {
return null;
}
@Override
public String getServerInfo() {
return null;
}
@Override
public String getInitParameter(String s) {
return "true";
}
@Override
public Enumeration<String> getInitParameterNames() {
return null;
}
@Override
public boolean setInitParameter(String s, String s1) {
return false;
}
@Override
public Object getAttribute(String s) {
return null;
}
@Override
public Enumeration<String> getAttributeNames() {
return null;
}
@Override
public void setAttribute(String s, Object o) {
}
@Override
public void removeAttribute(String s) {
}
@Override
public String getServletContextName() {
return null;
}
@Override
public ServletRegistration.Dynamic addServlet(String s, String s1) {
return null;
}
@Override
public ServletRegistration.Dynamic addServlet(String s, Servlet servlet) {
return null;
}
@Override
public ServletRegistration.Dynamic addServlet(String s, Class<? extends Servlet> aClass) {
return null;
}
@Override
public <T extends Servlet> T createServlet(Class<T> aClass) throws ServletException {
return null;
}
@Override
public ServletRegistration getServletRegistration(String s) {
return null;
}
@Override
public Map<String, ? extends ServletRegistration> getServletRegistrations() {
return null;
}
@Override
public FilterRegistration.Dynamic addFilter(String s, String s1) {
return null;
}
@Override
public FilterRegistration.Dynamic addFilter(String s, Filter filter) {
return null;
}
@Override
public FilterRegistration.Dynamic addFilter(String s, Class<? extends Filter> aClass) {
return null;
}
@Override
public <T extends Filter> T createFilter(Class<T> aClass) throws ServletException {
return null;
}
@Override
public FilterRegistration getFilterRegistration(String s) {
return null;
}
@Override
public Map<String, ? extends FilterRegistration> getFilterRegistrations() {
return null;
}
@Override
public SessionCookieConfig getSessionCookieConfig() {
return null;
}
@Override
public void setSessionTrackingModes(Set<SessionTrackingMode> set) throws IllegalStateException, IllegalArgumentException {
}
@Override
public Set<SessionTrackingMode> getDefaultSessionTrackingModes() {
return null;
}
@Override
public Set<SessionTrackingMode> getEffectiveSessionTrackingModes() {
return null;
}
@Override
public void addListener(String s) {
}
@Override
public <T extends EventListener> void addListener(T t) {
}
@Override
public void addListener(Class<? extends EventListener> aClass) {
}
@Override
public <T extends EventListener> T createListener(Class<T> aClass) throws ServletException {
return null;
}
@Override
public void declareRoles(String... strings) {
}
@Override
public ClassLoader getClassLoader() {
return null;
}
@Override
public JspConfigDescriptor getJspConfigDescriptor() {
return null;
}
}
Loading…
Cancel
Save