use of org.wso2.carbon.apimgt.core.dao.LabelDAO in project carbon-apimgt by wso2.
the class APIMgtAdminServiceImplTestCase method testDeleteLabelException.
@Test(description = "Exception when deleting a label", expectedExceptions = APIManagementException.class)
public void testDeleteLabelException() throws APIManagementException {
LabelDAO labelDAO = Mockito.mock(LabelDAO.class);
APIMgtAdminServiceImpl adminService = getAPIMgtAdminServiceImpl(labelDAO);
Label label = SampleTestObjectCreator.createLabel("Public", SampleTestObjectCreator.LABEL_TYPE_STORE).build();
String labelId = label.getId();
Mockito.doThrow(new APIMgtDAOException("Error occurred while deleting label [labelId] " + labelId)).when(labelDAO).deleteLabel(labelId);
adminService.deleteLabel(labelId);
Mockito.verify(labelDAO, Mockito.times(1)).deleteLabel(labelId);
}
use of org.wso2.carbon.apimgt.core.dao.LabelDAO in project carbon-apimgt by wso2.
the class LabelDAOImpl method getLabelIdByNameAndType.
/**
* @see LabelDAO#getLabelIdByNameAndType(String, String)
*/
@Override
public String getLabelIdByNameAndType(String name, String type) throws APIMgtDAOException {
final String query = "SELECT LABEL_ID FROM AM_LABELS WHERE NAME = ? AND TYPE_NAME = ?";
try (Connection connection = DAOUtil.getConnection();
PreparedStatement statement = connection.prepareStatement(query)) {
statement.setString(1, name);
statement.setString(2, type);
try (ResultSet rs = statement.executeQuery()) {
if (rs.next()) {
return rs.getString("LABEL_ID");
} else {
return null;
}
}
} catch (SQLException e) {
String message = "Error while retrieving label ID of label [label Name] " + name;
throw new APIMgtDAOException(message, e, ExceptionCodes.LABEL_NOT_FOUND);
}
}
use of org.wso2.carbon.apimgt.core.dao.LabelDAO in project carbon-apimgt by wso2.
the class LabelDAOImpl method deleteLabel.
/**
* @see LabelDAO#deleteLabel(String)
*/
@Override
public void deleteLabel(String labelId) throws APIMgtDAOException {
final String query = "DELETE FROM AM_LABELS WHERE LABEL_ID = ?";
try (Connection connection = DAOUtil.getConnection();
PreparedStatement statement = connection.prepareStatement(query)) {
try {
connection.setAutoCommit(false);
statement.setString(1, labelId);
statement.execute();
connection.commit();
} catch (SQLException e) {
connection.rollback();
String message = DAOUtil.DAO_ERROR_PREFIX + "deleting the label [label id] " + labelId;
throw new APIMgtDAOException(message, e);
} finally {
connection.setAutoCommit(DAOUtil.isAutoCommit());
}
} catch (SQLException e) {
String message = DAOUtil.DAO_ERROR_PREFIX + "deleting the label [label id] " + labelId;
throw new APIMgtDAOException(message, e);
}
}
use of org.wso2.carbon.apimgt.core.dao.LabelDAO in project carbon-apimgt by wso2.
the class DAOFactory method getLabelDAO.
public static LabelDAO getLabelDAO() throws APIMgtDAOException {
LabelDAO labelDAO = null;
try (Connection connection = DAOUtil.getConnection()) {
String driverName = connection.getMetaData().getDriverName();
if (driverName.contains(MYSQL) || driverName.contains(H2)) {
labelDAO = new LabelDAOImpl();
} else if (driverName.contains(DB2)) {
} else if (driverName.contains(MS_SQL) || driverName.contains(MICROSOFT)) {
labelDAO = new LabelDAOImpl();
} else if (driverName.contains(POSTGRE)) {
labelDAO = new LabelDAOImpl();
} else if (driverName.contains(ORACLE)) {
labelDAO = new LabelDAOImpl();
} else {
throw new APIMgtDAOException("Unhandled DB driver: " + driverName + " detected", ExceptionCodes.APIM_DAO_EXCEPTION);
}
} catch (SQLException e) {
throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "getting LabelDAO", e);
}
setup();
return labelDAO;
}
use of org.wso2.carbon.apimgt.core.dao.LabelDAO in project carbon-apimgt by wso2.
the class APIPublisherImplTestCase method testUpdateDedicatedGatewayWhenLabelIsNotNull.
@Test(description = "Update dedicated gateway when label is not null and present in the system")
public void testUpdateDedicatedGatewayWhenLabelIsNotNull() throws APIManagementException {
ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class);
LabelDAO labelDAO = Mockito.mock(LabelDAO.class);
IdentityProvider identityProvider = Mockito.mock(IdentityProvider.class);
API api = SampleTestObjectCreator.createDefaultAPI().build();
String uuid = api.getId();
Mockito.when(apiDAO.getAPI(uuid)).thenReturn(api);
Mockito.when(identityProvider.getRoleName(SampleTestObjectCreator.DEVELOPER_ROLE_ID)).thenReturn(DEVELOPER_ROLE);
Mockito.when(identityProvider.getRoleName(SampleTestObjectCreator.ADMIN_ROLE_ID)).thenReturn(ADMIN_ROLE);
Mockito.when(apiDAO.getApiSwaggerDefinition(api.getId())).thenReturn(SampleTestObjectCreator.apiDefinition);
String autoGenLabelName = ContainerBasedGatewayConstants.PRIVATE_JET_API_PREFIX + uuid;
Label label = new Label.Builder().id(UUID.randomUUID().toString()).name(autoGenLabelName).accessUrls(null).build();
Mockito.when(labelDAO.getLabelByName(autoGenLabelName)).thenReturn(label);
Mockito.when(labelDAO.getLabelIdByNameAndType(Mockito.anyString(), Mockito.anyString())).thenReturn(UUID.randomUUID().toString());
APIPublisherImpl apiPublisher = getApiPublisherImpl(identityProvider, apiDAO, labelDAO, gatewaySourceGenerator);
DedicatedGateway dedicatedGateway = SampleTestObjectCreator.createDedicatedGateway(uuid, true, api.getCreatedBy());
apiPublisher.updateDedicatedGateway(dedicatedGateway);
Mockito.verify(labelDAO, Mockito.times(0)).addLabels(Mockito.anyList());
}
Aggregations