-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
197aca0
commit 841da37
Showing
6 changed files
with
95 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,25 @@ | ||
package ch.stephan.chickenfarm.dto; | ||
|
||
public record Chicken(String name, int weight) { | ||
public static final Chicken HEIDI = new Chicken("Heidi", 2200); | ||
public static final Chicken KLARA = new Chicken("Klara", 2850); | ||
public static final Chicken LILI = new Chicken("Lili", 2851); | ||
public static final Chicken LULU = new Chicken("Lulu", 3450); | ||
public enum Chicken { | ||
HEIDI("Heidi", 2200), // | ||
KLARA("Klara", 2850), // | ||
LILI("Lili", 2851), // | ||
LULU("Lulu", 3450); | ||
|
||
private String name; | ||
private int weight; | ||
|
||
private Chicken(String name, int weight) { | ||
this.name = name; | ||
this.weight = weight; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public int getWeight() { | ||
return weight; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 56 additions & 54 deletions
110
src/test/java/ch/stephan/chickenfarm/timer/ScaleObserverTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1,90 @@ | ||
package ch.stephan.chickenfarm.timer; | ||
|
||
import ch.stephan.chickenfarm.dto.Box; | ||
import ch.stephan.chickenfarm.dto.Chicken; | ||
import ch.stephan.chickenfarm.messenger.MessengerService; | ||
import ch.stephan.chickenfarm.registry.BoxService; | ||
import ch.stephan.chickenfarm.registry.ChickenService; | ||
import ch.stephan.chickenfarm.scale.ScaleService; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.times; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.times; | ||
|
||
import ch.stephan.chickenfarm.dto.Box; | ||
import ch.stephan.chickenfarm.dto.Chicken; | ||
import ch.stephan.chickenfarm.messenger.MessengerService; | ||
import ch.stephan.chickenfarm.registry.BoxService; | ||
import ch.stephan.chickenfarm.registry.ChickenService; | ||
import ch.stephan.chickenfarm.scale.ScaleService; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class ScaleObserverTest { | ||
|
||
private ScaleObserver scaleObserver; | ||
private ScaleObserver scaleObserver; | ||
|
||
@InjectMocks | ||
private BoxService boxService; | ||
|
||
@Mock | ||
private ScaleService mockedScaleService; | ||
|
||
@InjectMocks | ||
private BoxService boxService; | ||
@Mock | ||
private MessengerService mockMessengerService; | ||
|
||
@Mock | ||
private ScaleService mockedScaleService; | ||
@InjectMocks | ||
private ChickenService chickenService; | ||
|
||
@Mock | ||
private MessengerService mockMessengerService; | ||
@BeforeEach | ||
void setUp() throws Exception { | ||
boxService.initBoxes(); | ||
chickenService.initBoxes(); | ||
|
||
@InjectMocks | ||
private ChickenService chickenService; | ||
scaleObserver = new ScaleObserver(boxService, mockedScaleService, mockMessengerService, chickenService); | ||
} | ||
|
||
@BeforeEach | ||
void setUp() throws Exception { | ||
boxService.initBoxes(); | ||
chickenService.initBoxes(); | ||
@Test | ||
void testMeasureWeights() { | ||
// Step 1: Two chickens are in the house | ||
Box vorne = Box.VORNE; | ||
Box hinten = Box.HINTEN; | ||
Chicken heidi = Chicken.HEIDI; | ||
Chicken klara = Chicken.KLARA; | ||
|
||
scaleObserver = new ScaleObserver(boxService, mockedScaleService, mockMessengerService, chickenService); | ||
} | ||
Mockito.when(mockedScaleService.measureWeight(eq(hinten.getId()))).thenReturn(klara.getWeight() + 13); | ||
Mockito.when(mockedScaleService.measureWeight(eq(vorne.getId()))).thenReturn(heidi.getWeight() + 17); | ||
|
||
@Test | ||
void testMeasureWeights() { | ||
// Step 1: Two chickens are in the house | ||
Box vorne = Box.VORNE; | ||
Box hinten = Box.HINTEN; | ||
Chicken heidi = Chicken.HEIDI; | ||
Chicken klara = Chicken.KLARA; | ||
scaleObserver.measureWeights(); | ||
|
||
Mockito.when(mockedScaleService.measureWeight(eq(hinten.getId()))).thenReturn(klara.weight() + 13); | ||
Mockito.when(mockedScaleService.measureWeight(eq(vorne.getId()))).thenReturn(heidi.weight() + 17); | ||
Mockito.verify(mockMessengerService, times(2)).sendNotification(anyString()); | ||
|
||
scaleObserver.measureWeights(); | ||
// measure again, no new message | ||
scaleObserver.measureWeights(); | ||
|
||
Mockito.verify(mockMessengerService, times(2)).sendNotification(anyString()); | ||
Mockito.verify(mockMessengerService, times(2)).sendNotification(anyString()); | ||
|
||
// measure again, no new message | ||
scaleObserver.measureWeights(); | ||
// Step 2: Chickens laid eggs and left the house | ||
Mockito.when(mockedScaleService.measureWeight(eq(hinten.getId()))).thenReturn(67); | ||
Mockito.when(mockedScaleService.measureWeight(eq(vorne.getId()))).thenReturn(64); | ||
|
||
Mockito.verify(mockMessengerService, times(2)).sendNotification(anyString()); | ||
scaleObserver.measureWeights(); | ||
|
||
// Step 2: Chickens laid eggs and left the house | ||
Mockito.when(mockedScaleService.measureWeight(eq(hinten.getId()))).thenReturn(67); | ||
Mockito.when(mockedScaleService.measureWeight(eq(vorne.getId()))).thenReturn(64); | ||
Mockito.verify(mockMessengerService, times(4)).sendNotification(anyString()); | ||
|
||
scaleObserver.measureWeights(); | ||
// measure again, no new message | ||
scaleObserver.measureWeights(); | ||
|
||
Mockito.verify(mockMessengerService, times(4)).sendNotification(anyString()); | ||
Mockito.verify(mockMessengerService, times(4)).sendNotification(anyString()); | ||
|
||
// measure again, no new message | ||
scaleObserver.measureWeights(); | ||
|
||
Mockito.verify(mockMessengerService, times(4)).sendNotification(anyString()); | ||
|
||
// Step 3: Pamela removed the eggs | ||
Mockito.when(mockedScaleService.measureWeight(eq(hinten.getId()))).thenReturn(2); | ||
Mockito.when(mockedScaleService.measureWeight(eq(vorne.getId()))).thenReturn(-3); | ||
// Step 3: Pamela removed the eggs | ||
Mockito.when(mockedScaleService.measureWeight(eq(hinten.getId()))).thenReturn(2); | ||
Mockito.when(mockedScaleService.measureWeight(eq(vorne.getId()))).thenReturn(-3); | ||
|
||
scaleObserver.measureWeights(); | ||
scaleObserver.measureWeights(); | ||
|
||
Mockito.verify(mockMessengerService, times(6)).sendNotification(anyString()); | ||
Mockito.verify(mockMessengerService, times(6)).sendNotification(anyString()); | ||
|
||
} | ||
} | ||
|
||
} |