diff --git a/pi4micronaut-utils/build.gradle b/pi4micronaut-utils/build.gradle index 339297cb..65f2680a 100644 --- a/pi4micronaut-utils/build.gradle +++ b/pi4micronaut-utils/build.gradle @@ -30,6 +30,10 @@ dependencies { testImplementation 'org.junit.jupiter:junit-jupiter-api:5.5.1' testImplementation 'org.junit.jupiter:junit-jupiter' testRuntimeOnly 'org.junit.platform:junit-platform-launcher' + testImplementation("org.mockito:mockito-core:5.+") + testImplementation(platform("org.junit:junit-bom:5.11.0")) + testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine") + testRuntimeOnly("org.junit.vintage:junit-vintage-engine") } diff --git a/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/LEDHelper.java b/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/LEDHelper.java index b88abc94..0a4757ed 100644 --- a/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/LEDHelper.java +++ b/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/LEDHelper.java @@ -11,7 +11,7 @@ * */ public class LEDHelper { - private static final Logger log = LoggerFactory.getLogger(LEDHelper.class); + private static Logger log = LoggerFactory.getLogger(LEDHelper.class); private final DigitalOutput ledOutput; @@ -82,4 +82,13 @@ public void blink (int duration) { ledOutput.blink(duration, TimeUnit.MILLISECONDS); } + + /** + * Sets the logger object. + * + * @param log Logger object to set the logger to. + */ + public void setLog(Logger log) { + this.log = log; + } } diff --git a/pi4micronaut-utils/src/test/java/com/opensourcewithslu/outputdevices/LEDHelperTest.java b/pi4micronaut-utils/src/test/java/com/opensourcewithslu/outputdevices/LEDHelperTest.java new file mode 100644 index 00000000..7cab7784 --- /dev/null +++ b/pi4micronaut-utils/src/test/java/com/opensourcewithslu/outputdevices/LEDHelperTest.java @@ -0,0 +1,84 @@ +package com.opensourcewithslu.outputdevices; + +import com.pi4j.io.gpio.digital.DigitalOutput; +import org.junit.jupiter.api.*; +import org.slf4j.Logger; + +import java.util.concurrent.TimeUnit; + +import static org.mockito.Mockito.*; + +class LEDHelperTest { + DigitalOutput ledOutput = mock(DigitalOutput.class); + LEDHelper ledHelper = new LEDHelper(ledOutput); + Logger log = mock(Logger.class); + + @BeforeEach + public void openMocks() { + ledHelper.setLog(log); + } + + @Test + void ledOnTurnsOnWhenOff() { + when(ledOutput.isLow()).thenReturn(true); + ledHelper.ledOn(); + verify(ledOutput).high(); + } + + @Test + void ledOnDoesNotTurnOnWhenOn() { + when(ledOutput.isLow()).thenReturn(false); + ledHelper.ledOn(); + verify(ledOutput, never()).high(); + } + + @Test + void ledOffTurnsOffWhenOn() { + when(ledOutput.isHigh()).thenReturn(true); + ledHelper.ledOff(); + verify(ledOutput).low(); + } + + @Test + void ledOffDoesNotTurnOffWhenOff() { + when(ledOutput.isHigh()).thenReturn(false); + ledHelper.ledOff(); + verify(ledOutput, never()).low(); + } + + @Test + void switchStateTogglesStateWhenOn() { + when(ledOutput.isHigh()).thenReturn(true); + ledHelper.switchState(); + verify(ledOutput).low(); + } + + @Test + void switchStateTogglesStateWhenOff() { + when(ledOutput.isHigh()).thenReturn(false); + when(ledOutput.isLow()).thenReturn(true); // Called when switchState() calls ledOn() + ledHelper.switchState(); + verify(ledOutput).high(); + } + + @Test + void blinkCallsWithCorrectParameters() { + int duration = 1000; + ledHelper.blink(duration); + verify(ledOutput).blink(duration, TimeUnit.MILLISECONDS); + } + + @Test + void ledOnLogsWhenTurnedOn() { + when(ledOutput.isLow()).thenReturn(true); + ledHelper.ledOn(); + verify(log).info("Turning on LED"); + } + + @Test + void ledOffLogsWhenTurnedOff() { + when(ledOutput.isHigh()).thenReturn(true); + ledHelper.ledOff(); + verify(log).info("Turning off LED"); + } +} \ No newline at end of file