Search in sources :

Example 6 with LabelException

use of org.wso2.carbon.apimgt.core.exception.LabelException in project carbon-apimgt by wso2.

the class APIStoreImplTestCase method getAllLabelsException.

@Test(description = "get all labels Exception", expectedExceptions = Exception.class)
public void getAllLabelsException() throws Exception {
    LabelDAO labelDao = Mockito.mock(LabelDAO.class);
    APIStore apiStore = getApiStoreImpl();
    List<Label> labels = new ArrayList<>();
    Label label = new Label.Builder().id("123").name("Default").type("STORE").accessUrls(new ArrayList<>()).build();
    labels.add(label);
    Mockito.when(labelDao.getLabels()).thenReturn(labels);
    Mockito.doThrow(new LabelException("Error occurred while retrieving labels ")).when(labelDao).getLabels();
    apiStore.getAllLabels();
}
Also used : Label(org.wso2.carbon.apimgt.core.models.Label) ArrayList(java.util.ArrayList) LabelDAO(org.wso2.carbon.apimgt.core.dao.LabelDAO) LabelException(org.wso2.carbon.apimgt.core.exception.LabelException) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 7 with LabelException

use of org.wso2.carbon.apimgt.core.exception.LabelException in project carbon-apimgt by wso2.

the class APIPublisherImplTestCase method testGetAllLabelsException.

@Test(description = "Exception when retrieving all labels", expectedExceptions = Exception.class)
public void testGetAllLabelsException() throws Exception {
    LabelDAO labelDAO = Mockito.mock(LabelDAO.class);
    APIPublisherImpl apiPublisher = getApiPublisherImpl(labelDAO);
    Mockito.when(labelDAO.getLabels()).thenThrow(new LabelException("Error occurred while retrieving labels"));
    apiPublisher.getAllLabels();
}
Also used : LabelDAO(org.wso2.carbon.apimgt.core.dao.LabelDAO) LabelException(org.wso2.carbon.apimgt.core.exception.LabelException) Test(org.testng.annotations.Test)

Example 8 with LabelException

use of org.wso2.carbon.apimgt.core.exception.LabelException in project carbon-apimgt by wso2.

the class APIStoreImpl method getAPIWSDLArchive.

@Override
public WSDLArchiveInfo getAPIWSDLArchive(String apiId, String labelName) throws APIMgtDAOException, APIMgtWSDLException, APINotFoundException, LabelException {
    API api = getApiDAO().getAPI(apiId);
    if (api == null) {
        throw new APINotFoundException("API with id " + apiId + " not found.", ExceptionCodes.API_NOT_FOUND);
    }
    // api.getLabels() should not be null and the labels should contain labelName
    if ((api.getLabels() == null || !api.getLabels().contains(labelName))) {
        throw new LabelException("API with id " + apiId + " does not contain label " + labelName, ExceptionCodes.LABEL_NOT_FOUND_IN_API);
    }
    try (InputStream wsdlZipInputStream = getApiDAO().getWSDLArchive(apiId)) {
        String rootPath = System.getProperty(APIMgtConstants.JAVA_IO_TMPDIR) + File.separator + APIMgtConstants.WSDLConstants.WSDL_ARCHIVES_FOLDERNAME + File.separator + UUID.randomUUID().toString();
        String archivePath = rootPath + File.separator + APIMgtConstants.WSDLConstants.WSDL_ARCHIVE_FILENAME;
        String extractedLocation = APIFileUtils.extractUploadedArchive(wsdlZipInputStream, APIMgtConstants.WSDLConstants.EXTRACTED_WSDL_ARCHIVE_FOLDERNAME, archivePath, rootPath);
        if (log.isDebugEnabled()) {
            log.debug("Successfully extracted WSDL archive in path: " + extractedLocation);
        }
        Label label = getLabelDAO().getLabelByName(labelName);
        WSDLProcessor processor = WSDLProcessFactory.getInstance().getWSDLProcessorForPath(extractedLocation);
        String wsdlPath = processor.getUpdatedWSDLPath(api, label);
        if (log.isDebugEnabled()) {
            log.debug("Successfully updated WSDLs in path [" + extractedLocation + "] with endpoints of label: " + labelName + " and context of API " + api.getContext());
        }
        String wsdlArchiveProcessedFileName = api.getProvider() + "-" + api.getName() + "-" + api.getVersion() + "-" + labelName + "-wsdl";
        APIFileUtils.archiveDirectory(wsdlPath, rootPath, wsdlArchiveProcessedFileName);
        if (log.isDebugEnabled()) {
            log.debug("Successfully archived WSDL files: " + wsdlPath);
        }
        WSDLArchiveInfo archiveInfo = new WSDLArchiveInfo(rootPath, wsdlArchiveProcessedFileName + ".zip");
        archiveInfo.setWsdlInfo(processor.getWsdlInfo());
        return archiveInfo;
    } catch (IOException e) {
        throw new APIMgtWSDLException(e);
    }
}
Also used : WSDLProcessor(org.wso2.carbon.apimgt.core.api.WSDLProcessor) APIMgtWSDLException(org.wso2.carbon.apimgt.core.exception.APIMgtWSDLException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Label(org.wso2.carbon.apimgt.core.models.Label) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) WSDLArchiveInfo(org.wso2.carbon.apimgt.core.models.WSDLArchiveInfo) IOException(java.io.IOException) LabelException(org.wso2.carbon.apimgt.core.exception.LabelException) APINotFoundException(org.wso2.carbon.apimgt.core.exception.APINotFoundException)

