Skip to content

Commit

Permalink
Create BeatTable #238 #239 #240 (#291)
Browse files Browse the repository at this point in the history
* Create BeatTable #238 #239 #240

* CompareBeats #249 #250

* Add unimplemeneted test

* Traits pattern
  • Loading branch information
valentinbujdoso authored and magwas committed Apr 11, 2019
1 parent dfc50dc commit 35e5aef
Show file tree
Hide file tree
Showing 10 changed files with 318 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/main/java/org/rulez/demokracia/pdengine/BeatTable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.rulez.demokracia.pdengine;

import java.util.ArrayList;
import java.util.Collection;

import org.rulez.demokracia.pdengine.dataobjects.Pair;
import org.rulez.demokracia.types.MapMatrix;

public class BeatTable extends MapMatrix<Choice, Pair> implements ContainingBeats{
private static final long serialVersionUID = 1L;

public enum Direction {
DIRECTION_FORWARD, DIRECTION_BACKWARD
}

public BeatTable() {
this(new ArrayList<Choice>());
}

public BeatTable(final Collection<Choice> keyCollection) {
super(keyCollection);
}
}
50 changes: 50 additions & 0 deletions src/main/java/org/rulez/demokracia/pdengine/ContainingBeats.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.rulez.demokracia.pdengine;

import org.rulez.demokracia.pdengine.BeatTable.Direction;
import org.rulez.demokracia.pdengine.dataobjects.Pair;
import org.rulez.demokracia.pdengine.exception.ReportedException;
import org.rulez.demokracia.types.Matrix;

public interface ContainingBeats extends Matrix<Choice, Pair>{

long serialVersionUID = 1L;
default void checkPair(final Pair pair) {
if (pair == null)
throw new ReportedException("Invalid Pair key");
}

default int beatInformation(final Choice choice1, final Choice choice2, final Direction direction) {
if (direction == null)
throw new ReportedException("Invalid direction");

int result = 0;
Pair pair = getElement(choice1, choice2);


if (direction.equals(Direction.DIRECTION_FORWARD))
result = pair.winning;
else
result = pair.losing;

return result;
}

default Pair compareBeats(final Pair beat1, final Pair beat2) {
checkPair(beat1);
checkPair(beat2);

if (beat1.winning > beat2.winning)
return beat1;
else if (beat1.winning < beat2.winning)
return beat2;
else
if (beat1.losing < beat2.losing)
return beat1;
else if (beat2.losing < beat1.losing)
return beat2;
else
throw new UnsupportedOperationException();
}


}
12 changes: 12 additions & 0 deletions src/main/java/org/rulez/demokracia/pdengine/dataobjects/Pair.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.rulez.demokracia.pdengine.dataobjects;


