use of org.wso2.balana.attr.AttributeValue in project carbon-apimgt by wso2.
the class ApisApiServiceImplTestCase method testApisChangeLifecyclePostWithoutChecklistItemNonChange.
@Test
public void testApisChangeLifecyclePostWithoutChecklistItemNonChange() throws Exception {
printTestMethodName();
ApisApiServiceImpl apisApiService = new ApisApiServiceImpl();
APIPublisher apiPublisher = Mockito.mock(APIPublisherImpl.class);
String checklist = "test1:test1,test2:test2";
String action = "CheckListItemChangeDifferent";
Map<String, Boolean> lifecycleChecklistMap = new HashMap<>();
if (checklist != null) {
String[] checkList = checklist.split(",");
for (String checkList1 : checkList) {
StringTokenizer attributeTokens = new StringTokenizer(checkList1, ":");
String attributeName = attributeTokens.nextToken();
Boolean attributeValue = Boolean.valueOf(attributeTokens.nextToken());
lifecycleChecklistMap.put(attributeName, attributeValue);
}
}
WorkflowResponse workflowResponse = new GeneralWorkflowResponse();
workflowResponse.setWorkflowStatus(WorkflowStatus.CREATED);
PowerMockito.mockStatic(RestAPIPublisherUtil.class);
PowerMockito.when(RestAPIPublisherUtil.getApiPublisher(USER)).thenReturn(apiPublisher);
String apiId = UUID.randomUUID().toString();
Mockito.doReturn(workflowResponse).doThrow(new IllegalArgumentException()).when(apiPublisher).updateAPIStatus(apiId, action, lifecycleChecklistMap);
Response response = apisApiService.apisChangeLifecyclePost(action, apiId, checklist, null, null, getRequest());
assertEquals(response.getStatus(), 202);
assertTrue(response.getEntity().toString().contains("CREATED"));
}
use of org.wso2.balana.attr.AttributeValue in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method apisChangeLifecyclePost.
/**
* Change the lifecycle state of an API
*
* @param action lifecycle action
* @param apiId UUID of API
* @param lifecycleChecklist lifecycle check list items
* @param ifMatch If-Match header value
* @param ifUnmodifiedSince If-Unmodified-Since header value
* @param request msf4j request object
* @return 200 OK if the operation is succesful
* @throws NotFoundException When the particular resource does not exist in the system
*/
@Override
public Response apisChangeLifecyclePost(String action, String apiId, String lifecycleChecklist, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
String username = RestApiUtil.getLoggedInUsername(request);
Map<String, Boolean> lifecycleChecklistMap = new HashMap<>();
WorkflowResponseDTO response = null;
try {
if (lifecycleChecklist != null) {
String[] checkList = lifecycleChecklist.split(",");
for (String checkList1 : checkList) {
StringTokenizer attributeTokens = new StringTokenizer(checkList1, ":");
String attributeName = attributeTokens.nextToken();
Boolean attributeValue = Boolean.valueOf(attributeTokens.nextToken());
lifecycleChecklistMap.put(attributeName, attributeValue);
}
}
if (action.trim().equals(APIMgtConstants.CHECK_LIST_ITEM_CHANGE_EVENT)) {
RestAPIPublisherUtil.getApiPublisher(username).updateCheckListItem(apiId, action, lifecycleChecklistMap);
WorkflowResponse workflowResponse = new GeneralWorkflowResponse();
// since workflows are not defined for checklist items
workflowResponse.setWorkflowStatus(WorkflowStatus.APPROVED);
response = MappingUtil.toWorkflowResponseDTO(workflowResponse);
return Response.ok().entity(response).build();
} else {
WorkflowResponse workflowResponse = RestAPIPublisherUtil.getApiPublisher(username).updateAPIStatus(apiId, action, lifecycleChecklistMap);
response = MappingUtil.toWorkflowResponseDTO(workflowResponse);
// be in either pending or approved state) send back the workflow response
if (WorkflowStatus.CREATED == workflowResponse.getWorkflowStatus()) {
URI location = new URI(RestApiConstants.RESOURCE_PATH_APIS + "/" + apiId);
return Response.status(Response.Status.ACCEPTED).header(RestApiConstants.LOCATION_HEADER, location).entity(response).build();
} else {
return Response.ok().entity(response).build();
}
}
} catch (APIManagementException e) {
String errorMessage = "Error while updating lifecycle of API" + apiId + " to " + action;
Map<String, String> paramList = new HashMap<>();
paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
} catch (URISyntaxException e) {
String errorMessage = "Error while adding location header in response for api : " + apiId;
Map<String, String> paramList = new HashMap<>();
paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
ErrorHandler errorHandler = ExceptionCodes.LOCATION_HEADER_INCORRECT;
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorHandler, paramList);
log.error(errorMessage, e);
return Response.status(errorHandler.getHttpStatusCode()).entity(errorDTO).build();
}
}
use of org.wso2.balana.attr.AttributeValue in project ballerina by ballerina-lang.
the class ServiceProtoUtils method getServiceConfiguration.
static ServiceConfiguration getServiceConfiguration(ServiceNode serviceNode) {
String rpcEndpoint = null;
boolean clientStreaming = false;
boolean serverStreaming = false;
boolean generateClientConnector = false;
for (AnnotationAttachmentNode annotationNode : serviceNode.getAnnotationAttachments()) {
if (!ServiceProtoConstants.ANN_SERVICE_CONFIG.equals(annotationNode.getAnnotationName().getValue())) {
continue;
}
if (annotationNode.getExpression() instanceof BLangRecordLiteral) {
List<BLangRecordLiteral.BLangRecordKeyValue> attributes = ((BLangRecordLiteral) annotationNode.getExpression()).getKeyValuePairs();
for (BLangRecordLiteral.BLangRecordKeyValue attributeNode : attributes) {
String attributeName = attributeNode.getKey().toString();
String attributeValue = attributeNode.getValue() != null ? attributeNode.getValue().toString() : null;
switch(attributeName) {
case ServiceProtoConstants.SERVICE_CONFIG_RPC_ENDPOINT:
{
rpcEndpoint = attributeValue != null ? attributeValue : null;
break;
}
case ServiceProtoConstants.SERVICE_CONFIG_CLIENT_STREAMING:
{
clientStreaming = attributeValue != null && Boolean.parseBoolean(attributeValue);
break;
}
case ServiceProtoConstants.SERVICE_CONFIG_SERVER_STREAMING:
{
serverStreaming = attributeValue != null && Boolean.parseBoolean(attributeValue);
break;
}
case ServiceProtoConstants.SERVICE_CONFIG_GENERATE_CLIENT:
{
generateClientConnector = attributeValue != null && Boolean.parseBoolean(attributeValue);
break;
}
default:
{
break;
}
}
}
}
}
return new ServiceConfiguration(rpcEndpoint, clientStreaming, serverStreaming, generateClientConnector);
}
use of org.wso2.balana.attr.AttributeValue in project charon by wso2.
the class JSONDecoder method buildSimpleAttribute.
/*
* Return a simple attribute with the user defined value included and necessary attribute characteristics set
*
* @param attributeSchema - Attribute schema
* @param attributeValue - value for the attribute
* @return SimpleAttribute
*/
public SimpleAttribute buildSimpleAttribute(AttributeSchema attributeSchema, Object attributeValue) throws CharonException, BadRequestException {
Object attributeValueObject = AttributeUtil.getAttributeValueFromString(attributeValue, attributeSchema.getType());
SimpleAttribute simpleAttribute = new SimpleAttribute(attributeSchema.getName(), attributeValueObject);
return (SimpleAttribute) DefaultAttributeFactory.createAttribute(attributeSchema, simpleAttribute);
}
use of org.wso2.balana.attr.AttributeValue in project charon by wso2.
the class JSONDecoder method buildComplexMultiValuedAttribute.
/*
* Return complex type multi valued attribute with the user defined
* value included and necessary attribute characteristics set
* @param attributeSchema - Attribute schema
* @param attributeValues - values for the attribute
* @return MultiValuedAttribute
*/
public MultiValuedAttribute buildComplexMultiValuedAttribute(AttributeSchema attributeSchema, JSONArray attributeValues) throws CharonException, BadRequestException {
try {
MultiValuedAttribute multiValuedAttribute = new MultiValuedAttribute(attributeSchema.getName());
List<Attribute> complexAttributeValues = new ArrayList<Attribute>();
List<Object> simpleAttributeValues = new ArrayList<>();
// iterate through JSONArray and create the list of string values.
for (int i = 0; i < attributeValues.length(); i++) {
Object attributeValue = attributeValues.get(i);
if (attributeValue instanceof JSONObject) {
JSONObject complexAttributeValue = (JSONObject) attributeValue;
complexAttributeValues.add(buildComplexValue(attributeSchema, complexAttributeValue));
} else if (attributeValue instanceof String || attributeValue instanceof Integer || attributeValue instanceof Double || attributeValue instanceof Boolean || attributeValue == null) {
if (logger.isDebugEnabled()) {
if (attributeValue != null) {
logger.debug("Primitive attribute type detected. Attribute type: " + attributeValue.getClass().getName() + ", attribute value: " + attributeValue);
} else {
logger.debug("Attribute value is null.");
}
}
// If an attribute is passed without a value, no need to save it.
if (attributeValue == null) {
continue;
}
simpleAttributeValues.add(attributeValue);
} else {
String error = "Unknown JSON representation for the MultiValued attribute " + attributeSchema.getName() + " which has data type as " + attributeSchema.getType();
throw new BadRequestException(error, ResponseCodeConstants.INVALID_SYNTAX);
}
}
multiValuedAttribute.setAttributeValues(complexAttributeValues);
multiValuedAttribute.setAttributePrimitiveValues(simpleAttributeValues);
return (MultiValuedAttribute) DefaultAttributeFactory.createAttribute(attributeSchema, multiValuedAttribute);
} catch (JSONException e) {
String error = "Error in accessing JSON value of multivalued attribute";
throw new CharonException(error, e);
}
}
Aggregations