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());
}
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());
}
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);
}
}
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);
}
}
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);
}
}
}
Aggregations