Search in sources :

Example 6 with Tag

use of org.wso2.carbon.apimgt.core.models.Tag in project carbon-apimgt by wso2.

the class TagMappingUtilTestCase method testFromTagToDTO.

@Test
public void testFromTagToDTO() {
    Tag.Builder tagBuilder = new Tag.Builder();
    Tag tag = tagBuilder.name("tag1").count(1).build();
    TagDTO tagDTO = TagMappingUtil.fromTagToDTO(tag);
    assertEquals(tag.getName(), tagDTO.getName());
    assertEquals((Integer) tag.getCount(), tagDTO.getWeight());
}
Also used : TagDTO(org.wso2.carbon.apimgt.rest.api.store.dto.TagDTO) Tag(org.wso2.carbon.apimgt.core.models.Tag) Test(org.testng.annotations.Test)

Example 7 with Tag

use of org.wso2.carbon.apimgt.core.models.Tag in project jaggery by wso2.

the class RegistryHostObject method jsFunction_search.

public static Scriptable jsFunction_search(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException {
    String functionName = "search";
    int argsCount = args.length;
    if (argsCount != 1) {
        HostObjectUtil.invalidNumberOfArgs(hostObjectName, functionName, argsCount, false);
    }
    if (!(args[0] instanceof NativeObject)) {
        HostObjectUtil.invalidArgsError(hostObjectName, functionName, "1", "json", args[0], false);
    }
    NativeObject options = (NativeObject) args[0];
    CustomSearchParameterBean parameters = new CustomSearchParameterBean();
    String path = null;
    List<String[]> values = new ArrayList<String[]>();
    DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
    String val;
    for (Object idObj : options.getIds()) {
        String id = (String) idObj;
        Object value = options.get(id, options);
        if (value == null || value instanceof Undefined) {
            continue;
        }
        if ("path".equals(id)) {
            path = (String) value;
            continue;
        }
        if ("createdBefore".equals(id) || "createdAfter".equals(id) || "updatedBefore".equals(id) || "updatedAfter".equals(id)) {
            long t;
            if (value instanceof Number) {
                t = ((Number) value).longValue();
            } else {
                t = Long.parseLong(HostObjectUtil.serializeObject(value));
            }
            val = new String(dateFormat.format(new Date(t)).getBytes());
        } else {
            val = HostObjectUtil.serializeObject(value);
        }
        values.add(new String[] { id, val });
    }
    parameters.setParameterValues(values.toArray(new String[0][0]));
    RegistryHostObject registryHostObject = (RegistryHostObject) thisObj;
    int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
    try {
        UserRegistry userRegistry = RegistryHostObjectContext.getRegistryService().getRegistry(registryHostObject.registry.getUserName(), tenantId);
        Registry configRegistry = RegistryHostObjectContext.getRegistryService().getConfigSystemRegistry(tenantId);
        AdvancedSearchResultsBean resultsBean = registryHostObject.search(configRegistry, userRegistry, parameters);
        if (resultsBean.getResourceDataList() == null) {
            ScriptableObject error = (ScriptableObject) cx.newObject(thisObj);
            error.put("error", error, true);
            error.put("description", error, resultsBean.getErrorMessage());
            return error;
        }
        List<ScriptableObject> results = new ArrayList<ScriptableObject>();
        for (ResourceData resourceData : resultsBean.getResourceDataList()) {
            String resourcePath = resourceData.getResourcePath();
            if (path != null && !resourcePath.startsWith(path)) {
                continue;
            }
            ScriptableObject result = (ScriptableObject) cx.newObject(thisObj);
            result.put("author", result, resourceData.getAuthorUserName());
            result.put("rating", result, resourceData.getAverageRating());
            result.put("created", result, resourceData.getCreatedOn().getTime().getTime());
            result.put("description", result, resourceData.getDescription());
            result.put("name", result, resourceData.getName());
            result.put("path", result, resourceData.getResourcePath());
            List<ScriptableObject> tags = new ArrayList<ScriptableObject>();
            if (resourceData.getTagCounts() != null) {
                for (TagCount tagCount : resourceData.getTagCounts()) {
                    ScriptableObject tag = (ScriptableObject) cx.newObject(thisObj);
                    tag.put("name", tag, tagCount.getKey());
                    tag.put("count", tag, tagCount.getValue());
                    tags.add(tag);
                }
            }
            result.put("tags", result, cx.newArray(thisObj, tags.toArray()));
            results.add(result);
        }
        return cx.newArray(thisObj, results.toArray());
    } catch (RegistryException e) {
        throw new ScriptException(e);
    } catch (CarbonException e) {
        throw new ScriptException(e);
    }
}
Also used : CustomSearchParameterBean(org.wso2.carbon.registry.search.beans.CustomSearchParameterBean) ResourceData(org.wso2.carbon.registry.common.ResourceData) CarbonException(org.wso2.carbon.CarbonException) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) RegistryException(org.wso2.carbon.registry.api.RegistryException) AdvancedSearchResultsBean(org.wso2.carbon.registry.search.beans.AdvancedSearchResultsBean) ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) TagCount(org.wso2.carbon.registry.common.TagCount) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat)

