Search in sources :

Example 1 with AttributeValue

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"));
}
Also used : WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse) GeneralWorkflowResponse(org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse) Response(javax.ws.rs.core.Response) StringTokenizer(java.util.StringTokenizer) HashMap(java.util.HashMap) GeneralWorkflowResponse(org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse) APIPublisher(org.wso2.carbon.apimgt.core.api.APIPublisher) WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse) GeneralWorkflowResponse(org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 2 with AttributeValue

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();
    }
}
Also used : ErrorHandler(org.wso2.carbon.apimgt.core.exception.ErrorHandler) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) GeneralWorkflowResponse(org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) WorkflowResponseDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.WorkflowResponseDTO) StringTokenizer(java.util.StringTokenizer) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse) GeneralWorkflowResponse(org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse) Map(java.util.Map) HashMap(java.util.HashMap)

Example 3 with AttributeValue

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);
}
Also used : ServiceConfiguration(org.ballerinalang.net.grpc.config.ServiceConfiguration) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode)

Example 4 with AttributeValue

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);
}
Also used : SimpleAttribute(org.wso2.charon3.core.attributes.SimpleAttribute) AbstractSCIMObject(org.wso2.charon3.core.objects.AbstractSCIMObject) JSONObject(org.json.JSONObject)

Example 5 with AttributeValue

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);
    }
}
Also used : MultiValuedAttribute(org.wso2.charon3.core.attributes.MultiValuedAttribute) SimpleAttribute(org.wso2.charon3.core.attributes.SimpleAttribute) ComplexAttribute(org.wso2.charon3.core.attributes.ComplexAttribute) Attribute(org.wso2.charon3.core.attributes.Attribute) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) MultiValuedAttribute(org.wso2.charon3.core.attributes.MultiValuedAttribute) JSONObject(org.json.JSONObject) BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException) AbstractSCIMObject(org.wso2.charon3.core.objects.AbstractSCIMObject) JSONObject(org.json.JSONObject) CharonException(org.wso2.charon3.core.exceptions.CharonException) AbstractCharonException(org.wso2.charon3.core.exceptions.AbstractCharonException)

Aggregations

ArrayList (java.util.ArrayList)19 ApplyElementDTO (org.wso2.balana.utils.policy.dto.ApplyElementDTO)14 AttributeValueElementDTO (org.wso2.balana.utils.policy.dto.AttributeValueElementDTO)14 MultiValuedAttribute (org.wso2.charon3.core.attributes.MultiValuedAttribute)14 SimpleAttribute (org.wso2.charon3.core.attributes.SimpleAttribute)14 ComplexAttribute (org.wso2.charon3.core.attributes.ComplexAttribute)13 Attribute (org.wso2.charon3.core.attributes.Attribute)12 CharonException (org.wso2.charon3.core.exceptions.CharonException)10 Map (java.util.Map)9 AttributeValue (org.wso2.balana.attr.AttributeValue)9 AbstractSCIMObject (org.wso2.charon3.core.objects.AbstractSCIMObject)9 HashMap (java.util.HashMap)8 URI (java.net.URI)7 JSONObject (org.json.JSONObject)7 AttributeDesignatorDTO (org.wso2.balana.utils.policy.dto.AttributeDesignatorDTO)6 BadRequestException (org.wso2.charon3.core.exceptions.BadRequestException)6 AbstractAttribute (org.wso2.charon3.core.attributes.AbstractAttribute)5 SCIMObject (org.wso2.charon3.core.objects.SCIMObject)5 List (java.util.List)4 JSONException (org.json.JSONException)4