public class Pair extends PairEntity {
private static final long serialVersionUID = 1L;

public Pair(final int winning, final int losing) {
super();
this.winning = winning;
this.losing = losing;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.rulez.demokracia.pdengine.dataobjects;

import org.rulez.demokracia.pdengine.persistence.BaseEntity;

public class PairEntity extends BaseEntity {

private static final long serialVersionUID = 1L;
public int winning;
public int losing;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.rulez.demokracia.pdengine;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;
import org.rulez.demokracia.pdengine.BeatTable.Direction;
import org.rulez.demokracia.pdengine.annotations.TestedBehaviour;
import org.rulez.demokracia.pdengine.annotations.TestedFeature;
import org.rulez.demokracia.pdengine.annotations.TestedOperation;
import org.rulez.demokracia.pdengine.exception.ReportedException;
import org.rulez.demokracia.pdengine.testhelpers.CreatedBeatTable;

@TestedFeature("Supporting functionality")
@TestedOperation("BeatTable")
@TestedBehaviour("the beat information related to a and b can be obtained for forward and backward")
public class BeatTableBeatInformationTest extends CreatedBeatTable {

@Before
public void setUp() throws ReportedException {
super.setUp();
}

@Test
public void beatInformation_throws_an_exception_when_direction_is_not_defined() {
assertThrows(() -> beatTable.beatInformation(null, null, null)
).assertMessageIs("Invalid direction");
}

@Test
public void beatInformation_gives_back_the_number_of_winnings_from_choice1_to_choice2() {
createNewBeatTableWithData();

assertEquals(pair.winning, beatTable.beatInformation(choice1, choice2, Direction.DIRECTION_FORWARD));
}

@Test
public void beatInformation_gives_back_the_number_of_losing_from_choice1_to_choice2() {
createNewBeatTableWithData();

assertEquals(pair.losing, beatTable.beatInformation(choice1, choice2, Direction.DIRECTION_BACKWARD));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.rulez.demokracia.pdengine;

import static org.junit.Assert.assertTrue;

import org.junit.Before;
import org.junit.Test;
import org.rulez.demokracia.pdengine.annotations.TestedBehaviour;
import org.rulez.demokracia.pdengine.annotations.TestedFeature;
import org.rulez.demokracia.pdengine.annotations.TestedOperation;
import org.rulez.demokracia.pdengine.exception.ReportedException;
import org.rulez.demokracia.pdengine.testhelpers.CreatedBeatTable;

@TestedFeature("Schulze method")
@TestedOperation("compare beats")
@TestedBehaviour("if beats tie, looses decide")
public class BeatTableCompareBeatsLosingTest extends CreatedBeatTable {

@Before
public void setUp() throws ReportedException {
super.setUp();
}

@Test
public void compareBeats_gives_back_the_backward_lower_beat1() {
setCompareBeatsAsResult(beats1, beats3);
assertTrue(result.winning == beats1.winning && result.losing == beats1.losing);
}

@Test
public void compareBeats_gives_back_the_backward_lower_beat2() {
setCompareBeatsAsResult(beats1, beats5);
assertTrue(result.winning == beats5.winning && result.losing == beats5.losing);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.rulez.demokracia.pdengine;

import org.junit.Before;
import org.junit.Test;
import org.rulez.demokracia.pdengine.annotations.TestedBehaviour;
import org.rulez.demokracia.pdengine.annotations.TestedFeature;
import org.rulez.demokracia.pdengine.annotations.TestedOperation;
import org.rulez.demokracia.pdengine.exception.ReportedException;
import org.rulez.demokracia.pdengine.testhelpers.CreatedBeatTable;

@TestedFeature("Unimplemented")
@TestedOperation("Unimplemented")
@TestedBehaviour("Unimplemented")
public class BeatTableCompareBeatsTieTest extends CreatedBeatTable {

@Before
public void setUp() throws ReportedException {
super.setUp();
}

@Test
public void compareBeats_is_not_yet_implemented_when_the_result_is_tie() {
assertUnimplemented(() -> beatTable.compareBeats(beats1, beats1));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.rulez.demokracia.pdengine;

import static org.junit.Assert.assertTrue;

import org.junit.Before;
import org.junit.Test;
import org.rulez.demokracia.pdengine.annotations.TestedBehaviour;
import org.rulez.demokracia.pdengine.annotations.TestedFeature;
import org.rulez.demokracia.pdengine.annotations.TestedOperation;
import org.rulez.demokracia.pdengine.exception.ReportedException;
import org.rulez.demokracia.pdengine.testhelpers.CreatedBeatTable;

@TestedFeature("Schulze method")
@TestedOperation("compare beats")
@TestedBehaviour("if beats are different, the bigger wins")
public class BeatTableCompareBeatsWinningTest extends CreatedBeatTable {

@Before
public void setUp() throws ReportedException {
super.setUp();
}

@Test
public void compareBeats_throws_an_exception_when_first_input_param_is_not_defined() {
assertThrows(() -> beatTable.compareBeats(null, beats2)
).assertMessageIs("Invalid Pair key");
}

@Test
public void compareBeats_throws_an_exception_when_second_input_param_is_not_defined() {
assertThrows(() -> beatTable.compareBeats(beats1, null)
).assertMessageIs("Invalid Pair key");
}

@Test
public void compareBeats_gives_back_the_forward_bigger_beat1() {
setCompareBeatsAsResult(beats1, beats2);
assertTrue(result.winning == beats1.winning && result.losing == beats1.losing);
}

@Test
public void compareBeats_gives_back_the_forward_bigger_beat2() {
setCompareBeatsAsResult(beats4, beats3);
assertTrue(result.winning == beats3.winning && result.losing == beats3.losing);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package org.rulez.demokracia.pdengine.testhelpers;

import java.util.ArrayList;
import java.util.List;

import org.junit.Before;
import org.rulez.demokracia.pdengine.BeatTable;
import org.rulez.demokracia.pdengine.Choice;
import org.rulez.demokracia.pdengine.dataobjects.Pair;
import org.rulez.demokracia.testhelpers.ThrowableTester;

public class CreatedBeatTable extends ThrowableTester{
protected BeatTable beatTable;
protected List<Choice> list;

protected Pair pair;
protected Pair result;

protected Choice choice1;
protected Choice choice2;
protected Choice choice3;

protected Pair beats1;
protected Pair beats2;
protected Pair beats3;
protected Pair beats4;
protected Pair beats5;

@Before
public void setUp() {
beatTable = new BeatTable();
list = new ArrayList<Choice>();
pair = new Pair(4, 5);
choice1 = new Choice("name1", "userName1");
choice2 = new Choice("name2", "userName2");
choice3 = new Choice("name3", "userName3");
list.add(choice1);
list.add(choice2);
list.add(choice3);

beats1 = new Pair(150, 22);
beats2 = new Pair(100, 40);
beats3 = new Pair(150, 40);
beats4 = new Pair(100, 22);
beats5 = new Pair(150, 10);
}

protected void createNewBeatTableWithData() {
beatTable = new BeatTable(list);
beatTable.setElement(choice1, choice2, pair);
}

protected void createNewBeatTableWithComplexData() {
createNewBeatTableWithData();
beatTable.setElement(choice1, choice2, new Pair(14, 1));
beatTable.setElement(choice1, choice3, new Pair(13, 2));
beatTable.setElement(choice2, choice1, new Pair(12, 3));
beatTable.setElement(choice2, choice3, new Pair(11, 4));
beatTable.setElement(choice1, choice2, pair);
}

protected void setGetElementAsResult(final Choice first, final Choice second) {
result = beatTable.getElement(first, second);
}

protected void setCompareBeatsAsResult(final Pair first, final Pair second) {
result = beatTable.compareBeats(first, second);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,10 @@ public ThrowableTester assertException(final Class<? extends RuntimeException> k
assertEquals(message, klass, thrown.getClass());
return this;
}

public ThrowableTester assertUnimplemented(final Thrower thrower) {
assertThrows(thrower);
assertEquals(UnsupportedOperationException.class, thrown.getClass());
return this;
}
}

0 comments on commit 35e5aef

Please sign in to comment.