Skip to content

Commit

Permalink
More unit tests for Sonar
Browse files Browse the repository at this point in the history
  • Loading branch information
mjunkin committed Nov 28, 2024
1 parent a77b588 commit 29b0101
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ private Response runProjection(
InputStream debugLogStream = new ByteArrayInputStream(new byte[0]);
try {
if (debugLoggingEnabled) {
debugLogStream = FileHelper.get(debugLogPath);
debugLogStream = FileHelper.getForReading(debugLogPath);
}
} catch (IOException e) {
String message = Exceptions.getMessage(e, "Projection, when opening input files,");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ public class Exceptions {
public static String getMessage(Throwable t, String context) {

String message;

if (context == null) {
context = "";
} else {
context = context.stripTrailing();
}

if (t.getCause() != null) {

message = MessageFormat.format(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) {
return false;
}
UtilizationParameter parametersUtilsInner = (UtilizationParameter) o;
return Objects.equals(this.speciesName, parametersUtilsInner.speciesName)
&& Objects.equals(this.value, parametersUtilsInner.value);
UtilizationParameter up = (UtilizationParameter) o;
return Objects.equals(this.speciesName, up.speciesName)
&& Objects.equals(this.value, up.value);
}

@Override
Expand All @@ -135,9 +135,9 @@ public int hashCode() {
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class ParametersUtilsInner {\n");

sb.append(" speciesName: ").append(toIndentedString(speciesName)).append("\n");
sb.append("class ");
sb.append(UtilizationParameter.class.getSimpleName());
sb.append(" {\n speciesName: ").append(toIndentedString(speciesName)).append("\n");
sb.append(" value: ").append(toIndentedString(value)).append("\n");
sb.append("}");
return sb.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static InputStream getStubResourceFile(String fileName) throws IOExceptio
return FileHelper.class.getClassLoader().getResourceAsStream(resourceFilePath);
}

public static InputStream get(Path filePath) throws IOException {
public static InputStream getForReading(Path filePath) throws IOException {

return Files.newInputStream(filePath, StandardOpenOption.READ);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package ca.bc.gov.nrs.api.v1.endpoints;

import org.junit.Assert;
import org.junit.jupiter.api.Test;

import ca.bc.gov.nrs.vdyp.backend.v1.api.impl.exceptions.Exceptions;
import ca.bc.gov.nrs.vdyp.backend.v1.api.impl.exceptions.NotFoundException;
import ca.bc.gov.nrs.vdyp.backend.v1.api.impl.exceptions.ProjectionExecutionException;
import ca.bc.gov.nrs.vdyp.backend.v1.api.impl.exceptions.ProjectionRequestValidationException;

public class ExceptionsTest {

@Test
void ProjectionExecutionExceptionTest() {

var e1 = new ProjectionExecutionException(new IllegalStateException("illegal"));
Assert.assertEquals("illegal", e1.getCause().getMessage());

var e2 = new ProjectionExecutionException("validation error");
Assert.assertEquals("validation error", e2.getMessage());

var e3 = new ProjectionExecutionException(new IllegalStateException("illegal"), "validation error");
Assert.assertEquals("illegal", e3.getCause().getMessage());
Assert.assertEquals("validation error", e3.getMessage());
}

@Test
void ProjectionRequestValidationExceptionTest() {

var e1 = new ProjectionRequestValidationException(new IllegalStateException("illegal"));
Assert.assertEquals("illegal", e1.getCause().getMessage());

var e2 = new ProjectionRequestValidationException("validation error");
Assert.assertEquals("validation error", e2.getMessage());

var e3 = new ProjectionRequestValidationException(new IllegalStateException("illegal"), "validation error");
Assert.assertEquals("illegal", e3.getCause().getMessage());
Assert.assertEquals("validation error", e3.getMessage());
}

@Test
void NotFoundExceptionTest() {

var e1 = new NotFoundException();
Assert.assertEquals(null, e1.getCause());
}

@Test
void ExceptionsClassTest() {

var e1 = new ProjectionRequestValidationException("validation error");
String message1 = Exceptions.getMessage(e1, "while performation operation f, ");
Assert.assertTrue(message1.startsWith("while performation operation f, saw"));
Assert.assertTrue(message1.endsWith("validation error"));

var e2 = new ProjectionRequestValidationException(new IllegalStateException("illegal"), "validation error");
String message2 = Exceptions.getMessage(e2, "while performation operation f, ");
Assert.assertTrue(message2.startsWith("while performation operation f, saw"));
Assert.assertTrue(message2.endsWith("illegal"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package ca.bc.gov.nrs.api.v1.endpoints;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;

import org.junit.Assert;
import org.junit.jupiter.api.Test;

import ca.bc.gov.nrs.vdyp.backend.v1.utils.FileHelper;

public class FileHelperTest {

@Test
void testGetStubResourceFile() throws IOException {
InputStream is = FileHelper.getStubResourceFile("Output_Log.txt");
Assert.assertNotNull(is);
}

@Test
void testGetAndDeleteFile() throws IOException {
Path tempFilePath = Files.createTempFile("pre_", "_post");

InputStream is1 = FileHelper.getForReading(tempFilePath);
Assert.assertNotNull(is1);

FileHelper.delete(tempFilePath);

Assert.assertThrows(NoSuchFileException.class, () -> FileHelper.getForReading(tempFilePath));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ca.bc.gov.nrs.api.v1.endpoints;

import org.junit.Assert;
import org.junit.jupiter.api.Test;

import ca.bc.gov.nrs.vdyp.backend.v1.gen.api.ParameterNames;

/** test ParameterNamesTest for Sonar coverage purposes only */
public class ParameterNamesTest {

@Test
void test() {
Assert.assertEquals("historyInputData", ParameterNames.HISTORY_INPUT_DATA);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;

import org.junit.Assert;
import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.databind.ObjectMapper;
Expand All @@ -19,13 +20,10 @@
import ca.bc.gov.nrs.vdyp.backend.v1.gen.model.ProgressFrequency.EnumValue;
import ca.bc.gov.nrs.vdyp.backend.v1.gen.model.UtilizationParameter;
import ca.bc.gov.nrs.vdyp.backend.v1.gen.model.UtilizationParameter.ValueEnum;
import io.quarkus.test.junit.QuarkusTest;
import io.smallrye.common.constraint.Assert;
import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.MediaType;

@QuarkusTest
public class ParametersProviderTest {
public class ParametersTest {

@Test
public void testParametersProvider() throws WebApplicationException, IOException {
Expand Down Expand Up @@ -104,14 +102,60 @@ public void testParametersProvider() throws WebApplicationException, IOException
ParametersProvider provider = new ParametersProvider();

Assert.assertTrue(provider.isReadable(Parameters.class, Parameters.class, null, MediaType.APPLICATION_JSON_TYPE));

Parameters np = provider.readFrom(Parameters.class, Parameters.class, null, MediaType.APPLICATION_JSON_TYPE, null
, new ByteArrayInputStream(json));

Assert.assertTrue(op.equals(np));

Assert.assertFalse(provider.isReadable(Object.class, null, null, MediaType.APPLICATION_JSON_TYPE));
Assert.assertFalse(provider.isReadable(Parameters.class, null, null, MediaType.APPLICATION_OCTET_STREAM_TYPE));

np.getProgressFrequency().setEnumValue(EnumValue.POLYGON);

Assert.assertFalse(op.equals(np));
}

@Test
void testProgressFrequency() {

Assert.assertNull(new ProgressFrequency().getIntValue());
Assert.assertNull(new ProgressFrequency().getEnumValue());
Assert.assertEquals(Integer.valueOf(12), new ProgressFrequency(12).getIntValue());
Assert.assertNull(new ProgressFrequency(12).getEnumValue());
Assert.assertNull(new ProgressFrequency(ProgressFrequency.EnumValue.MAPSHEET).getIntValue());
Assert.assertEquals(ProgressFrequency.EnumValue.MAPSHEET, new ProgressFrequency(ProgressFrequency.EnumValue.MAPSHEET).getEnumValue());

Assert.assertThrows(IllegalArgumentException.class, () -> ProgressFrequency.EnumValue.fromValue("not a value"));

ProgressFrequency pf1 = new ProgressFrequency(12);
ProgressFrequency pf2 = new ProgressFrequency(ProgressFrequency.EnumValue.MAPSHEET);
Assert.assertEquals(pf1, pf1);
Assert.assertEquals(Integer.valueOf(12).hashCode(), pf1.hashCode());
Assert.assertEquals(ProgressFrequency.EnumValue.MAPSHEET.hashCode(), pf2.hashCode());
Assert.assertEquals(17, new ProgressFrequency().hashCode());

Assert.assertTrue(pf1.toString().indexOf("12") != -1);
Assert.assertTrue(pf2.toString().indexOf("mapsheet") != -1);
}

@Test
void testUtilizationParameter() {
Assert.assertEquals("AL", new UtilizationParameter().speciesName("AL").value(ValueEnum._12_5).getSpeciesName());
Assert.assertEquals(ValueEnum._17_5, new UtilizationParameter().speciesName("AL").value(ValueEnum._17_5).getValue());

Assert.assertThrows(IllegalArgumentException.class, () -> UtilizationParameter.ValueEnum.fromValue("ZZZ"));

var up1 = new UtilizationParameter().speciesName("AL").value(ValueEnum._12_5);
var up2 = new UtilizationParameter().speciesName("C").value(ValueEnum._12_5);
var up3 = new UtilizationParameter().speciesName("C").value(ValueEnum._22_5);

Assert.assertEquals(up1, up1);
Assert.assertTrue(up1.hashCode() == up1.hashCode());
Assert.assertNotEquals(up2, up3);
Assert.assertNotEquals(up2, "C");

Assert.assertTrue(up1.toString().indexOf("speciesName: AL") != -1);
Assert.assertTrue(up1.toString().indexOf("value: 12.5") != -1);
}
}

0 comments on commit 29b0101

Please sign in to comment.