Merge pull request #1050 from Nirothipan/appPubliserTests

Test cases for APIPublisherServiceImpl class
revert-70aa11f8
sinthuja 7 years ago committed by GitHub
commit 658b5f36ae

@ -62,7 +62,7 @@ public class APIPublisherServiceImpl implements APIPublisherService {
for (APIInfo apiInfo : apiList.getList()) { for (APIInfo apiInfo : apiList.getList()) {
if (api.getName().equals(apiInfo.getName()) && api.getVersion().equals(apiInfo.getVersion())) { if (api.getName().equals(apiInfo.getName()) && api.getVersion().equals(apiInfo.getVersion())) {
api = publisherClient.getApi().apisApiIdPut(apiInfo.getId(), api, CONTENT_TYPE, null, null); api = publisherClient.getApi().apisApiIdPut(apiInfo.getId(), api, CONTENT_TYPE, null, null);
if (CREATED_STATUS.equals(api.getStatus())) { if (api != null && CREATED_STATUS.equals(api.getStatus())) {
publisherClient.getApi().apisChangeLifecyclePost(PUBLISH_ACTION, api.getId(), null, null, publisherClient.getApi().apisChangeLifecyclePost(PUBLISH_ACTION, api.getId(), null, null,
null); null);
} }
@ -83,7 +83,9 @@ public class APIPublisherServiceImpl implements APIPublisherService {
return false; return false;
} }
for (APIInfo existingApi : apiList.getList()) { for (APIInfo existingApi : apiList.getList()) {
if (existingApi.getName().equals(api.getName()) && existingApi.getVersion().equals(api.getVersion())) { if (existingApi.getName() != null && existingApi.getName().equals(api.
getName()) && existingApi.getVersion() != null &&
existingApi.getVersion().equals(api.getVersion())) {
return true; return true;
} }
} }

@ -19,17 +19,30 @@ package org.wso2.carbon.apimgt.webapp.publisher;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.mockito.Mockito;
import org.testng.annotations.BeforeTest; import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import org.wso2.carbon.apimgt.integration.client.IntegrationClientServiceImpl;
import org.wso2.carbon.apimgt.integration.client.OAuthRequestInterceptor; import org.wso2.carbon.apimgt.integration.client.OAuthRequestInterceptor;
import org.wso2.carbon.apimgt.integration.client.model.OAuthApplication; import org.wso2.carbon.apimgt.integration.client.model.OAuthApplication;
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.integration.generated.client.publisher.model.API;
import org.wso2.carbon.apimgt.integration.generated.client.publisher.model.APIInfo;
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.ApiScope;
import org.wso2.carbon.apimgt.webapp.publisher.dto.ApiUriTemplate; 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.exception.APIManagerPublisherException;
import org.wso2.carbon.apimgt.webapp.publisher.internal.APIPublisherDataHolder;
import org.wso2.carbon.apimgt.webapp.publisher.utils.Api;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.*; import java.util.*;
import static org.mockito.Mockito.doReturn;
/** /**
* This is the test class for {@link APIPublisherServiceImpl} * This is the test class for {@link APIPublisherServiceImpl}
*/ */
@ -37,23 +50,107 @@ public class
APIPublisherServiceTest extends BaseAPIPublisherTest { APIPublisherServiceTest extends BaseAPIPublisherTest {
private static final Log log = LogFactory.getLog(APIPublisherServiceTest.class); private static final Log log = LogFactory.getLog(APIPublisherServiceTest.class);
private APIPublisherServiceImpl apiPublisherService = new APIPublisherServiceImpl(); private APIPublisherServiceImpl apiPublisherService = new APIPublisherServiceImpl();
private APIConfig apiConfig = new APIConfig();
@BeforeTest @BeforeTest
public void initialConfigs() throws Exception { public void initialConfigs() throws Exception {
setApiConfigs(apiConfig);
initializeOAuthApplication(); initializeOAuthApplication();
WebappPublisherConfig.init();
} }
@Test(description = "Publishes an API | will fail if there are any exceptions") @Test(description = "Publishes an API | will fail if there are any exceptions")
public void publishAPI() throws NoSuchFieldException, IllegalAccessException, public void publishAPI() throws NoSuchFieldException, IllegalAccessException,
APIManagerPublisherException { APIManagerPublisherException {
APIConfig apiConfig = new APIConfig();
setApiConfigs(apiConfig, "testAPI-0");
apiPublisherService.publishAPI(apiConfig);
}
@Test(description = "Testing for API status CREATED | will fail if there are any exceptions")
public void publishCreatedAPI() throws APIManagerPublisherException, NoSuchFieldException,
IllegalAccessException {
APIConfig apiConfig = new APIConfig();
setApiConfigs(apiConfig, "testAPI-1");
APIPublisherDataHolder apiPublisherDataHolder = Mockito.mock(APIPublisherDataHolder.getInstance().
getClass(), Mockito.CALLS_REAL_METHODS);
IntegrationClientService integrationClientService = Mockito.mock(IntegrationClientServiceImpl.
class, Mockito.CALLS_REAL_METHODS);
doReturn(integrationClientService).when(apiPublisherDataHolder).getIntegrationClientService();
PublisherClient publisherClient = APIPublisherDataHolder.getInstance().getIntegrationClientService().
getPublisherClient();
doReturn(publisherClient).when(integrationClientService).getPublisherClient();
APIsApi apIsApi = Mockito.mock(Api.class, Mockito.CALLS_REAL_METHODS);
doReturn(apIsApi).when(publisherClient).getApi();
API api = Mockito.mock(API.class, Mockito.CALLS_REAL_METHODS);
api.setStatus("CREATED");
doReturn(api).when(apIsApi).apisPost(Mockito.any(), Mockito.anyString());
apiPublisherService.publishAPI(apiConfig); apiPublisherService.publishAPI(apiConfig);
} }
private void setApiConfigs(APIConfig apiConfig) { @Test(description = "createAPIListWithNoApi | will fail if there are any exceptions")
apiConfig.setName("Windows Device Management Administrative Service"); private void publishWithNoAPIListCreated() throws APIManagerPublisherException {
apiConfig.setContext("/api/device-mgt/windows/v1.0/admin/devices"); APIConfig apiConfig = new APIConfig();
setApiConfigs(apiConfig, "testAPI-3");
APIPublisherDataHolder apiPublisherDataHolder = Mockito.mock(APIPublisherDataHolder.getInstance().
getClass(), Mockito.CALLS_REAL_METHODS);
IntegrationClientService integrationClientService = Mockito.mock(IntegrationClientServiceImpl.
class, Mockito.CALLS_REAL_METHODS);
doReturn(integrationClientService).when(apiPublisherDataHolder).getIntegrationClientService();
PublisherClient publisherClient = APIPublisherDataHolder.getInstance().getIntegrationClientService().
getPublisherClient();
doReturn(publisherClient).when(integrationClientService).getPublisherClient();
APIsApi apIsApi = Mockito.mock(Api.class, Mockito.CALLS_REAL_METHODS);
doReturn(apIsApi).when(publisherClient).getApi();
API api = Mockito.mock(API.class, Mockito.CALLS_REAL_METHODS);
api.setStatus("CREATED");
doReturn(api).when(apIsApi).apisPost(Mockito.any(), Mockito.anyString());
APIList apiList = Mockito.mock(APIList.class, Mockito.CALLS_REAL_METHODS);
APIInfo apiInfo = new APIInfo();
List<APIInfo> apiInfoList = new ArrayList<>();
apiInfoList.add(apiInfo);
apiList.list(apiInfoList);
doReturn(apiList).when(apIsApi).apisGet(Mockito.anyInt(), Mockito.anyInt(),
Mockito.anyString(), Mockito.anyString(), Mockito.anyString());
doReturn(api).when(apIsApi).apisApiIdPut(Mockito.anyString(), Mockito.any(),
Mockito.anyString(), Mockito.anyString(), Mockito.anyString());
apiPublisherService.publishAPI(apiConfig);
}
@Test(description = "createAPIList | will fail if there are any exceptions")
private void publishWithAPIListCreated() throws APIManagerPublisherException {
APIConfig apiConfig = new APIConfig();
setApiConfigs(apiConfig, "testAPI-2");
APIPublisherDataHolder apiPublisherDataHolder = Mockito.mock(APIPublisherDataHolder.getInstance().
getClass(), Mockito.CALLS_REAL_METHODS);
IntegrationClientService integrationClientService = Mockito.mock(IntegrationClientServiceImpl.
class, Mockito.CALLS_REAL_METHODS);
doReturn(integrationClientService).when(apiPublisherDataHolder).getIntegrationClientService();
PublisherClient publisherClient = APIPublisherDataHolder.getInstance().getIntegrationClientService().
getPublisherClient();
doReturn(publisherClient).when(integrationClientService).getPublisherClient();
APIsApi apIsApi = Mockito.mock(Api.class, Mockito.CALLS_REAL_METHODS);
doReturn(apIsApi).when(publisherClient).getApi();
API api = Mockito.mock(API.class, Mockito.CALLS_REAL_METHODS);
api.setStatus("CREATED");
doReturn(api).when(apIsApi).apisPost(Mockito.any(), Mockito.anyString());
APIList apiList = Mockito.mock(APIList.class, Mockito.CALLS_REAL_METHODS);
APIInfo apiInfo = new APIInfo();
apiInfo.setName("testAPI-2");
apiInfo.setVersion("1.0.0");
apiInfo.setId("test-one");
List<APIInfo> apiInfoList = new ArrayList<>();
apiInfoList.add(apiInfo);
apiList.list(apiInfoList);
doReturn(apiList).when(apIsApi).apisGet(Mockito.anyInt(), Mockito.anyInt(),
Mockito.anyString(), Mockito.anyString(), Mockito.anyString());
doReturn(api).when(apIsApi).apisApiIdPut(Mockito.anyString(), Mockito.any(),
Mockito.anyString(), Mockito.anyString(), Mockito.anyString());
apiConfig.setSharedWithAllTenants(false);
apiPublisherService.publishAPI(apiConfig);
}
private void setApiConfigs(APIConfig apiConfig, String name) {
apiConfig.setName(name);
apiConfig.setContext("api/device-mgt/windows/v1.g0/admin/devices");
apiConfig.setOwner("admin"); apiConfig.setOwner("admin");
apiConfig.setEndpoint("https://localhost:9443/api/device-mgt/windows/v1.0/admin/devices"); apiConfig.setEndpoint("https://localhost:9443/api/device-mgt/windows/v1.0/admin/devices");
apiConfig.setVersion("1.0.0"); apiConfig.setVersion("1.0.0");
@ -98,3 +195,4 @@ APIPublisherServiceTest extends BaseAPIPublisherTest {
} }
} }

@ -0,0 +1,39 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
~ 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.
-->
<!--
This configuration file represents the configuration that are needed
when publishing APIs to API Manager
-->
<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>
<!-- If it is true, the APIs of this instance will be published to the defined host -->
<PublishAPI>true</PublishAPI>
<!-- If it is true, the APIs of this instance will be updated when the webapps are redeployed -->
<EnabledUpdateApi>true</EnabledUpdateApi>
<!--Webapp will be published only when running below profiles-->
<Profiles>
<Profile>default</Profile>
</Profiles>
</WebappPublisherConfigs>
Loading…
Cancel
Save