Skip to content

Commit

Permalink
Merge pull request #45 from BME-MIT-IET/strategy_unit_test_david
Browse files Browse the repository at this point in the history
Strategy unit test David
  • Loading branch information
Tamassch authored May 18, 2024
2 parents ae2a1a3 + 09040b3 commit 383d322
Show file tree
Hide file tree
Showing 23 changed files with 883 additions and 8 deletions.
14 changes: 12 additions & 2 deletions src/main/java/virologist/model/strategy/BearMove.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,33 @@
import virologist.model.map.Field;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

/**
* TODO comment
*/
public class BearMove implements IMoveStr{
public class BearMove implements IMoveStr {
Field randomNeighbour;

public BearMove() {
}

public BearMove(Field randomNeighbour) {
this.randomNeighbour = randomNeighbour;
}

@Override
public void Move(Virologist v, Field from, Field to) {

ArrayList<Field> neighbours = from.GetNeighbours();
from.RemoveVirologist(v);
Field randomNeighbour;

randomNeighbour = neighbours.get(ThreadLocalRandom.current().nextInt(0, neighbours.size()));

randomNeighbour.AddVirologist(v);
randomNeighbour.DestroyMaterial();

for (Virologist vir : randomNeighbour.GetVirologists()) {
vir.TargetedWith(new Bear());
}
Expand Down
16 changes: 11 additions & 5 deletions src/main/java/virologist/model/strategy/Looted.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,17 @@
*/
public class Looted implements ILootedStr
{
private int delta;

/**
* Sikeres kifosztásért felelős stratégia létrehozása
*/
public Looted(){
public Looted() {
delta = 1;
}

public Looted(int delta) {
this.delta = delta;
}

/**
Expand All @@ -39,8 +45,8 @@ public void LootedForEquipment(Virologist v, Virologist from, Equipment e)
public void LootedForAminoAcid(Virologist v, Virologist from)
{
try {
from.RemoveAminoAcid(1);
v.AddAminoAcid(1);
from.RemoveAminoAcid(delta);
v.AddAminoAcid(delta);
} catch (Exception e) {
//Nem volt mit elvenni
}
Expand All @@ -55,8 +61,8 @@ public void LootedForAminoAcid(Virologist v, Virologist from)
public void LootedForNukleotide(Virologist v, Virologist from)
{
try{
from.RemoveNucleotide(1);
v.AddNucleotide(1);
from.RemoveNucleotide(delta);
v.AddNucleotide(delta);
}catch (Exception e){
//Nem volt mit elvenni
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/virologist/model/strategy/NoDrop.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class NoDrop implements IDropStr {
*/
@Override
public void Drop(Virologist v, Field f, Equipment e) {
v.AddEquipment(e);
v.AddEquipment(e); // Na ez szerintem folosleges ide, ki kellene szedni, es ugy tesztelni a jatekot. David
v.DecreaseActions();
}

Expand Down
58 changes: 58 additions & 0 deletions src/test/java/virologist/model/strategy/BearMoveTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package virologist.model.strategy;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import virologist.model.Virologist;
import virologist.model.agents.Bear;
import virologist.model.map.Field;

import static org.mockito.Mockito.*;

import java.util.ArrayList;

class BearMoveTest {
private BearMove bearMove;
private Virologist virologist;
private Field from;
private Field to;
private Field randomNeighbour;
private ArrayList<Field> neighbours;
private ArrayList<Virologist> virologists;
private Virologist virologist1;
private Virologist virologist2;

@BeforeEach
public void init() {
virologist = mock(Virologist.class);
from = mock(Field.class);
to = mock(Field.class);
randomNeighbour = mock(Field.class);
neighbours = new ArrayList<>();
neighbours.add(randomNeighbour);
virologists = new ArrayList<>();
virologist1 = mock(Virologist.class);
virologist2 = mock(Virologist.class);
virologists.add(virologist1);
virologists.add(virologist2);
bearMove = new BearMove(randomNeighbour);
}

@Test
void moveTest() {
// Arrange
when(from.GetNeighbours()).thenReturn(neighbours);
when(randomNeighbour.GetVirologists()).thenReturn(virologists);

// Act
bearMove.Move(virologist, from, to);

// Assert
verify(from, times(1)).RemoveVirologist(virologist);
verify(randomNeighbour, times(1)).AddVirologist(virologist);
verify(randomNeighbour, times(1)).DestroyMaterial();
verify(virologist1, times(1)).TargetedWith(any(Bear.class));
verify(virologist2, times(1)).TargetedWith(any(Bear.class));
verify(virologist, times(1)).DecreaseActions();
}

}
30 changes: 30 additions & 0 deletions src/test/java/virologist/model/strategy/DefAttackTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package virologist.model.strategy;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import virologist.model.Virologist;
import static org.mockito.Mockito.*;

class DefAttackTest {
private DefAttack defAttack;
private Virologist attacker;
private Virologist target;

@BeforeEach
public void init() {
defAttack = new DefAttack();
attacker = mock(Virologist.class);
target = mock(Virologist.class);
}

@Test
void attackTest() {
// Arrange

// Act
defAttack.Attack(attacker, target);

// Assert
verify(attacker, times(1)).DecreaseActions();
}
}
36 changes: 36 additions & 0 deletions src/test/java/virologist/model/strategy/DefCollectTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package virologist.model.strategy;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import virologist.model.Virologist;
import virologist.model.map.Field;

class DefCollectTest {
private DefCollect defCollect;
private Virologist virologist;
private Field field;

@BeforeEach
public void init() {
defCollect = new DefCollect();
virologist = mock(Virologist.class);
field = mock(Field.class);
}

@Test
void collectTest() {
// Arrange

// Act
defCollect.Collect(virologist, field);

// Assert
verify(field, times(1)).CollectMaterial(virologist);
verify(virologist, times(1)).DecreaseActions();
}
}
41 changes: 41 additions & 0 deletions src/test/java/virologist/model/strategy/DefDropTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package virologist.model.strategy;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import virologist.model.Virologist;
import virologist.model.equipments.Equipment;
import virologist.model.map.Field;

class DefDropTest {
private DefDrop defDrop;
private Virologist virologist;
private Field field;
private Equipment equipment;

@BeforeEach
public void init() {
defDrop = new DefDrop();
virologist = mock(Virologist.class);
field = mock(Field.class);
equipment = mock(Equipment.class);
}

@Test
void dropTest() {
// Arrange

// Act
defDrop.Drop(virologist, field, equipment);

// Assert
verify(equipment, times(1)).Disable(virologist);
verify(field, times(1)).Drop(equipment);
verify(virologist, times(1)).Reset();
verify(virologist, times(1)).DecreaseActions();
}
}
36 changes: 36 additions & 0 deletions src/test/java/virologist/model/strategy/DefEquipTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package virologist.model.strategy;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import virologist.model.Virologist;
import virologist.model.map.Field;

class DefEquipTest {
private DefEquip defEquip;
private Virologist virologist;
private Field field;

@BeforeEach
public void init() {
defEquip = new DefEquip();
virologist = mock(Virologist.class);
field = mock(Field.class);
}

@Test
void equipTest() {
// Arrange

// Act
defEquip.Equip(virologist, field);

// Assert
(verify(field, times(1))).PickUpEquipment(virologist);
verify(virologist, times(1)).DecreaseActions();
}
}
58 changes: 58 additions & 0 deletions src/test/java/virologist/model/strategy/DefInjectTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package virologist.model.strategy;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import virologist.model.Virologist;
import virologist.model.agents.Agent;
import virologist.model.codes.GeneticCode;

class DefInjectTest {
private DefInject defInject;
private Virologist attacker;
private Virologist target;
private GeneticCode geneticCode;
private Agent agent;

@BeforeEach
public void init() {
defInject = new DefInject();
attacker = mock(Virologist.class);
target = mock(Virologist.class);
geneticCode = mock(GeneticCode.class);
agent = mock(Agent.class);
}

@Test
void injectTest_enough_material() throws Exception {
// Arrange
doNothing().when(target).TargetedWith(attacker, agent);
when(geneticCode.Create(attacker)).thenReturn(agent);

// Act and Assert
assertDoesNotThrow(() -> defInject.Inject(attacker, target, geneticCode));
verify(attacker, times(1)).DecreaseActions();
verify(geneticCode, times(1)).Create(attacker);
verify(target, times(1)).TargetedWith(attacker, agent);
}

@Test
void injectTest_not_enough_material() throws Exception {
// Arrange
doThrow(new Exception("I don't have enough material!")).when(geneticCode).Create(attacker);

// Act and Assert
assertDoesNotThrow(() -> defInject.Inject(attacker, target, geneticCode));
verify(attacker, times(1)).DecreaseActions();
verify(geneticCode, times(1)).Create(attacker);
verify(target, times(0)).TargetedWith(attacker, agent);
}
}
48 changes: 48 additions & 0 deletions src/test/java/virologist/model/strategy/DefInjectedTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package virologist.model.strategy;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import virologist.model.Virologist;
import virologist.model.agents.Agent;
import static org.mockito.Mockito.*;

class DefInjectedTest {
private DefInjected defInjected;
private Virologist by;
private Virologist injected;
private Agent agent;

@BeforeEach
public void init() {
defInjected = new DefInjected();
by = mock(Virologist.class);
injected = mock(Virologist.class);
agent = mock(Agent.class);
}

@Test
void injectedTestSelf() {
// Arrange

// Act
defInjected.Injected(injected, agent);

// Assert
verify(agent, times(1)).Apply(injected);
verify(injected, times(1)).AddAgent(agent);
verify(agent, times(1)).ApplyStrategy(injected);
}

@Test
void injectedTestBy() {
// Arrange

// Act
defInjected.Injected(by, injected, agent);

// Assert
verify(agent, times(1)).Apply(injected);
verify(injected, times(1)).AddAgent(agent);
verify(agent, times(1)).ApplyStrategy(injected);
}
}
Loading

0 comments on commit 383d322

Please sign in to comment.