Search in sources :

Example 1 with LabelDAO

use of org.wso2.carbon.apimgt.core.dao.LabelDAO in project carbon-apimgt by wso2.

the class LabelDAOImpl method updateLabel.

/**
 * @see LabelDAO#updateLabel(Label)
 */
@Override
public Label updateLabel(Label updatedLabel) throws APIMgtDAOException {
    final String query = "UPDATE AM_LABELS SET NAME=? , TYPE_NAME=? WHERE LABEL_ID=?";
    String labelId = updatedLabel.getId();
    List<String> accessURLs = updatedLabel.getAccessUrls();
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement statement = connection.prepareStatement(query)) {
        try {
            connection.setAutoCommit(false);
            statement.setString(1, updatedLabel.getName());
            if (updatedLabel.getType() != null) {
                statement.setString(2, updatedLabel.getType().toUpperCase(Locale.ENGLISH));
            } else {
                statement.setString(2, updatedLabel.getType());
            }
            statement.setString(3, updatedLabel.getId());
            statement.executeUpdate();
            connection.commit();
            deleteLabelAccessUrlMappings(labelId);
            if (APIMgtConstants.LABEL_TYPE_GATEWAY.equalsIgnoreCase(updatedLabel.getType())) {
                insertAccessUrlMappings(labelId, accessURLs);
            } else {
                accessURLs = new ArrayList<>();
            }
        } catch (SQLException e) {
            connection.rollback();
            String message = "Error while updating the label" + " [label id] " + labelId;
            throw new APIMgtDAOException(message, e, ExceptionCodes.LABEL_UPDATE_FAILED);
        } finally {
            connection.setAutoCommit(DAOUtil.isAutoCommit());
        }
        return new Label.Builder().id(labelId).name(updatedLabel.getName()).description(updatedLabel.getDescription()).accessUrls(accessURLs).type(updatedLabel.getType()).build();
    } catch (SQLException e) {
        String message = "Error while updating the label [label ID] " + labelId;
        throw new APIMgtDAOException(message, e, ExceptionCodes.LABEL_UPDATE_FAILED);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 2 with LabelDAO

use of org.wso2.carbon.apimgt.core.dao.LabelDAO in project carbon-apimgt by wso2.

the class LabelDAOImpl method getLabelsByName.

/**
 * @see LabelDAO#getLabelsByName(List)
 */
@SuppressFBWarnings("SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING")
@Override
public List<Label> getLabelsByName(List<String> labelNames) throws APIMgtDAOException {
    List<Label> matchingLabels = new ArrayList<>();
    if (!labelNames.isEmpty()) {
        final String query = "SELECT LABEL_ID, NAME FROM AM_LABELS WHERE NAME IN (" + DAOUtil.getParameterString(labelNames.size()) + ")";
        try (Connection connection = DAOUtil.getConnection();
            PreparedStatement statement = connection.prepareStatement(query)) {
            for (int i = 0; i < labelNames.size(); ++i) {
                statement.setString(i + 1, labelNames.get(i));
            }
            try (ResultSet rs = statement.executeQuery()) {
                while (rs.next()) {
                    Label label = new Label.Builder().id(rs.getString("LABEL_ID")).name(rs.getString("NAME")).accessUrls(getLabelAccessUrls(rs.getString("LABEL_ID"))).build();
                    matchingLabels.add(label);
                }
            }
        } catch (SQLException e) {
            String message = DAOUtil.DAO_ERROR_PREFIX + "retrieving labels";
            throw new APIMgtDAOException(message, e);
        }
    }
    return matchingLabels;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Label(org.wso2.carbon.apimgt.core.models.Label) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 3 with LabelDAO

use of org.wso2.carbon.apimgt.core.dao.LabelDAO in project carbon-apimgt by wso2.

the class ApiDAOImplIT method testUpdateGetDedicatedGateway.

@Test
public void testUpdateGetDedicatedGateway() throws Exception {
    ApiDAO apiDAO = DAOFactory.getApiDAO();
    API.APIBuilder builder = SampleTestObjectCreator.createDefaultAPI().apiDefinition(SampleTestObjectCreator.apiDefinition);
    API api = builder.build();
    testAddGetEndpoint();
    apiDAO.addAPI(api);
    String autoGeneratedLabelName = ContainerBasedGatewayConstants.PRIVATE_JET_API_PREFIX + api.getId();
    List<Label> labelList = new ArrayList<>();
    LabelDAO labelDAO = DAOFactory.getLabelDAO();
    Label autoGenLabel = new Label.Builder().id(UUID.randomUUID().toString()).name(autoGeneratedLabelName).accessUrls(null).build();
    labelList.add(autoGenLabel);
    labelDAO.addLabels(labelList);
    DedicatedGateway dedicatedGateway = new DedicatedGateway();
    dedicatedGateway.setEnabled(true);
    dedicatedGateway.setUpdatedBy(api.getCreatedBy());
    dedicatedGateway.setApiId(api.getId());
    List<String> labels = new ArrayList<>();
    labels.add(autoGeneratedLabelName);
    apiDAO.updateDedicatedGateway(dedicatedGateway, labels);
    DedicatedGateway result = apiDAO.getDedicatedGateway(api.getId());
    Assert.assertEquals(result.isEnabled(), dedicatedGateway.isEnabled());
}
Also used : Label(org.wso2.carbon.apimgt.core.models.Label) ArrayList(java.util.ArrayList) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) LabelDAO(org.wso2.carbon.apimgt.core.dao.LabelDAO) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) DedicatedGateway(org.wso2.carbon.apimgt.core.models.DedicatedGateway) Test(org.testng.annotations.Test)

