Skip to content

Commit

Permalink
test(nm): refactor to use better exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mattdibi committed Jan 10, 2024
1 parent d56aab4 commit e800a1b
Showing 1 changed file with 36 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@
package org.eclipse.kura.nm;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Optional;

import org.eclipse.kura.configuration.Password;
Expand All @@ -42,13 +45,12 @@ public class NetworkPropertiesTest {
private List<String> stringListResult;
private Map<String, Object> resultMap;

private Boolean hasNullPointExceptionBeenThrown = false;
private Boolean hasNoSuchElementExceptionBeenThrown = false;
private Exception occurredException;

@Test
public void constructorShouldThrowWithNullMap() {
givenNetworkPropertiesBuiltWith(null);
thenANullPointerExceptionOccured();
thenExceptionOccurred(NullPointerException.class);
}

@Test
Expand Down Expand Up @@ -91,31 +93,31 @@ public void getShouldThrowWithNullValue() {
givenMapWith("testKey1", null);
givenNetworkPropertiesBuiltWith(this.properties);
whenGetIsCalledWith("testKey1", String.class);
thenANoSuchElementExceptionOccured();
thenExceptionOccurred(NoSuchElementException.class);
}

@Test
public void getShouldThrowWithMissingKey() {
givenMapWith("testKey1", null);
givenNetworkPropertiesBuiltWith(this.properties);
whenGetIsCalledWith("testKey1-nonExistant", String.class);
thenANoSuchElementExceptionOccured();
thenExceptionOccurred(NoSuchElementException.class);
}

@Test
public void getShouldThrowWithEmptyString() {
givenMapWith("Empty-String", "");
givenNetworkPropertiesBuiltWith(this.properties);
whenGetIsCalledWith("Empty-String", String.class);
thenANoSuchElementExceptionOccured();
thenExceptionOccurred(NoSuchElementException.class);
}

@Test
public void getShouldThrowWithEmptyPassword() {
givenMapWith("Empty-Password", new Password(""));
givenNetworkPropertiesBuiltWith(this.properties);
whenGetIsCalledWith("Empty-Password", Password.class);
thenANoSuchElementExceptionOccured();
thenExceptionOccurred(NoSuchElementException.class);
}

@Test
Expand Down Expand Up @@ -282,15 +284,15 @@ public void getStringListShouldThrowWithNullValue() {
givenMapWith("testKey-comma-seperated", null);
givenNetworkPropertiesBuiltWith(this.properties);
whenGetStringListIsCalledWith("testKey-comma-seperated");
thenANoSuchElementExceptionOccured();
thenExceptionOccurred(NoSuchElementException.class);
}

@Test
public void getStringListShouldThrowWithNonExistantKey() {
givenMapWith("testKey-comma-seperated", null);
givenNetworkPropertiesBuiltWith(this.properties);
whenGetStringListIsCalledWith("testKey-comma-seperated-not-existant");
thenANoSuchElementExceptionOccured();
thenExceptionOccurred(NoSuchElementException.class);
}

@Test
Expand Down Expand Up @@ -366,14 +368,10 @@ public void givenMapWith(String key, Object pair) {
}

public void givenNetworkPropertiesBuiltWith(Map<String, Object> properties) {

try {
this.netProps = new NetworkProperties(properties);

} catch (NullPointerException e) {
this.hasNullPointExceptionBeenThrown = true;
} catch (NoSuchElementException e) {
this.hasNoSuchElementExceptionBeenThrown = true;
} catch (Exception e) {
this.occurredException = e;
}
}

Expand Down Expand Up @@ -404,42 +402,32 @@ public void whenGetIsCalledWith(String key, Class<?> clazz) {
throw new IllegalArgumentException("Data type is not supported with this Test");
}

} catch (NullPointerException e) {
this.hasNullPointExceptionBeenThrown = true;
} catch (NoSuchElementException e) {
this.hasNoSuchElementExceptionBeenThrown = true;
} catch (Exception e) {
this.occurredException = e;
}
}

public void whenGetOptIsCalledWith(String key, Class<?> clazz) {
try {
this.optResult = this.netProps.getOpt(clazz, key, "");
} catch (NullPointerException e) {
this.hasNullPointExceptionBeenThrown = true;
} catch (NoSuchElementException e) {
this.hasNoSuchElementExceptionBeenThrown = true;
} catch (Exception e) {
this.occurredException = e;
}
}

public void whenGetStringListIsCalledWith(String key) {
try {
this.stringListResult = this.netProps.getStringList(key, "");

} catch (NullPointerException e) {
this.hasNullPointExceptionBeenThrown = true;
} catch (NoSuchElementException e) {
this.hasNoSuchElementExceptionBeenThrown = true;
} catch (Exception e) {
this.occurredException = e;
}
}

public void whenGetOptStringListIsCalledWith(String key) {
try {
this.optResult = this.netProps.getOptStringList(key, "");

} catch (NullPointerException e) {
this.hasNullPointExceptionBeenThrown = true;
} catch (NoSuchElementException e) {
this.hasNoSuchElementExceptionBeenThrown = true;
} catch (Exception e) {
this.occurredException = e;
}
}

Expand Down Expand Up @@ -492,17 +480,22 @@ private void thenOptionalPasswordResultEquals(Optional<Password> optPasswordResu
}
}

public void thenANullPointerExceptionOccured() {
assertTrue(this.hasNullPointExceptionBeenThrown);
}

public void thenANoSuchElementExceptionOccured() {
assertTrue(this.hasNoSuchElementExceptionBeenThrown);
private <E extends Exception> void thenExceptionOccurred(Class<E> expectedException) {
assertNotNull(this.occurredException);
assertEquals(expectedException.getName(), this.occurredException.getClass().getName());
}

public void thenNoExceptionsOccured() {
assertFalse(this.hasNullPointExceptionBeenThrown);
assertFalse(this.hasNoSuchElementExceptionBeenThrown);
String errorMessage = "Empty message";
if (Objects.nonNull(this.occurredException)) {
StringWriter sw = new StringWriter();
this.occurredException.printStackTrace(new PrintWriter(sw));

errorMessage = String.format("No exception expected, \"%s\" found. Caused by: %s",
this.occurredException.getClass().getName(), sw.toString());
}

assertNull(errorMessage, this.occurredException);
}

}

0 comments on commit e800a1b

Please sign in to comment.