<commit_before>/*
 * Copyright (c) 2015. Escalon System-Entwicklung, <NAME>
 *
 * Licensed 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 de.escalon.hypermedia.spring.uber;

import com.fasterxml.jackson.databind.ObjectMapper;
import de.escalon.hypermedia.spring.SpringActionDescriptor;
import de.escalon.hypermedia.spring.SpringActionInputParameter;
import org.junit.Test;
import org.springframework.core.MethodParameter;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.Resource;
import org.springframework.hateoas.ResourceSupport;
import org.springframework.hateoas.Resources;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.*;

import static org.junit.Assert.*;

public class UberUtilsTest {

    private static final String URL_HOME = "http://www.example.com";
    private static final Link LINK_HOME = new Link(URL_HOME, "home");
    private static final String FOO_VALUE = "foo";
    private static final String BAR_VALUE = "bar";
    private static final String NESTED_FOO_VALUE = "nestedFooValue";
    private static final String NESTED_BAR_VALUE = "nestedBarValue";


    @Test
    public void linkGetToUberNode() throws Exception {
        UberNode linkNode = UberUtils.toUberLink("/foo", new SpringActionDescriptor("get", RequestMethod.GET.name()),
                Link.REL_SELF);
        assertEquals(Arrays.asList(Link.REL_SELF), linkNode.getRel());
        assertEquals("/foo", linkNode.getUrl());
        assertNull(linkNode.getModel());
        assertNull(linkNode.getAction());
    }

    public static class FooRequestBody {
        String bar;
        int foo;

        public void setBar(String bar) {
            this.bar = bar;
        }

        public String getBar() {
            return bar;
        }

        public int getFoo() {
            return foo;
        }

        public void setFoo(int foo) {
            this.foo = foo;
        }
    }

    public void requestBody(FooRequestBody requestBody) {

    }

    @Test
    public void linkPostToUberNode() throws Exception {
        // TODO create a Link with variables separate from URITemplate for POST
        SpringActionDescriptor actionDescriptor = new SpringActionDescriptor("post", RequestMethod
                .POST.name());
        actionDescriptor.setRequestBody(
                new SpringActionInputParameter(new MethodParameter(this.getClass()
                .getMethod("requestBody", FooRequestBody.class), 0), null));
        UberNode linkNode = UberUtils.toUberLink("/foo", actionDescriptor, Link.REL_SELF);
        assertEquals(Arrays.asList(Link.REL_SELF), linkNode.getRel());
        assertEquals("/foo", linkNode.getUrl());
        assertEquals("bar={bar}&foo={foo}", linkNode.getModel());
        assertEquals(UberAction.APPEND, linkNode.getAction());
    }

    class NestedBean {

        String foo = FOO_VALUE;
        Bean bean = new Bean(NESTED_FOO_VALUE, NESTED_BAR_VALUE);

        public NestedBean() {

        }

        public NestedBean(String foo, Bean bar) {
            this.foo = foo;
            this.bean = bar;
        }

        public String getFoo() {
            return foo;
        }

        public Bean getBean() {<commit_msg>Uber model test now accepts parameters in any order<commit_after>/*
 * Copyright (c) 2015. Escalon System-Entwicklung, <NAME>
 *
 * Licensed 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 de.escalon.hypermedia.spring.uber;

import com.fasterxml.jackson.databind.ObjectMapper;
import de.escalon.hypermedia.spring.SpringActionDescriptor;
import de.escalon.hypermedia.spring.SpringActionInputParameter;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.springframework.core.MethodParameter;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.Resource;
import org.springframework.hateoas.ResourceSupport;
import org.springframework.hateoas.Resources;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.*;

import static org.junit.Assert.*;

public class UberUtilsTest {

    private static final String URL_HOME = "http://www.example.com";
    private static final Link LINK_HOME = new Link(URL_HOME, "home");
    private static final String FOO_VALUE = "foo";
    private static final String BAR_VALUE = "bar";
    private static final String NESTED_FOO_VALUE = "nestedFooValue";
    private static final String NESTED_BAR_VALUE = "nestedBarValue";


    @Test
    public void linkGetToUberNode() throws Exception {
        UberNode linkNode = UberUtils.toUberLink("/foo", new SpringActionDescriptor("get", RequestMethod.GET.name()),
                Link.REL_SELF);
        assertEquals(Arrays.asList(Link.REL_SELF), linkNode.getRel());
        assertEquals("/foo", linkNode.getUrl());
        assertNull(linkNode.getModel());
        assertNull(linkNode.getAction());
    }

    public static class FooRequestBody {
        String bar;
        int foo;

        public void setBar(String bar) {
            this.bar = bar;
        }

        public String getBar() {
            return bar;
        }

        public int getFoo() {
            return foo;
        }

        public void setFoo(int foo) {
            this.foo = foo;
        }
    }

    public void requestBody(FooRequestBody requestBody) {

    }

    @Test
    public void linkPostToUberNode() throws Exception {
        // TODO create a Link with variables separate from URITemplate for POST
        SpringActionDescriptor actionDescriptor = new SpringActionDescriptor("post", RequestMethod
                .POST.name());
        actionDescriptor.setRequestBody(
                new SpringActionInputParameter(new MethodParameter(this.getClass()
                        .getMethod("requestBody", FooRequestBody.class), 0), null));
        UberNode linkNode = UberUtils.toUberLink("/foo", actionDescriptor, Link.REL_SELF);
        assertEquals(Arrays.asList(Link.REL_SELF), linkNode.getRel());
        assertEquals("/foo", linkNode.getUrl());
        assertEquals("bar={bar}&foo={foo}", linkNode.getModel());
        assertThat(linkNode.getModel(),
                Matchers.containsString("foo={foo}"));
        assertThat(linkNode.getModel(),
                Matchers.containsString("bar={bar}"));
        assertEquals(UberAction.APPEND, linkNode.getAction());
    }

    class NestedBean {

        String foo = FOO_VALUE;
        Bean bean = new Bean(NESTED_FOO_VALUE, NESTED_BAR_VALUE);

        public NestedBean() {

        }

        public NestedBean(String foo, Bean bar) {
            this.foo = foo;
            this.bean = bar;
        }

        public String getFoo() {
            return foo;
        }

        public Bean getBean() {
{'fraction_non_alphanumeric': 0.09625876851130163, 'fraction_numerical': 0.002338269680436477, 'mean_word_length': 2.9583547557840615, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 2}, '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': '6747753', 'n_tokens_mistral': 2137, 'n_tokens_neox': 2042, 'n_words': 572}