<commit_before>package com.codingchili.router.controller.transport;

import com.codingchili.router.controller.RouterHandler;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpServerOptions;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.unit.Async;
import io.vertx.ext.unit.TestContext;
import io.vertx.ext.unit.junit.Timeout;
import io.vertx.ext.unit.junit.VertxUnitRunner;
import org.junit.*;
import org.junit.runner.RunWith;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

import com.codingchili.core.protocol.ClusterNode;
import com.codingchili.core.protocol.ResponseStatus;
import com.codingchili.core.security.RemoteIdentity;

import com.codingchili.router.configuration.ListenerSettings;
import com.codingchili.router.configuration.RouterSettings;
import com.codingchili.router.model.Endpoint;
import com.codingchili.router.model.WireType;
import com.codingchili.common.Strings;

import static com.codingchili.common.Strings.*;

/**
 * @author <NAME>
 *
 * Contains test cases for transport implementations.
 */
@Ignore
@RunWith(VertxUnitRunner.class)
public abstract class TransportTestCases {
    static final String PATCHING_ROOT = "/patching";
    private static RouterSettings settings;
    private static ListenerSettings listener;
    static final String HOST = "localhost";
    static final int PORT = 19797;
    Vertx vertx;
    private ContextMock context;
    private WireType wireType;

    TransportTestCases(WireType wireType) {
        this.wireType = wireType;
    }

    @Rule
    public Timeout timeout = new Timeout(15, TimeUnit.SECONDS);

    @Before
    public void setUp(TestContext testContext) {
        Async async = testContext.async();

        Vertx.clusteredVertx(new VertxOptions(), cluster -> {
            vertx = cluster.result();

            settings = new RouterSettings();
            context = new ContextMock(vertx);

            ArrayList<ListenerSettings> transport = new ArrayList<>();

            settings.setIdentity(new RemoteIdentity("node", "at"));

            listener = new ListenerSettings();
            listener.setMaxRequestBytes(10)
                    .setPort(PORT)
                    .setType(wireType)
                    .setTimeout(105000)
                    .setHttpOptions(new HttpServerOptions().setCompressionSupported(false));

            HashMap<String, Endpoint> api = new HashMap<>();
            api.put(PATCHING_ROOT, new Endpoint().setTarget(NODE_PATCHING));
            listener.setApi(api);

            settings.getHidden().add(Strings.NODE_LOGGING);
            transport.add(listener);
            settings.setTransport(transport);
            context.setSettings(settings);

            vertx.deployVerticle(fromWireType(wireType), deploy -> {
                async.complete();
            });
        });
    }

    private ClusterNode fromWireType(WireType type) {
        switch (type) {
            case REST:
                return new RestListener(new RouterHandler<>(context));
            case UDP:
                return new UdpListener(new RouterHandler<>(context));
            default:
                throw new RuntimeException("Transport verticle undefined for given transport type");
        }
    }

    @After
    public void tearDown(TestContext context) {
        vertx.close(context.asyncAssertSuccess());
    }

    @Test
    public void testLargePacketRejected(TestContext context) {
        Async async = context.async();

        mockNode(PATCHING_ROOT);

        sendRequest(PATCHING_ROOT, (result, status) -> {
            context.assertEquals(ResponseStatus.BAD, status);
            async.complete();
        }, new JsonObject().put("data", "request is larger than 10 bytes!!"));
    }
<commit_msg>add transports to predefined handlers in tests<commit_after>package com.codingchili.router.controller.transport;

import com.codingchili.router.configuration.*;
import com.codingchili.router.controller.RouterHandler;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpServerOptions;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.unit.Async;
import io.vertx.ext.unit.TestContext;
import io.vertx.ext.unit.junit.Timeout;
import io.vertx.ext.unit.junit.VertxUnitRunner;
import org.junit.*;
import org.junit.runner.RunWith;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

import com.codingchili.core.protocol.ClusterNode;
import com.codingchili.core.protocol.ResponseStatus;
import com.codingchili.core.security.RemoteIdentity;

import com.codingchili.router.model.Endpoint;
import com.codingchili.router.model.WireType;
import com.codingchili.common.Strings;

import static com.codingchili.common.Strings.*;

/**
 * @author <NAME>
 *
 * Contains test cases for transport implementations.
 */
@Ignore
@RunWith(VertxUnitRunner.class)
public abstract class TransportTestCases {
    static final String PATCHING_ROOT = "/patching";
    private static RouterSettings settings;
    private static ListenerSettings listener;
    static final String HOST = "localhost";
    static final int PORT = 19797;
    Vertx vertx;
    private ContextMock context;
    private WireType wireType;

    TransportTestCases(WireType wireType) {
        this.wireType = wireType;
    }

    @Rule
    public Timeout timeout = new Timeout(15, TimeUnit.SECONDS);

    @Before
    public void setUp(TestContext testContext) {
        Async async = testContext.async();

        Vertx.clusteredVertx(new VertxOptions(), cluster -> {
            vertx = cluster.result();

            settings = new RouterSettings();
            context = new ContextMock(vertx);

            ArrayList<ListenerSettings> transport = new ArrayList<>();

            settings.setIdentity(new RemoteIdentity("node", "at"));

            listener = new ListenerSettings();
            listener.setMaxRequestBytes(10)
                    .setPort(PORT)
                    .setType(wireType)
                    .setTimeout(105000)
                    .setHttpOptions(new HttpServerOptions().setCompressionSupported(false));

            HashMap<String, Endpoint> api = new HashMap<>();
            api.put(PATCHING_ROOT, new Endpoint().setTarget(NODE_PATCHING));
            listener.setApi(api);

            settings.getHidden().add(Strings.NODE_LOGGING);
            transport.add(listener);
            settings.setTransport(transport);
            context.setSettings(settings);

            vertx.deployVerticle(fromWireType(wireType), deploy -> {
                async.complete();
            });
        });
    }

    private ClusterNode fromWireType(WireType type) {
        RouterHandler<RouterContext> handler = new RouterHandler<>(context);
        switch (type) {
            case REST:
                return new RestListener(handler);
            case UDP:
                return new UdpListener(handler);
            case TCP:
                return new TcpListener(handler);
            case WEBSOCKET:
                return new WebsocketListener(handler);
            default:
                throw new RuntimeException("Transport verticle undefined for given transport type");
        }
    }

    @After
    public void tearDown(TestContext context) {
        vertx.close(context.asyncAssertSuccess());
    }

    @Test
    public void testLargePacketRejected(TestContext context) {
        Async async = context.async();

        mockNode(PATCHING_ROOT);

        sendRequest(PATCHING_ROOT, (result, status) -> {
            context.assertEquals(ResponseStatus.BAD, status);
            async.complete();
        }, new JsonObject().put("data", "request is larger than 10 bytes!!"));
    }

{'fraction_non_alphanumeric': 0.10090457383106129, 'fraction_numerical': 0.004331762007899096, 'mean_word_length': 2.8292682926829267, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 23, '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': '6724867', 'n_tokens_mistral': 2205, 'n_tokens_neox': 2091, 'n_words': 446}