<commit_before>/*
*  Copyright (c) 2019, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
*  WSO2 Inc. licenses this file to you under the Apache License,
*  Version 2.0 (the "License"); you may not use this file except
*  in compliance with the License.
*  You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.wso2.am.integration.tests.restapi;

import org.apache.axiom.util.base64.Base64Utils;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.json.JSONException;
import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Factory;
import org.testng.annotations.Test;
import org.wso2.am.integration.test.utils.APIManagerIntegrationTestException;
import org.wso2.am.integration.test.utils.base.APIMIntegrationBaseTest;
import org.wso2.am.integration.test.utils.base.APIMIntegrationConstants;
import org.wso2.am.integration.test.utils.bean.APILifeCycleState;
import org.wso2.am.integration.test.utils.bean.APILifeCycleStateRequest;
import org.wso2.am.integration.test.utils.bean.APIRequest;
import org.wso2.am.integration.test.utils.http.HTTPSClientUtils;
import org.wso2.carbon.apimgt.api.model.APIIdentifier;
import org.wso2.carbon.automation.engine.context.TestUserMode;
import org.wso2.carbon.automation.engine.exceptions.AutomationFrameworkException;
import org.wso2.carbon.automation.test.utils.http.client.HttpRequestUtil;
import org.wso2.carbon.utils.multitenancy.MultitenantUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;

public class ContentSearchTestCase extends APIMIntegrationBaseTest {
    private Log log  = LogFactory.getLog(ContentSearchTestCase.class);
    private String storeRestAPIBasePath = "api/am/store/v0.14/";
    private String publisherRestAPIBasePath = "api/am/publisher/v0.14/";
    private URL tokenApiUrl;
    private String endpointURL = "http://gdata.youtube.com/feeds/api/standardfeeds";
    private String version = "1.0.0";
    private int retries = 10; //because indexing needs time, we are retrying api calls at an interval of 3s

    @Factory(dataProvider = "userModeDataProvider") public ContentSearchTestCase(TestUserMode userMode) {
        this.userMode = userMode;
    }

    @DataProvider public static Object[][] userModeDataProvider() {
        return new Object[][] { new Object[] { TestUserMode.SUPER_TENANT_ADMIN },
                new Object[] { TestUserMode.TENANT_ADMIN }, };
    }

    @BeforeClass(alwaysRun = true) public void setEnvironment() throws Exception {
        super.init(userMode);
        tokenApiUrl = new URL(getKeyManagerURLHttps() + "oauth2/token");
    }

    @Test(groups = { "wso2.am" }, description = "Test basic content Search") public void testBasicContentSearch()
            throws Exception {
        log.info("Basic Content Search");
        String contentSearchTestAPI = "contentSearchTestAPI";
        String description = "UnifiedSearchFeature";
        APIRequest apiRequest = createAPIRequest(contentSearchTestAPI, contentSearchTestAPI, endpointURL, version,
                user.getUserName(), description);
        apiPublisher.login(user.getUserName(), user.getPassword());
        apiPublisher.addAPI(apiRequest);
        APIIdentifier apiIdentifier = new APIIdentifier(user.getUserName(), contentSearchTestAPI, version);
        apiPublisher.changeAPILifeCycleStatusToPublish(apiIdentifier, false);

        HttpClient client = HTTPSClientUtils.getHttpsClient();
        String accessToken = getAccessToken("publisher_client", user.getUserName(), user.getPassword());

        //check in publisher
        for (int i = 0; i <= retries; i++) {
            HttpGet getPublisherAPIs = new HttpGet(
                    getStoreURLHttps() + publisherRestAPIBasePath + "search?query=" + description);
            getPublisherAPIs.setHeader("Authorization", "Bearer " + accessToken);
            HttpResponse publisherResponse = client.execute(getPublisherAPIs);
            if (getResultCount(publisherResponse) == 1) {
                Assert.assertTrue(true);
                break;
            } else {
                if (i == retries) {
                    Assert.fail("Basic content search in publisher failed. Received response : " + publisherResponse);
                } else {
                    log.warn("Basic content search in publisher failed. Received response : " + publisherResponse + " Retrying...");
                    Thread.sleep(3000);
                }
            }
        }

        for (int i = 0; i <= retries; i++) {
            HttpGet getStoreAPIs = new HttpGet(getStoreURLHttps() + storeRestAPIBasePath + "search?query=" + description);
            if (TestUserMode.TENANT_ADMIN == userMode) {
                getStoreAPIs.setHeader("X-WSO2-Tenant", user.getUserDomain());
            }

            //search term : UnifiedSearchFeature, created api has this in description filed
            getStoreAPIs.setHeader("Authorization", "Bearer " + accessToken);
            HttpResponse storeResponse = client.execute(getStoreAPIs);
            if (getResultCount(storeResponse) == 1) {
                Assert.assertTrue(true);
                break;
            } else {
                if (i == retries) {
                    Assert.fail("Basic content search in store failed. Received response : " + storeResponse);
                } else {
                    log.warn("Basic content search in store failed. Received response : " + storeResponse + " Retrying...");
                    Thread.sleep(3000);
                }
            }
        }

        //change status to create and check whether it is accessible from store
        APILifeCycleStateRequest updateRequest = new APILifeCycleStateRequest(contentSearchTestAPI, user.getUserName(),
                APILifeCycleState.CREATED);
        apiPublisher.changeAPILifeCycleStatus(updateRequest);
        for (int i = 0; i <= retries; i++) {
            HttpGet getStoreAPIs = new HttpGet(getStoreURLHttps() + storeRestAPIBasePath + "search?query=" + description);
            if (TestUserMode.TENANT_ADMIN == userMode) {
                getStoreAPIs.setHeader("X-WSO2-Tenant", user.getUserDomain());
            }<commit_msg>Modify basicContentSearchTestCase to check multiple keyword search<commit_after>/*
*  Copyright (c) 2019, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
*  WSO2 Inc. licenses this file to you under the Apache License,
*  Version 2.0 (the "License"); you may not use this file except
*  in compliance with the License.
*  You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.wso2.am.integration.tests.restapi;

import org.apache.axiom.util.base64.Base64Utils;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.json.JSONException;
import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Factory;
import org.testng.annotations.Test;
import org.wso2.am.integration.test.utils.APIManagerIntegrationTestException;
import org.wso2.am.integration.test.utils.base.APIMIntegrationBaseTest;
import org.wso2.am.integration.test.utils.base.APIMIntegrationConstants;
import org.wso2.am.integration.test.utils.bean.APILifeCycleState;
import org.wso2.am.integration.test.utils.bean.APILifeCycleStateRequest;
import org.wso2.am.integration.test.utils.bean.APIRequest;
import org.wso2.am.integration.test.utils.http.HTTPSClientUtils;
import org.wso2.carbon.apimgt.api.model.APIIdentifier;
import org.wso2.carbon.automation.engine.context.TestUserMode;
import org.wso2.carbon.automation.engine.exceptions.AutomationFrameworkException;
import org.wso2.carbon.automation.test.utils.http.client.HttpRequestUtil;
import org.wso2.carbon.utils.multitenancy.MultitenantUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;

public class ContentSearchTestCase extends APIMIntegrationBaseTest {
    private Log log  = LogFactory.getLog(ContentSearchTestCase.class);
    private String storeRestAPIBasePath = "api/am/store/v0.14/";
    private String publisherRestAPIBasePath = "api/am/publisher/v0.14/";
    private URL tokenApiUrl;
    private String endpointURL = "http://gdata.youtube.com/feeds/api/standardfeeds";
    private String version = "1.0.0";
    private int retries = 10; //because indexing needs time, we are retrying api calls at an interval of 3s

    @Factory(dataProvider = "userModeDataProvider") public ContentSearchTestCase(TestUserMode userMode) {
        this.userMode = userMode;
    }

    @DataProvider public static Object[][] userModeDataProvider() {
        return new Object[][] { new Object[] { TestUserMode.SUPER_TENANT_ADMIN },
                new Object[] { TestUserMode.TENANT_ADMIN }, };
    }

    @BeforeClass(alwaysRun = true) public void setEnvironment() throws Exception {
        super.init(userMode);
        tokenApiUrl = new URL(getKeyManagerURLHttps() + "oauth2/token");
    }

    @Test(groups = { "wso2.am" }, description = "Test basic content Search") public void testBasicContentSearch()
            throws Exception {
        log.info("Basic Content Search");
        String contentSearchTestAPI = "contentSearchTestAPI";
        String description = "UnifiedSearchFeature";
        APIRequest apiRequest = createAPIRequest(contentSearchTestAPI, contentSearchTestAPI, endpointURL, version,
                user.getUserName(), description);
        apiPublisher.login(user.getUserName(), user.getPassword());
        apiPublisher.addAPI(apiRequest);
        APIIdentifier apiIdentifier = new APIIdentifier(user.getUserName(), contentSearchTestAPI, version);
        apiPublisher.changeAPILifeCycleStatusToPublish(apiIdentifier, false);

        HttpClient client = HTTPSClientUtils.getHttpsClient();
        String accessToken = getAccessToken("publisher_client", user.getUserName(), user.getPassword());

        //check in publisher
        for (int i = 0; i <= retries; i++) {
            HttpGet getPublisherAPIs = new HttpGet(
                    getPublisherURLHttps() + publisherRestAPIBasePath + "search?query=" + URLEncoder
                            .encode(description, "UTF-8"));
            getPublisherAPIs.setHeader("Authorization", "Bearer " + accessToken);
            HttpResponse publisherResponse = client.execute(getPublisherAPIs);
            if (getResultCount(publisherResponse) == 1) {
                Assert.assertTrue(true);
                break;
            } else {
                if (i == retries) {
                    Assert.fail("Basic content search in publisher failed. Received response : " + publisherResponse);
                } else {
                    log.warn("Basic content search in publisher failed. Received response : " + publisherResponse + " Retrying...");
                    Thread.sleep(3000);
                }
            }
        }

        for (int i = 0; i <= retries; i++) {
            HttpGet getStoreAPIs = new HttpGet(getStoreURLHttps() + storeRestAPIBasePath + "search?query=" + URLEncoder
                    .encode(description, "UTF-8"));
            if (TestUserMode.TENANT_ADMIN == userMode) {
                getStoreAPIs.setHeader("X-WSO2-Tenant", user.getUserDomain());
            }

            //search term : UnifiedSearchFeature, created api has this in description filed
            getStoreAPIs.setHeader("Authorization", "Bearer " + accessToken);
            HttpResponse storeResponse = client.execute(getStoreAPIs);
            if (getResultCount(storeResponse) == 1) {
                Assert.assertTrue(true);
                break;
            } else {
                if (i == retries) {
                    Assert.fail("Basic content search in store failed. Received response : " + storeResponse);
                } else {
                    log.warn("Basic content search in store failed. Received response : " + storeResponse + " Retrying...");
                    Thread.sleep(3000);
                }
            }
        }

        //change status to create and check whether it is accessible from store
        APILifeCycleStateRequest updateRequest = new APILifeCycleStateRequest(contentSearchTestAPI, user.getUserName(),
                APILifeCycleState.CREATED);
        apiPublisher.changeAPILifeCycleStatus(updateRequest);
        for (int i = 0; i <= retries; i++) {
            HttpGet getStoreAPIs = new HttpGet(getStoreURLHttps() + storeRestAPIBasePath + "search?query=" + URLEncoder
                    .encode(description, "UTF-8"));
            if (TestUserMode.TENANT_ADMIN == userMode) {
                getStoreAPIs.setHeader("X-WSO2-Tenant", user.getUserDomain());
            }
{'fraction_non_alphanumeric': 0.10104675505931612, 'fraction_numerical': 0.008443824145150035, 'mean_word_length': 3.1121951219512196, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2660355', 'n_tokens_mistral': 4092, 'n_tokens_neox': 3955, 'n_words': 910}