<commit_before>package org.jboss.aerogear.test.api.installation;

import com.jayway.restassured.path.json.JsonPath;
import com.jayway.restassured.response.Response;
import org.apache.http.HttpStatus;
import org.jboss.aerogear.test.Headers;
import org.jboss.aerogear.test.UnexpectedResponseException;
import org.jboss.aerogear.test.api.AbstractUPSWorker;
import org.jboss.aerogear.unifiedpush.api.Variant;
import org.jboss.aerogear.unifiedpush.api.Installation;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import java.net.URI;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

public abstract class InstallationWorker<
        BLUEPRINT extends InstallationBlueprint<BLUEPRINT, EDITOR, PARENT, WORKER, CONTEXT>,
        EDITOR extends InstallationEditor<BLUEPRINT, EDITOR, PARENT, WORKER, CONTEXT>,
        PARENT extends Variant,
        CONTEXT extends InstallationContext<BLUEPRINT, EDITOR, PARENT, WORKER, CONTEXT>,
        WORKER extends InstallationWorker<BLUEPRINT, EDITOR, PARENT, CONTEXT, WORKER>>

        extends AbstractUPSWorker<Installation, String, BLUEPRINT, EDITOR, PARENT, CONTEXT, WORKER> {

    @Override
    public JSONObject marshall(Installation entity) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("deviceToken", entity.getDeviceToken());
        jsonObject.put("deviceType", entity.getDeviceType());
        jsonObject.put("operatingSystem", entity.getOperatingSystem());
        jsonObject.put("osVersion", entity.getOsVersion());
        jsonObject.put("alias", entity.getAlias());

        if (entity.getCategories() != null) {
            // JSONObject doesn't understand Set<String>
            JSONArray categories = new JSONArray();
            for (String category : entity.getCategories()) {
                categories.add(category);
            }
            jsonObject.put("categories", categories);
        }
        return jsonObject;
    }

    @Override
    public EDITOR demarshall(CONTEXT context, JsonPath jsonPath) {
        EDITOR editor = context.createEditor();
        editor.setId(jsonPath.getString("id"));
        editor.setPlatform(jsonPath.getString("platform"));
        editor.setEnabled(jsonPath.getBoolean("enabled"));
        editor.setOperatingSystem(jsonPath.getString("operatingSystem"));
        editor.setOsVersion(jsonPath.getString("osVersion"));
        editor.setAlias(jsonPath.getString("alias"));
        editor.setDeviceType(jsonPath.getString("deviceType"));
        editor.setDeviceToken(jsonPath.getString("deviceToken"));
        HashSet<String> categories = new HashSet<String>();
        List<String> jsonCategories = jsonPath.getList("categories");
        if (jsonCategories != null) {
            for (String jsonCategory : jsonCategories) {
                categories.add(jsonCategory);
            }
        }
        editor.setCategories(categories);
        return editor;
    }

    @Override
    public List<EDITOR> create(CONTEXT context, Collection<? extends BLUEPRINT> blueprints) {
        List<EDITOR> editors = new ArrayList<EDITOR>();
        for (InstallationBlueprint blueprint : blueprints) {
            Response response = context.getSession().given()
                    .contentType(getContentType())
                    .auth().basic(context.getParent().getVariantID(), context.getParent().getSecret())
                    .header(Headers.acceptJson())
                    .body(marshall(blueprint))
                    .post("/rest/registry/device");

            UnexpectedResponseException.verifyResponse(response, HttpStatus.SC_OK);

            editors.add(demarshall(context, response.jsonPath()));
        }
        return editors;
    }

    public void unregister(CONTEXT context, Collection<? extends Installation> entities) {
        for (Installation entity : entities) {
            String token = entity.getDeviceToken();
            System.out.println("Installation token: " + token);
            token = URLEncoder.encode(token);
            System.out.println("Installation token encoded: " + token);
            token = URLEncoder.encode(token);
            //System.out.println("Installation token double-encoded: " + token);

            Response response = context.getSession().given().log().all()
                    .contentType(getContentType())
                    .auth().basic(context.getParent().getVariantID(), context.getParent().getSecret())
                    .delete(URI.create("/rest/registry/device/" + token));

            UnexpectedResponseException.verifyResponse(response, HttpStatus.SC_NO_CONTENT);
        }
    }

    @Override
    public List<EDITOR> readAll(CONTEXT context) {
        Response response = context.getSession().givenAuthorized()<commit_msg>Remove URL encoding for tokens when unregistering an installation.<commit_after>package org.jboss.aerogear.test.api.installation;

import com.jayway.restassured.path.json.JsonPath;
import com.jayway.restassured.response.Response;
import org.apache.http.HttpStatus;
import org.jboss.aerogear.test.Headers;
import org.jboss.aerogear.test.UnexpectedResponseException;
import org.jboss.aerogear.test.api.AbstractUPSWorker;
import org.jboss.aerogear.unifiedpush.api.Installation;
import org.jboss.aerogear.unifiedpush.api.Variant;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

public abstract class InstallationWorker<
        BLUEPRINT extends InstallationBlueprint<BLUEPRINT, EDITOR, PARENT, WORKER, CONTEXT>,
        EDITOR extends InstallationEditor<BLUEPRINT, EDITOR, PARENT, WORKER, CONTEXT>,
        PARENT extends Variant,
        CONTEXT extends InstallationContext<BLUEPRINT, EDITOR, PARENT, WORKER, CONTEXT>,
        WORKER extends InstallationWorker<BLUEPRINT, EDITOR, PARENT, CONTEXT, WORKER>>

        extends AbstractUPSWorker<Installation, String, BLUEPRINT, EDITOR, PARENT, CONTEXT, WORKER> {

    @Override
    public JSONObject marshall(Installation entity) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("deviceToken", entity.getDeviceToken());
        jsonObject.put("deviceType", entity.getDeviceType());
        jsonObject.put("operatingSystem", entity.getOperatingSystem());
        jsonObject.put("osVersion", entity.getOsVersion());
        jsonObject.put("alias", entity.getAlias());

        if (entity.getCategories() != null) {
            // JSONObject doesn't understand Set<String>
            JSONArray categories = new JSONArray();
            for (String category : entity.getCategories()) {
                categories.add(category);
            }
            jsonObject.put("categories", categories);
        }
        return jsonObject;
    }

    @Override
    public EDITOR demarshall(CONTEXT context, JsonPath jsonPath) {
        EDITOR editor = context.createEditor();
        editor.setId(jsonPath.getString("id"));
        editor.setPlatform(jsonPath.getString("platform"));
        editor.setEnabled(jsonPath.getBoolean("enabled"));
        editor.setOperatingSystem(jsonPath.getString("operatingSystem"));
        editor.setOsVersion(jsonPath.getString("osVersion"));
        editor.setAlias(jsonPath.getString("alias"));
        editor.setDeviceType(jsonPath.getString("deviceType"));
        editor.setDeviceToken(jsonPath.getString("deviceToken"));
        HashSet<String> categories = new HashSet<String>();
        List<String> jsonCategories = jsonPath.getList("categories");
        if (jsonCategories != null) {
            for (String jsonCategory : jsonCategories) {
                categories.add(jsonCategory);
            }
        }
        editor.setCategories(categories);
        return editor;
    }

    @Override
    public List<EDITOR> create(CONTEXT context, Collection<? extends BLUEPRINT> blueprints) {
        List<EDITOR> editors = new ArrayList<EDITOR>();
        for (InstallationBlueprint blueprint : blueprints) {
            Response response = context.getSession().given()
                    .contentType(getContentType())
                    .auth().basic(context.getParent().getVariantID(), context.getParent().getSecret())
                    .header(Headers.acceptJson())
                    .body(marshall(blueprint))
                    .post("/rest/registry/device");

            UnexpectedResponseException.verifyResponse(response, HttpStatus.SC_OK);

            editors.add(demarshall(context, response.jsonPath()));
        }
        return editors;
    }

    public void unregister(CONTEXT context, Collection<? extends Installation> entities) {
        for (Installation entity : entities) {
            Response response = context.getSession().given().log().all()
                    .contentType(getContentType())
                    .auth().basic(context.getParent().getVariantID(), context.getParent().getSecret())
                    .urlEncodingEnabled(false)
                    .delete("/rest/registry/device/{token}", entity.getDeviceToken());

            UnexpectedResponseException.verifyResponse(response, HttpStatus.SC_NO_CONTENT);
        }
    }

    @Override
    public List<EDITOR> readAll(CONTEXT context) {
        Response response = context.getSession().givenAuthorized()
{'fraction_non_alphanumeric': 0.1133976874933701, 'fraction_numerical': 0.0, 'mean_word_length': 3.0796192124621378, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '7595250', 'n_tokens_mistral': 2544, 'n_tokens_neox': 2441, 'n_words': 454}