From 530035ce1b6d01ec2605eafa579b64d1166fcf30 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Wed, 27 Apr 2022 22:05:13 +0200 Subject: [PATCH] loved lab04 --- .../java/ch/zhaw/prog2/heartbeat/Heart.java | 22 ++++- .../ch/zhaw/prog2/heartbeat/HeartTest.java | 97 +++++++++++-------- .../zhaw/prog2/heartbeat/PacemakerTest.java | 20 +++- 3 files changed, 94 insertions(+), 45 deletions(-) diff --git a/code/Herzschlag/src/main/java/ch/zhaw/prog2/heartbeat/Heart.java b/code/Herzschlag/src/main/java/ch/zhaw/prog2/heartbeat/Heart.java index 29bbe36..19eae26 100644 --- a/code/Herzschlag/src/main/java/ch/zhaw/prog2/heartbeat/Heart.java +++ b/code/Herzschlag/src/main/java/ch/zhaw/prog2/heartbeat/Heart.java @@ -65,14 +65,30 @@ public class Heart { * InvalidValvePositionException occurs during diastole or systole * TODO Implement a pause mechanism based on the current heart rate */ - public void executeHeartBeat() throws HeartBeatDysfunctionException, Valve.IllegalValveStateException, InvalidValvePositionException { + public void executeHeartBeat() throws HeartBeatDysfunctionException { //TODO implement for(int i = 0; i < 2; i ++){ if(state == State.DIASTOLE) { - executeDiastole(); + try { + executeDiastole(); + } catch (Valve.IllegalValveStateException e) { + e.printStackTrace(); + throw new HeartBeatDysfunctionException(e.getMessage()); + } catch (InvalidValvePositionException e) { + e.printStackTrace(); + throw new HeartBeatDysfunctionException(e.getMessage()); + } } else if(state == State.SYSTOLE){ - executeSystole(); + try { + executeSystole(); + } catch (Valve.IllegalValveStateException e) { + e.printStackTrace(); + throw new HeartBeatDysfunctionException(e.getMessage()); + } catch (InvalidValvePositionException e) { + e.printStackTrace(); + throw new HeartBeatDysfunctionException(e.getMessage()); + } } } diff --git a/code/Herzschlag/src/test/java/ch/zhaw/prog2/heartbeat/HeartTest.java b/code/Herzschlag/src/test/java/ch/zhaw/prog2/heartbeat/HeartTest.java index 5f9cd4d..89f4838 100644 --- a/code/Herzschlag/src/test/java/ch/zhaw/prog2/heartbeat/HeartTest.java +++ b/code/Herzschlag/src/test/java/ch/zhaw/prog2/heartbeat/HeartTest.java @@ -91,7 +91,7 @@ class HeartTest { @Test void testExecuteHartBeatErrorBehaviourWithStubbing() { //TODO implement - Half half = mock(Half.class, withSettings().useConstructor(Half.Side.LEFT)); + Half half = spy(new Half(Half.Side.LEFT)); Heart heart = new Heart(half, new Half(Half.Side.RIGHT)); half.initializeState(heart.getState()); when(half.isAtrioventricularValveOpen()).thenReturn(false); @@ -109,60 +109,77 @@ class HeartTest { * */ @Test - void testValvesBehavior() throws Valve.IllegalValveStateException, Heart.HeartBeatDysfunctionException, Heart.InvalidValvePositionException { + void testValvesBehavior() throws Valve.IllegalValveStateException, Heart.HeartBeatDysfunctionException { //TODO implement - Half half1 = mock(Half.class, withSettings().useConstructor(Half.Side.LEFT)); - Half half2 = mock(Half.class, withSettings().useConstructor(Half.Side.RIGHT)); - Heart heart = new Heart(half1, half2); + Half halfLeft = spy(new Half(Half.Side.LEFT)); + Half halfRight = spy(new Half(Half.Side.RIGHT)); + Heart heart = new Heart(halfLeft, halfRight); heart.executeHeartBeat(); - InOrder inOrderHalf1 = inOrder(half1); + InOrder inOrderHalfLeft = inOrder(halfLeft); - inOrderHalf1.verify(half1).closeAtrioventricularValve(); - inOrderHalf1.verify(half1).openSemilunarValve(); - inOrderHalf1.verify(half1).relaxAtrium(); - inOrderHalf1.verify(half1).relaxVentricle(); + inOrderHalfLeft.verify(halfLeft).openAtrioventricularValve(); + inOrderHalfLeft.verify(halfLeft).closeSemilunarValve(); + inOrderHalfLeft.verify(halfLeft).relaxAtrium(); + inOrderHalfLeft.verify(halfLeft).relaxVentricle(); - inOrderHalf1.verify(half1).closeAtrioventricularValve(); - inOrderHalf1.verify(half1).openSemilunarValve(); - inOrderHalf1.verify(half1).contractAtrium(); - inOrderHalf1.verify(half1).contractVentricle(); + inOrderHalfLeft.verify(halfLeft).closeAtrioventricularValve(); + inOrderHalfLeft.verify(halfLeft).openSemilunarValve(); + inOrderHalfLeft.verify(halfLeft).contractAtrium(); + inOrderHalfLeft.verify(halfLeft).contractVentricle(); - inOrderHalf1.verifyNoMoreInteractions(); + inOrderHalfLeft.verifyNoMoreInteractions(); - InOrder inOrderHalf2 = inOrder(half2); + InOrder inOrderHalfRight = inOrder(halfRight); - inOrderHalf2.verify(half2).closeAtrioventricularValve(); - inOrderHalf2.verify(half2).openSemilunarValve(); - inOrderHalf2.verify(half2).relaxAtrium(); - inOrderHalf2.verify(half2).relaxVentricle(); + inOrderHalfRight.verify(halfRight).openAtrioventricularValve(); + inOrderHalfRight.verify(halfRight).closeSemilunarValve(); + inOrderHalfRight.verify(halfRight).relaxAtrium(); + inOrderHalfRight.verify(halfRight).relaxVentricle(); - inOrderHalf2.verify(half2).closeAtrioventricularValve(); - inOrderHalf2.verify(half2).openSemilunarValve(); - inOrderHalf2.verify(half2).contractAtrium(); - inOrderHalf2.verify(half2).contractVentricle(); + inOrderHalfRight.verify(halfRight).closeAtrioventricularValve(); + inOrderHalfRight.verify(halfRight).openSemilunarValve(); + inOrderHalfRight.verify(halfRight).contractAtrium(); + inOrderHalfRight.verify(halfRight).contractVentricle(); - inOrderHalf2.verifyNoMoreInteractions(); + inOrderHalfRight.verifyNoMoreInteractions(); } - /** - * This is code used for the lecture slide. - */ @Test - void testForSlide() throws Valve.IllegalValveStateException, Heart.InvalidValvePositionException { - Half mockedHalf = mock(Half.class); - Heart heart = new Heart(mockedHalf, new Half(Half.Side.RIGHT)); - heart.setState(State.SYSTOLE); - heart.initalizeState(); - - when(mockedHalf.isAtrioventricularValveOpen()).thenReturn(false); - when(mockedHalf.isSemilunarValveOpen()).thenReturn(true); - heart.executeSystole(); - - verify(mockedHalf).contractVentricle(); - verify(mockedHalf, times(1)).contractAtrium(); + void testDiastoleExceptionAtrioventricularValve(){ + Half half = spy(new Half(Half.Side.LEFT)); + Heart heart = new Heart(half, new Half(Half.Side.RIGHT)); + when(half.isAtrioventricularValveOpen()).thenReturn(true); + assertThrows(Heart.InvalidValvePositionException .class, // verification using lambda + () -> heart.executeDiastole()); } + @Test + void testDiastoleExceptionSemilunarValve(){ + Half half = spy(new Half(Half.Side.LEFT)); + Heart heart = new Heart(half, new Half(Half.Side.RIGHT)); + when(half.isSemilunarValveOpen()).thenReturn(false); + assertThrows(Heart.InvalidValvePositionException .class, // verification using lambda + () -> heart.executeDiastole()); + } + + @Test + void testSystoleExceptionAtrioventricularValve(){ + Half half = spy(new Half(Half.Side.LEFT)); + Heart heart = new Heart(half, new Half(Half.Side.RIGHT)); + when(half.isAtrioventricularValveOpen()).thenReturn(false); + assertThrows(Heart.InvalidValvePositionException .class, // verification using lambda + () -> heart.executeSystole()); + } + + @Test + void testSystoleExceptionSemilunarValve(){ + Half half = spy(new Half(Half.Side.LEFT)); + Heart heart = new Heart(half, new Half(Half.Side.RIGHT)); + when(half.isSemilunarValveOpen()).thenReturn(true); + assertThrows(Heart.InvalidValvePositionException .class, // verification using lambda + () -> heart.executeSystole()); + } } diff --git a/code/Herzschlag/src/test/java/ch/zhaw/prog2/heartbeat/PacemakerTest.java b/code/Herzschlag/src/test/java/ch/zhaw/prog2/heartbeat/PacemakerTest.java index 52d353f..b8d05f1 100644 --- a/code/Herzschlag/src/test/java/ch/zhaw/prog2/heartbeat/PacemakerTest.java +++ b/code/Herzschlag/src/test/java/ch/zhaw/prog2/heartbeat/PacemakerTest.java @@ -1,6 +1,8 @@ package ch.zhaw.prog2.heartbeat; import org.junit.jupiter.api.Test; +import org.mockito.Mock; + import static org.junit.jupiter.api.Assertions.*; /* @@ -8,13 +10,25 @@ import static org.junit.jupiter.api.Assertions.*; */ public class PacemakerTest { + @Mock + Pacemaker pacemaker = new Pacemaker(new Heart()); + /** * Test if setHeartRate does throw correct exception when rate is rejected (because frequency is out of range) */ @Test void testSetHeartRateRejectsFrequenciesOutOfRange() { //TODO implement - fail(); + for(int i = -10; i < 29; i ++){ + int finalI = i; + assertThrows(IllegalArgumentException .class, // verification using lambda + () -> pacemaker.setHeartRate(finalI)); + } + for(int i = 221; i < 230; i ++){ + int finalI1 = i; + assertThrows(IllegalArgumentException .class, // verification using lambda + () -> pacemaker.setHeartRate(finalI1)); + } } @@ -24,7 +38,9 @@ public class PacemakerTest { @Test void testSetHeartRateAppliesFrequenciesInsideRange() { //TODO implement - fail(); + for(int i = 30; i <= 220; i ++){ + assertEquals(i, pacemaker.setHeartRate(i)); + } } }