Example 9 with LabelException

use of org.wso2.carbon.apimgt.core.exception.LabelException in project carbon-apimgt by wso2.

the class APIStoreImpl method getAPIWSDL.

@Override
public String getAPIWSDL(String apiId, String labelName) throws APIMgtDAOException, APIMgtWSDLException, APINotFoundException, LabelException {
    API api = getApiDAO().getAPI(apiId);
    if (api == null) {
        throw new APINotFoundException("API with id " + apiId + " not found.", ExceptionCodes.API_NOT_FOUND);
    }
    // api.getLabels() should not be null and the labels should contain labelName
    if ((api.getLabels() == null || !api.getLabels().contains(labelName))) {
        throw new LabelException("API with id " + apiId + " does not contain label " + labelName, ExceptionCodes.LABEL_NOT_FOUND_IN_API);
    }
    String wsdl = getApiDAO().getWSDL(apiId);
    Label label = getLabelDAO().getLabelByName(labelName);
    if (!StringUtils.isEmpty(wsdl)) {
        WSDLProcessor processor;
        try {
            processor = WSDLProcessFactory.getInstance().getWSDLProcessor(wsdl.getBytes(APIMgtConstants.ENCODING_UTF_8));
            return new String(processor.getUpdatedWSDL(api, label), APIMgtConstants.ENCODING_UTF_8);
        } catch (UnsupportedEncodingException e) {
            throw new APIMgtWSDLException("WSDL content is not in utf-8 encoding", e, ExceptionCodes.CANNOT_PROCESS_WSDL_CONTENT);
        }
    }
    return null;
}
Also used : WSDLProcessor(org.wso2.carbon.apimgt.core.api.WSDLProcessor) APIMgtWSDLException(org.wso2.carbon.apimgt.core.exception.APIMgtWSDLException) Label(org.wso2.carbon.apimgt.core.models.Label) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) LabelException(org.wso2.carbon.apimgt.core.exception.LabelException) APINotFoundException(org.wso2.carbon.apimgt.core.exception.APINotFoundException)

Example 10 with LabelException

use of org.wso2.carbon.apimgt.core.exception.LabelException in project carbon-apimgt by wso2.

the class PoliciesApiServiceImplTestCase method testPoliciesTierLevelGetException.

@Test
public void testPoliciesTierLevelGetException() throws Exception {
    printTestMethodName();
    PoliciesApiServiceImpl policiesApiService = new PoliciesApiServiceImpl();
    APIPublisher apiPublisher = Mockito.mock(APIPublisherImpl.class);
    PowerMockito.mockStatic(RestAPIPublisherUtil.class);
    PowerMockito.when(RestAPIPublisherUtil.getApiPublisher(USER)).thenReturn(apiPublisher);
    Mockito.doThrow(new LabelException("Error occurred", ExceptionCodes.POLICY_LEVEL_NOT_SUPPORTED)).when(apiPublisher).getAllPoliciesByLevel(RestApiUtil.mapRestApiPolicyLevelToPolicyLevelEnum("subscription"));
    Response response = policiesApiService.policiesTierLevelGet("subscription", null, null, null, getRequest());
    assertEquals(response.getStatus(), 400);
    assertTrue(response.getEntity().toString().contains("Throttle Policy level invalid"));
}
Also used : Response(javax.ws.rs.core.Response) APIPublisher(org.wso2.carbon.apimgt.core.api.APIPublisher) LabelException(org.wso2.carbon.apimgt.core.exception.LabelException) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

LabelException (org.wso2.carbon.apimgt.core.exception.LabelException)10 Label (org.wso2.carbon.apimgt.core.models.Label)5 Test (org.testng.annotations.Test)4 LabelDAO (org.wso2.carbon.apimgt.core.dao.LabelDAO)4 Response (javax.ws.rs.core.Response)3 Test (org.junit.Test)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)3 ArrayList (java.util.ArrayList)2 BeforeTest (org.testng.annotations.BeforeTest)2 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)2 WSDLProcessor (org.wso2.carbon.apimgt.core.api.WSDLProcessor)2 APIMgtWSDLException (org.wso2.carbon.apimgt.core.exception.APIMgtWSDLException)2 APINotFoundException (org.wso2.carbon.apimgt.core.exception.APINotFoundException)2 API (org.wso2.carbon.apimgt.core.models.API)2 CompositeAPI (org.wso2.carbon.apimgt.core.models.CompositeAPI)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1