Search in sources :

Example 91 with Label

use of org.wso2.carbon.apimgt.core.models.Label in project carbon-apimgt by wso2.

the class LabelMappingUtilTestCase method testToLabelListDTO.

@Test
public void testToLabelListDTO() {
    List<Label> labelList = new ArrayList<>();
    Label label1 = SampleTestObjectCreator.createLabel("label1").build();
    Label label2 = SampleTestObjectCreator.createLabel("label2").build();
    labelList.add(label1);
    labelList.add(label2);
    LabelListDTO labelListDTO = LabelMappingUtil.toLabelListDTO(labelList);
    assertEquals(labelListDTO.getCount(), (Integer) labelList.size());
    assertEquals(labelListDTO.getList().get(0).getName(), label1.getName());
    assertEquals(labelListDTO.getList().get(0).getAccessUrls(), label1.getAccessUrls());
    assertEquals(labelListDTO.getList().get(0).getLabelId(), label1.getId());
    assertEquals(labelListDTO.getList().get(1).getName(), label2.getName());
    assertEquals(labelListDTO.getList().get(1).getAccessUrls(), label2.getAccessUrls());
    assertEquals(labelListDTO.getList().get(1).getLabelId(), label2.getId());
}
Also used : Label(org.wso2.carbon.apimgt.core.models.Label) ArrayList(java.util.ArrayList) LabelListDTO(org.wso2.carbon.apimgt.rest.api.store.dto.LabelListDTO) Test(org.testng.annotations.Test)

Example 92 with Label

use of org.wso2.carbon.apimgt.core.models.Label in project carbon-apimgt by wso2.

the class LabelsApiServiceImplTestCase method testLabelInfoGetWithNull.

@Test
public void testLabelInfoGetWithNull() throws APIManagementException, NotFoundException {
    TestUtil.printTestMethodName();
    LabelsApiServiceImpl labelsApiService = new LabelsApiServiceImpl();
    APIStore apiStore = Mockito.mock(APIStoreImpl.class);
    PowerMockito.mockStatic(RestApiUtil.class);
    PowerMockito.when(RestApiUtil.getConsumer(USER)).thenReturn(apiStore);
    Request request = TestUtil.getRequest();
    PowerMockito.when(RestApiUtil.getLoggedInUsername(request)).thenReturn(USER);
    List<String> labelNames = new ArrayList<>();
    List<Label> labelList = new ArrayList<>();
    Mockito.when(apiStore.getAllLabels()).thenReturn(labelList);
    Response response = labelsApiService.labelsGet(null, null, null, null, request);
    Assert.assertEquals(200, response.getStatus());
}
Also used : Response(javax.ws.rs.core.Response) Request(org.wso2.msf4j.Request) ArrayList(java.util.ArrayList) Label(org.wso2.carbon.apimgt.core.models.Label) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 93 with Label

use of org.wso2.carbon.apimgt.core.models.Label in project carbon-apimgt by wso2.

the class ApiDAOImpl method updateDedicatedGateway.

/**
 * @see ApiDAO#updateDedicatedGateway(DedicatedGateway, List)
 */
@Override
public void updateDedicatedGateway(DedicatedGateway dedicatedGateway, List<String> labels) throws APIMgtDAOException {
    // labels will come in 2 ways.
    // 1. auto-generated label - Update from dedicateGateway false to true
    // 2. default label - Update from dedicatedGateway true to false
    String apiId = dedicatedGateway.getApiId();
    final String query = "UPDATE AM_API SET HAS_OWN_GATEWAY = ?, LAST_UPDATED_TIME = ?, UPDATED_BY = ? " + "WHERE UUID = ?";
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement statement = connection.prepareStatement(query)) {
        try {
            connection.setAutoCommit(false);
            statement.setBoolean(1, dedicatedGateway.isEnabled());
            statement.setTimestamp(2, Timestamp.valueOf(LocalDateTime.now()));
            statement.setString(3, dedicatedGateway.getUpdatedBy());
            statement.setString(4, apiId);
            // if the labels are not null or not empty
            if (labels != null && !labels.isEmpty()) {
                deleteLabelsMapping(connection, apiId);
                addLabelMapping(connection, apiId, labels);
            }
            statement.execute();
            connection.commit();
        } catch (SQLException e) {
            String msg = "Couldn't update dedicated Gateway details of API : " + apiId;
            connection.rollback();
            throw new APIMgtDAOException(msg, e);
        } finally {
            connection.setAutoCommit(DAOUtil.isAutoCommit());
        }
    } catch (SQLException e) {
        throw new APIMgtDAOException("Error Executing query for updating Container Based Gateway", e);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 94 with Label

use of org.wso2.carbon.apimgt.core.models.Label 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 95 with Label

use of org.wso2.carbon.apimgt.core.models.Label in project carbon-apimgt by wso2.

the class APIMgtAdminServiceImpl method registerGatewayLabels.

@Override
public void registerGatewayLabels(List<Label> labels, String overwriteLabels) throws APIManagementException {
    if (!labels.isEmpty()) {
        List<String> labelNames = new ArrayList<>();
        boolean overwriteValues = Boolean.parseBoolean(overwriteLabels);
        for (Label label : labels) {
            labelNames.add(label.getName());
        }
        try {
            List<Label> existingLabels = labelDAO.getLabelsByName(labelNames);
            if (!existingLabels.isEmpty()) {
                List<Label> labelsToRemove = new ArrayList<>();
                for (Label existingLabel : existingLabels) {
                    for (Label label : labels) {
                        if (existingLabel.getName().equals(label.getName())) {
                            if (overwriteValues) {
                                labelDAO.updateLabel(label);
                            }
                            labelsToRemove.add(label);
                        }
                    }
                }
                // Remove already existing labels from the list
                labels.removeAll(labelsToRemove);
            }
            labelDAO.addLabels(labels);
        } catch (APIMgtDAOException e) {
            String msg = "Error occurred while registering gateway labels";
            log.error(msg, e);
            throw new APIManagementException(msg, ExceptionCodes.APIMGT_DAO_EXCEPTION);
        }
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ArrayList(java.util.ArrayList) Label(org.wso2.carbon.apimgt.core.models.Label)

Aggregations

Label (org.wso2.carbon.apimgt.core.models.Label)65 ArrayList (java.util.ArrayList)52 Test (org.testng.annotations.Test)45 LabelDAO (org.wso2.carbon.apimgt.core.dao.LabelDAO)32 API (org.wso2.carbon.apimgt.core.models.API)29 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)28 SQLException (java.sql.SQLException)21 PreparedStatement (java.sql.PreparedStatement)20 HashMap (java.util.HashMap)16 Connection (java.sql.Connection)15 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)14 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)13 Test (org.junit.Test)12 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)11 CompositeAPI (org.wso2.carbon.apimgt.core.models.CompositeAPI)11 Map (java.util.Map)10 BeforeTest (org.testng.annotations.BeforeTest)9 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)8 Response (javax.ws.rs.core.Response)8 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)8