<commit_before>package network;

import org.junit.Before;
import org.junit.Test;

import java.util.Arrays;
import java.util.Collections;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;

public class RouteTableTest {

    private RouteTable table;
    private Node defaultNeighbour;

    @Before
    public void setUp() throws Exception {
        defaultNeighbour = new Node(null, 0, null);
        table = new RouteTable(Collections.singletonList(defaultNeighbour), ComponentFactory.createAttributeFactory());
    }

    @Test
    public void setAttributeForNonExistingDestination_AttributeWasSetToPairDestinationNeighbour() throws Exception {
        Node destination = new Node(null, 1, null); // any destination node

        Attribute expectedAttribute = ComponentFactory.createAttribute(0);
        table.setAttribute(destination, defaultNeighbour, ComponentFactory.createAttribute(0));

        assertThat(table.getAttribute(destination, defaultNeighbour), equalTo(expectedAttribute));
    }

    @Test
    public void setAttributeForExistingDestination_AttributeChanged() throws Exception {
        Node destination = new Node(null, 1, null); // any destination node
        table.setAttribute(destination, defaultNeighbour, ComponentFactory.createAttribute(0));

        Attribute expectedAttribute = ComponentFactory.createAttribute(1);
        table.setAttribute(destination, defaultNeighbour, ComponentFactory.createAttribute(1));

        assertThat(table.getAttribute(destination, defaultNeighbour), equalTo(expectedAttribute));
    }

    @Test
    public void setAttributeForNonExistingNeighbour_GetAttributeReturnsNull() throws Exception {
        Node destination = new Node(null, 1, null); // any destination node
        Node nonExistingNeighbour = new Node(null, 1, null);

        table.setAttribute(destination, nonExistingNeighbour, ComponentFactory.createAttribute(0));

        assertThat(table.getAttribute(destination, defaultNeighbour), is(nullValue()));
    }

    @Test
    public void setPathForNonExistingDestination_PathWasSetToPairDestinationNeighbour() throws Exception {
        Node destination = new Node(null, 1, null); // any destination node

        PathAttribute expectedPath = new PathAttribute();
        table.setPath(destination, defaultNeighbour, new PathAttribute());

        assertThat(table.getPath(destination, defaultNeighbour), equalTo(expectedPath));
    }

    @Test
    public void setPathForExistingDestination_PathChanged() throws Exception {
        Node destination = new Node(null, 1, null); // any destination node
        table.setPath(destination, defaultNeighbour, new PathAttribute());

        PathAttribute expectedPath = new PathAttribute(new Node(null, 2, null)); // expect different path
        table.setPath(destination, defaultNeighbour, new PathAttribute(new Node(null, 2, null)));

        assertThat(table.getPath(destination, defaultNeighbour), equalTo(expectedPath));
    }

    @Test
    public void setPathForNonExistingNeighbour_GetPathReturnsNull() throws Exception {
        Node destination = new Node(null, 1, null); // any destination node
        Node nonExistingNeighbour = new Node(null, 1, null);

        table.setPath(destination, nonExistingNeighbour, new PathAttribute(destination));

        assertThat(table.getPath(destination, defaultNeighbour), is(nullValue()));
    }

    @Test
    public void getAttributeNotSetForExistingDestination_ReturnsInvalidAttribute() throws Exception {
        defaultNeighbour = new Node(null, 0, null);
        Node otherNeighbour = new Node(null, 1, null);  // neighbour for which the attribute was not yet set
        Node[] neighbours = {defaultNeighbour, otherNeighbour};
        table = new RouteTable(Arrays.asList(neighbours), ComponentFactory.createAttributeFactory());

        // add the destination
        Node destination = new Node(null, 2, null); // any destination node
        table.setAttribute(destination, defaultNeighbour, ComponentFactory.createAttribute(0));

        // expect invalid attribute
        Attribute expectedAttribute = ComponentFactory.createAttributeFactory().createInvalid();

        assertThat(table.getAttribute(destination, otherNeighbour), equalTo(expectedAttribute));
    }

