Skip to content

Commit

Permalink
added unit test methos to TestValueContainer
Browse files Browse the repository at this point in the history
  • Loading branch information
LewerenzM committed May 17, 2024
1 parent fdfd88e commit 9519357
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
* This enum provides all possible values of a ValueContainer
*/
public enum ValueContainerType {
INVALID, VOID, INT, LONG, BOOL, STRING, REQUEST, RESPONSE, INPUTSTREAM
INVALID, VOID, INT, LONG, BOOL, STRING, RESPONSE, INPUTSTREAM
}
20 changes: 0 additions & 20 deletions src/main/java/org/icatproject/ids/helpers/ValueContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.icatproject.ids.enums.ValueContainerType;
import org.icatproject.ids.exceptions.InternalException;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.ws.rs.core.Response;

/**
Expand Down Expand Up @@ -78,14 +77,6 @@ public ValueContainer(boolean value) {
this(value, ValueContainerType.BOOL);
}

/**
* Creates a ValueContainer of type Request
* @param value the value contained by the container
*/
public ValueContainer(HttpServletRequest value) {
this(value, ValueContainerType.REQUEST);
}

/**
* Creates a ValueContainer of type Response
* @param value the value contained by the container
Expand Down Expand Up @@ -150,16 +141,6 @@ public String getString() throws InternalException {
return (String) this.value;
}

/**
* Tries to return the value of the type Request.
* @return
* @throws InternalException if the container has another type an exception will be thrown
*/
public HttpServletRequest getRequest() throws InternalException {
this.checkType(ValueContainerType.REQUEST);
return (HttpServletRequest) this.value;
}

/**
* Tries to return the value of the type Response.
* @return
Expand Down Expand Up @@ -189,7 +170,6 @@ public String toString() {
case LONG: return ""+this.value;
case BOOL: return ((boolean)this.value ? "true" : "false");
case STRING: return (String)this.value;
case REQUEST: return "HttpServletRequest: " + ((HttpServletRequest) this.value).getQueryString();
case RESPONSE: return "Response " + ((Response) this.value).toString();
case INPUTSTREAM: return "An InputStream which will be printed here to prevent it from closing (and maybe it is too long).";
default:
Expand Down
32 changes: 30 additions & 2 deletions src/test/java/org/icatproject/ids/TestValueContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.stream.Collectors;

import org.icatproject.ids.enums.ValueContainerType;
import org.icatproject.ids.exceptions.InternalException;
import org.icatproject.ids.helpers.ValueContainer;
import org.junit.Test;

public class TestValueContainer {

public class TestValueContainer {

@Test
public void testInvalidValueContainer() throws Exception {
Expand Down Expand Up @@ -65,7 +71,7 @@ public void testBoolValueContainer() throws Exception {

assertEquals(vc.getBool(), true);

vc.getInt();
vc.getString();
}

@Test(expected = InternalException.class)
Expand All @@ -80,4 +86,26 @@ public void testStringValueContainer() throws Exception {

vc.getBool();
}

@Test(expected = InternalException.class)
public void testInputStreamValueContainer() throws Exception {

String s = "test InputStream";
var inputStream = new ByteArrayInputStream(s.getBytes());
var vc = new ValueContainer(inputStream);
assertFalse(vc.isVoid());
assertFalse(vc.isInvalid());
assertFalse(vc.isNull());

assertEquals(vc.getInputStream(), inputStream);

var ISReader = new InputStreamReader(vc.getInputStream(), StandardCharsets.UTF_8);
var BReader = new BufferedReader(ISReader);
String textFromStream = BReader.lines().collect(Collectors.joining());
BReader.close();
assertEquals(textFromStream, s);

vc.getString();
}

}

0 comments on commit 9519357

Please sign in to comment.