Example 8 with Tag

use of org.wso2.carbon.apimgt.core.models.Tag in project carbon-apimgt by wso2.

the class TagMappingUtil method fromTagToDTO.

/**
 * Converts a Tag object into TagDTO
 *
 * @param tag Tag object
 * @return TagDTO corresponds to Tag object
 */
public static TagDTO fromTagToDTO(Tag tag) {
    TagDTO tagDTO = new TagDTO();
    tagDTO.setName(tag.getName());
    tagDTO.setWeight(tag.getCount());
    return tagDTO;
}
Also used : TagDTO(org.wso2.carbon.apimgt.rest.api.store.dto.TagDTO)

Example 9 with Tag

use of org.wso2.carbon.apimgt.core.models.Tag in project carbon-apimgt by wso2.

the class TagsApiServiceImplTestCase method testTagsGet.

@Test
public void testTagsGet() throws APIManagementException, NotFoundException {
    TestUtil.printTestMethodName();
    TagsApiServiceImpl tagsApiService = new TagsApiServiceImpl();
    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);
    Tag tag1 = new Tag.Builder().build();
    Tag tag2 = new Tag.Builder().build();
    List<Tag> tagList = new ArrayList<>();
    tagList.add(tag1);
    tagList.add(tag2);
    Mockito.when(apiStore.getAllTags()).thenReturn(tagList);
    Response response = tagsApiService.tagsGet(10, 0, null, request);
    Assert.assertEquals(200, response.getStatus());
}
Also used : Response(javax.ws.rs.core.Response) Request(org.wso2.msf4j.Request) ArrayList(java.util.ArrayList) Tag(org.wso2.carbon.apimgt.core.models.Tag) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 10 with Tag

use of org.wso2.carbon.apimgt.core.models.Tag in project carbon-apimgt by wso2.

the class ApiDAOImplIT method addAPIWithGivenData.

/**
 * This method adds an API with given information
 *
 * @param apiName                API name
 * @param apiVersion             API version
 * @param apiContext             API context
 * @param apiProvider            API provider
 * @param apiVisibility          API visibility
 * @param visibleRoles           roles that are eligible to consume the API
 * @param initialLifecycleStatus initial lifecycle status
 * @param description            API description
 * @param tags                   tag list for the API
 * @param uriTemplates           URI templates, i.e - resources
 * @param finalLifecycleStatus   final lifecycle status
 * @throws APIMgtDAOException if it fails to add the API
 */
private void addAPIWithGivenData(String apiName, String apiVersion, String apiContext, String apiProvider, API.Visibility apiVisibility, Set<String> visibleRoles, String initialLifecycleStatus, String description, Set<String> tags, Map<String, UriTemplate> uriTemplates, String finalLifecycleStatus) throws APIMgtDAOException {
    API.APIBuilder builder;
    ApiDAO apiDAO = DAOFactory.getApiDAO();
    builder = SampleTestObjectCreator.createCustomAPI(apiName, apiVersion, apiContext);
    builder.provider(apiProvider);
    builder.createdBy(apiProvider);
    builder.visibility(apiVisibility);
    // visible roles should be added for restricted APIs
    if (apiVisibility != null && API.Visibility.RESTRICTED.toString().equalsIgnoreCase(apiVisibility.toString())) {
        builder.visibleRoles(visibleRoles);
    }
    builder.lifeCycleStatus(initialLifecycleStatus);
    builder.description(description);
    builder.tags(tags);
    builder.uriTemplates(uriTemplates);
    builder.endpoint(Collections.emptyMap());
    API api = builder.build();
    apiDAO.addAPI(api);
    apiDAO.changeLifeCycleStatus(api.getId(), finalLifecycleStatus);
}
Also used : CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO)

Aggregations

Tag (org.wso2.carbon.apimgt.core.models.Tag)7 ArrayList (java.util.ArrayList)4 Test (org.testng.annotations.Test)3 TagDTO (org.wso2.carbon.apimgt.rest.api.store.dto.TagDTO)3 TagListDTO (org.wso2.carbon.apimgt.rest.api.store.dto.TagListDTO)3 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 SQLException (java.sql.SQLException)2 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)2 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)2 API (org.wso2.carbon.apimgt.core.models.API)2 IOException (java.io.IOException)1 ResultSet (java.sql.ResultSet)1 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Response (javax.ws.rs.core.Response)1 ScriptException (org.jaggeryjs.scriptengine.exceptions.ScriptException)1 Test (org.junit.Test)1