    @Test
    public void getPathNotSetForExistingDestination_ReturnsInvalidPath() throws Exception {
        defaultNeighbour = new Node(null, 0, null);
        Node otherNeighbour = new Node(null, 1, null);  // neighbour for which the attribute was not yet set
        Node[] neighbours = {defaultNeighbour, otherNeighbour};
        table = new RouteTable(Arrays.asList(neighbours), ComponentFactory.createAttributeFactory());

        // add the destination
        Node destination = new Node(null, 2, null); // any destination node
        table.setPath(destination, defaultNeighbour, new PathAttribute(destination));

        // expect invalid attribute
        PathAttribute expectedPath = PathAttribute.createInvalid();

        assertThat(table.getPath(destination, otherNeighbour), equalTo(expectedPath));
    }

    // TODO clear method

}<commit_msg>removed tests for setPath and getPath since they are now repeated refactored the tests<commit_after>package network;

import org.junit.Test;

import java.util.Arrays;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertThat;

public class RouteTableTest {

    /**
     * Creates a route table. The neighbours can be added as normal parameters or by an array.
     * @param neighbours neighbours to assign to the route table.
     * @return route table initialized.
     */
    private RouteTable createRouteTable(Node... neighbours) {
        return new RouteTable(Arrays.asList(neighbours), ComponentFactory.createAttributeFactory());
    }

    @Test
    public void setAttributeForNonExistingDestination_AttributeWasSetToPairDestinationNeighbour() throws Exception {
        Node existingNeighbour = new Node(null, 0, null);
        Node destination = new Node(null, 1, null); // any destination node
        RouteTable table = createRouteTable(existingNeighbour);

        Attribute expectedAttribute = ComponentFactory.createAttribute(0);
        table.setAttribute(destination, existingNeighbour, ComponentFactory.createAttribute(0));

        assertThat(table.getAttribute(destination, existingNeighbour), equalTo(expectedAttribute));
    }

    @Test
    public void setAttributeForExistingDestination_AttributeChanged() throws Exception {
        Node existingNeighbour = new Node(null, 0, null);
        Node destination = new Node(null, 1, null);
        RouteTable table = createRouteTable(existingNeighbour);
        table.setAttribute(destination, existingNeighbour, ComponentFactory.createAttribute(0));    // add destination

        Attribute expectedAttribute = ComponentFactory.createAttribute(1);
        table.setAttribute(destination, existingNeighbour, ComponentFactory.createAttribute(1));

        assertThat(table.getAttribute(destination, existingNeighbour), equalTo(expectedAttribute));
    }

    @Test
    public void setAttributeForNonExistingNeighbour_GetAttributeReturnsNull() throws Exception {
        Node existingNeighbour = new Node(null, 0, null);
        Node destination = new Node(null, 1, null);
        RouteTable table = createRouteTable(existingNeighbour);
        Node nonExistingNeighbour = new Node(null, 1, null);

        table.setAttribute(destination, nonExistingNeighbour, ComponentFactory.createAttribute(0));

        assertThat(table.getAttribute(destination, existingNeighbour), is(nullValue()));
    }

    @Test
    public void getAttributeNotSetForExistingDestination_ReturnsInvalidAttribute() throws Exception {
        Node existingNeighbour = new Node(null, 0, null);
        Node notSetNeighbour = new Node(null, 1, null);  // neighbour for which the attribute was not yet set
        RouteTable table = createRouteTable(existingNeighbour, notSetNeighbour);
        Node destination = new Node(null, 2, null);
        table.setAttribute(destination, existingNeighbour, ComponentFactory.createAttribute(0));

        // expect invalid attribute
        Attribute expectedAttribute = ComponentFactory.createAttributeFactory().createInvalid();

        assertThat(table.getAttribute(destination, notSetNeighbour), equalTo(expectedAttribute));
    }

    // TODO clear method

}
{'fraction_non_alphanumeric': 0.08508615567439097, 'fraction_numerical': 0.004872251931075461, 'mean_word_length': 4.10369921164342, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1058198', 'n_tokens_mistral': 2092, 'n_tokens_neox': 2059, 'n_words': 549}