Example 4 with LabelDAO

use of org.wso2.carbon.apimgt.core.dao.LabelDAO in project carbon-apimgt by wso2.

the class ApiDAOImplIT method testGetAPIsByStatusAndLabel.

@Test
public void testGetAPIsByStatusAndLabel() throws Exception {
    // Define statuses used in test
    final String publishedStatus = "PUBLISHED";
    final String createdStatus = "CREATED";
    // Define labels used in test
    final String publicLabel = "public";
    final String privateLabel = "private";
    // Add labels
    LabelDAO labelDAO = DAOFactory.getLabelDAO();
    Label label1 = SampleTestObjectCreator.createLabel(publicLabel, SampleTestObjectCreator.LABEL_TYPE_GATEWAY).build();
    Label label2 = SampleTestObjectCreator.createLabel(privateLabel, SampleTestObjectCreator.LABEL_TYPE_GATEWAY).build();
    List<Label> labelList = new ArrayList<>();
    labelList.add(label1);
    labelList.add(label2);
    LabelDAOImpl.addLabel(label1);
    LabelDAOImpl.addLabel(label2);
    String publicLabelId = labelDAO.getLabelIdByNameAndType(publicLabel, SampleTestObjectCreator.LABEL_TYPE_GATEWAY);
    String privateLabelId = labelDAO.getLabelIdByNameAndType(privateLabel, SampleTestObjectCreator.LABEL_TYPE_GATEWAY);
    ApiDAO apiDAO = DAOFactory.getApiDAO();
    // Define number of APIs to be created for a given status
    final int numberOfPublishedWithLabelPublicPrivate = 1;
    final int numberOfPublishedWithLabelPrivate = 2;
    final int numberOfCreatedWithLabelPublic = 3;
    // Add APIs with Status = PUBLISHED having labels "public" and "private"
    List<API> publishedAPIsPublicPrivateSummary = new ArrayList<>();
    List<String> labelsPublicPrivate = new ArrayList<>(Arrays.asList(publicLabelId, privateLabelId));
    testAddGetEndpoint();
    for (int i = 0; i < numberOfPublishedWithLabelPublicPrivate; ++i) {
        API api = SampleTestObjectCreator.createUniqueAPI().lifeCycleStatus(publishedStatus).labels(labelsPublicPrivate).build();
        publishedAPIsPublicPrivateSummary.add(SampleTestObjectCreator.getSummaryFromAPI(api));
        apiDAO.addAPI(api);
    }
    // Add APIs with Status = PUBLISHED having label "private"
    List<API> publishedAPIsPrivateSummary = new ArrayList<>();
    List<String> labelsPrivate = new ArrayList<>(Collections.singletonList(privateLabelId));
    for (int i = 0; i < numberOfPublishedWithLabelPrivate; ++i) {
        API api = SampleTestObjectCreator.createUniqueAPI().lifeCycleStatus(publishedStatus).labels(labelsPrivate).build();
        publishedAPIsPrivateSummary.add(SampleTestObjectCreator.getSummaryFromAPI(api));
        apiDAO.addAPI(api);
    }
    // Add APIs with Status = CREATED having labels "public"
    List<API> createdAPIsPublicSummary = new ArrayList<>();
    List<String> labelsPublic = new ArrayList<>(Collections.singletonList(publicLabelId));
    for (int i = 0; i < numberOfCreatedWithLabelPublic; ++i) {
        API api = SampleTestObjectCreator.createUniqueAPI().lifeCycleStatus(createdStatus).labels(labelsPublic).build();
        createdAPIsPublicSummary.add(SampleTestObjectCreator.getSummaryFromAPI(api));
        apiDAO.addAPI(api);
    }
    // verifying APIs with Status = PUBLISHED having labels "public" or "private"
    List<API> publishedPublicPrivateApiListFromDB = apiDAO.getAPIsByStatus(Arrays.asList(publicLabel, privateLabel), publishedStatus);
    List<API> publishedApisWithPublicOrPrivateLabels = new ArrayList<>();
    publishedApisWithPublicOrPrivateLabels.addAll(publishedAPIsPrivateSummary);
    publishedApisWithPublicOrPrivateLabels.addAll(publishedAPIsPublicPrivateSummary);
    Assert.assertTrue(APIUtils.isListsEqualIgnoreOrder(publishedPublicPrivateApiListFromDB, publishedApisWithPublicOrPrivateLabels, new APIComparator()));
    List<API> publishedApisWithPrivateLabels = new ArrayList<>();
    publishedApisWithPrivateLabels.addAll(publishedAPIsPrivateSummary);
    publishedApisWithPrivateLabels.addAll(publishedAPIsPublicPrivateSummary);
    // verifying APIs with Status = PUBLISHED having label "private"
    List<API> publishedPrivateApiListFromDB = apiDAO.getAPIsByStatus(Collections.singletonList(privateLabel), publishedStatus);
    Assert.assertTrue(APIUtils.isListsEqualIgnoreOrder(publishedPrivateApiListFromDB, publishedApisWithPrivateLabels, new APIComparator()));
    // verifying APIs with Status = CREATED having label "public"
    List<API> createdPublicApiListFromDB = apiDAO.getAPIsByStatus(Collections.singletonList(publicLabel), createdStatus);
    Assert.assertTrue(APIUtils.isListsEqualIgnoreOrder(createdPublicApiListFromDB, createdAPIsPublicSummary, new APIComparator()));
    // verifying APIs with Status = CREATED having label "private"
    List<API> createdPrivateApiListFromDB = apiDAO.getAPIsByStatus(Collections.singletonList(privateLabel), createdStatus);
    Assert.assertTrue(createdPrivateApiListFromDB.isEmpty());
}
Also used : Label(org.wso2.carbon.apimgt.core.models.Label) ArrayList(java.util.ArrayList) APIComparator(org.wso2.carbon.apimgt.core.util.APIComparator) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) LabelDAO(org.wso2.carbon.apimgt.core.dao.LabelDAO) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) Test(org.testng.annotations.Test)

