<commit_before><commit_msg>Default server side size limitation works incorrectly (#282)<commit_after>package com.nhl.link.rest.it;

import com.nhl.link.rest.DataResponse;
import com.nhl.link.rest.LinkRest;
import com.nhl.link.rest.it.fixture.JerseyTestOnDerby;
import com.nhl.link.rest.it.fixture.cayenne.E4;
import org.apache.cayenne.query.SQLTemplate;
import org.junit.Test;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Configuration;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.FeatureContext;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

import static org.junit.Assert.assertEquals;

public class GET_SizeConstraintsIT extends JerseyTestOnDerby {

    @Override
    protected void doAddResources(FeatureContext context) {
        context.register(Resource.class);
    }

    // TODO: unclear what server-side fetch offset protects? so not testing it here.

    @Test
    public void testNoClientLimit() {

        SQLTemplate insert = new SQLTemplate(E4.class, "INSERT INTO utest.e4 (id) values (1),(2),(3)");
        newContext().performGenericQuery(insert);

        Response r = target("/e4/limit")
                .queryParam("sort", "id")
                .queryParam("include", "id")
                .request()
                .get();
        assertEquals(Response.Status.OK.getStatusCode(), r.getStatus());
        assertEquals("{\"data\":[{\"id\":1},{\"id\":2,\"cInt\":6}],\"total\":2}",
                r.readEntity(String.class));
    }

    @Test
    public void testClientLimitExceedsServer() {

        SQLTemplate insert = new SQLTemplate(E4.class, "INSERT INTO utest.e4 (id) values (1),(2),(3)");

        newContext().performGenericQuery(insert);

        Response r = target("/e4/limit")
                .queryParam("sort", "id")
                .queryParam("include", "id")
                .queryParam("limit", "5")
                .request()
                .get();
        assertEquals(Response.Status.OK.getStatusCode(), r.getStatus());
        assertEquals("{\"data\":[{\"id\":1},{\"id\":2}],\"total\":2}",
                r.readEntity(String.class));
    }

    @Path("")
    public static class Resource {

        @Context
        private Configuration config;

        @GET
        @Path("e4/limit")
        public DataResponse<E4> limit(@Context UriInfo uriInfo) {
            return LinkRest.select(E4.class, config).uri(uriInfo)
                    .fetchLimit(2)
                    .get();
        }
    }
}

{'fraction_non_alphanumeric': 0.15077782209812524, 'fraction_numerical': 0.011168727562824093, 'mean_word_length': 2.5274261603375527, 'pattern_counts': {'":': 9, '<': 4, '<?xml version=': 0, '>': 4, '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': '3957911', 'n_tokens_mistral': 776, 'n_tokens_neox': 735, 'n_words': 140}