use of org.wso2.carbon.apimgt.core.models.Label 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());
}
use of org.wso2.carbon.apimgt.core.models.Label in project carbon-apimgt by wso2.
the class APIPublisherImplTestCase method testUpdateApiForLabelsWhenAPIHasOwnGateway.
@Test(description = "Test label update when api has own gateway", expectedExceptions = APIManagementException.class)
public void testUpdateApiForLabelsWhenAPIHasOwnGateway() throws APIManagementException {
List<String> labels = new ArrayList<>();
labels.add("default");
API.APIBuilder apiBuilder = SampleTestObjectCreator.createDefaultAPI().id(UUID.randomUUID().toString()).endpoint(Collections.emptyMap()).hasOwnGateway(true).labels(labels);
apiBuilder.apiPermission("");
apiBuilder.permissionMap(null);
apiBuilder.policies(Collections.emptySet());
apiBuilder.apiPolicy(null);
ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
LabelDAO labelDao = Mockito.mock(LabelDAO.class);
Mockito.when(apiDAO.getAPI(apiBuilder.getId())).thenReturn(apiBuilder.build());
GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class);
APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
APIGateway gateway = Mockito.mock(APIGateway.class);
PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.api, APIMgtConstants.DEFAULT_API_POLICY)).thenReturn(new APIPolicy(APIMgtConstants.DEFAULT_API_POLICY));
APIPublisherImpl apiPublisher = getApiPublisherImpl(apiDAO, apiLifecycleManager, gatewaySourceGenerator, gateway, policyDAO, labelDao);
Mockito.when(apiDAO.getApiSwaggerDefinition(apiBuilder.getId())).thenReturn(SampleTestObjectCreator.apiDefinition);
apiPublisher.updateAPI(apiBuilder);
}
use of org.wso2.carbon.apimgt.core.models.Label in project carbon-apimgt by wso2.
the class LabelsApiServiceImpl method labelsLabelIdPut.
/**
* Update the label
* @param body The body of the label with fields to be modified
* @param contentType The content type of the body
* @param request The ms4j request object
* @return 200 OK response.
* @throws NotFoundException
*/
@Override
public Response labelsLabelIdPut(String labelId, LabelDTO body, String contentType, Request request) throws NotFoundException {
try {
APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
body.labelUUID(labelId);
Label updatedLabel = apiMgtAdminService.updateLabel(LabelMappingUtil.fromDTOTLabel(body));
return Response.status(Response.Status.OK).entity(LabelMappingUtil.fromLabelToDTO(updatedLabel)).build();
} catch (APIManagementException e) {
String errorMessage = "Error occurred while adding label, label name: " + body.getName();
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler());
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
}
use of org.wso2.carbon.apimgt.core.models.Label in project carbon-apimgt by wso2.
the class LabelsApiServiceImpl method labelsLabelIdDelete.
/**
* Delete label by label id
*
* @param labelId Id of the label
* @param request msf4j request object
* @return 200 OK if the operation is successful
* @throws NotFoundException If failed to find the particular resource
*/
@Override
public Response labelsLabelIdDelete(String labelId, Request request) throws NotFoundException {
try {
if (labelId != null) {
APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
apiMgtAdminService.deleteLabel(labelId);
} else {
// mandatory parameters not provided
String errorMessage = "Label Id parameter should be provided";
ErrorHandler errorHandler = ExceptionCodes.PARAMETER_NOT_PROVIDED;
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorHandler);
log.error(errorMessage);
return Response.status(errorHandler.getHttpStatusCode()).entity(errorDTO).build();
}
} catch (APIManagementException e) {
String errorMessage = "Error occurred while deleting the label [labelId] " + labelId;
HashMap<String, String> paramList = new HashMap<String, String>();
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
return Response.status(Response.Status.NO_CONTENT).build();
}
use of org.wso2.carbon.apimgt.core.models.Label in project carbon-apimgt by wso2.
the class LabelsApiServiceImpl method labelsGet.
/**
* Gets all labels
* @param accept Accept header value
* @param request ms4j request object
* @return a list of label objects
* @throws NotFoundException
*/
@Override
public Response labelsGet(String labelId, String accept, Request request) throws NotFoundException {
List<Label> labels = new ArrayList<>();
try {
APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
// get all labels
if (labelId == null) {
labels = apiMgtAdminService.getLabels();
} else {
Label label = apiMgtAdminService.getLabelByID(labelId);
labels.add(label);
}
} catch (APIManagementException e) {
String errorMessage = "Error occurred while retrieving all labels";
ErrorHandler errorHandler = ExceptionCodes.LABEL_EXCEPTION;
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorHandler);
log.error(errorMessage, e);
return Response.status(errorHandler.getHttpStatusCode()).entity(errorDTO).build();
}
return Response.status(Response.Status.OK).entity(LabelMappingUtil.fromLabelArrayToListDTO(labels)).build();
}
Aggregations