Example 5 with LabelDAO

use of org.wso2.carbon.apimgt.core.dao.LabelDAO in project carbon-apimgt by wso2.

the class ApiDAOImplIT method testUpdateAPIWithLabels.

@Test
public void testUpdateAPIWithLabels() throws Exception {
    LabelDAO labelDAO = DAOFactory.getLabelDAO();
    Label label1 = SampleTestObjectCreator.createLabel("public", SampleTestObjectCreator.LABEL_TYPE_STORE).build();
    Label label2 = SampleTestObjectCreator.createLabel("private", SampleTestObjectCreator.LABEL_TYPE_STORE).build();
    LabelDAOImpl.addLabel(label1);
    LabelDAOImpl.addLabel(label2);
    ApiDAO apiDAO = DAOFactory.getApiDAO();
    List<String> labelIds = new ArrayList<>();
    labelIds.add(label1.getId());
    API.APIBuilder builder1 = SampleTestObjectCreator.createDefaultAPI();
    API api = builder1.labels(labelIds).build();
    testAddGetEndpoint();
    apiDAO.addAPI(api);
    API apiFromDBWithOneLabel = apiDAO.getAPI(api.getId());
    Assert.assertEquals(apiFromDBWithOneLabel.getLabels().size(), builder1.getLabels().size());
    labelIds.add(label2.getId());
    API substituteAPI = new API.APIBuilder(api).labels(labelIds).build();
    apiDAO.updateAPI(api.getId(), substituteAPI);
    API apiFromDB = apiDAO.getAPI(api.getId());
    API expectedAPI = SampleTestObjectCreator.copyAPIIgnoringNonEditableFields(api, substituteAPI);
    Assert.assertNotNull(apiFromDB);
    Assert.assertEquals(apiFromDB.getLabels().size(), expectedAPI.getLabels().size());
}
Also used : Label(org.wso2.carbon.apimgt.core.models.Label) ArrayList(java.util.ArrayList) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) LabelDAO(org.wso2.carbon.apimgt.core.dao.LabelDAO) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) Test(org.testng.annotations.Test)

Aggregations

LabelDAO (org.wso2.carbon.apimgt.core.dao.LabelDAO)71 Test (org.testng.annotations.Test)70 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)45 API (org.wso2.carbon.apimgt.core.models.API)44 ArrayList (java.util.ArrayList)33 Label (org.wso2.carbon.apimgt.core.models.Label)33 GatewaySourceGenerator (org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator)28 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)26 APILifecycleManager (org.wso2.carbon.apimgt.core.api.APILifecycleManager)26 APIBuilder (org.wso2.carbon.apimgt.core.models.API.APIBuilder)26 Endpoint (org.wso2.carbon.apimgt.core.models.Endpoint)17 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)16 LifecycleState (org.wso2.carbon.lcm.core.impl.LifecycleState)15 HashMap (java.util.HashMap)14 DedicatedGateway (org.wso2.carbon.apimgt.core.models.DedicatedGateway)14 APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)14 BeforeTest (org.testng.annotations.BeforeTest)13 IdentityProvider (org.wso2.carbon.apimgt.core.api.IdentityProvider)11 CompositeAPI (org.wso2.carbon.apimgt.core.models.CompositeAPI)11 WorkflowExtensionsConfigBuilder (org.wso2.carbon.apimgt.core.workflow.WorkflowExtensionsConfigBuilder)11