Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions api/src/test/java/org/openmrs/util/OpenmrsUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@
*/
package org.openmrs.util;


import org.apache.commons.io.FileUtils;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.anyOf;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
Expand All @@ -25,6 +29,8 @@
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import java.io.File;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
Expand Down Expand Up @@ -151,6 +157,52 @@ public void collectionContains_shouldUseEqualsMethodForComparisonInsteadOfCompar

assertTrue(OpenmrsUtil.collectionContains(identifiers, pi), "Just because the date is null, doesn't make it not in the list anymore");
}


@Test
public void getFileAsBytes_shouldReturnByteArrayForValidFile() throws Exception {
File temp = File.createTempFile("openmrs-test-", ".txt");
String content = "Hello OpenMRS";
FileUtils.writeStringToFile(temp, content, StandardCharsets.UTF_8);

byte[] result = OpenmrsUtil.getFileAsBytes(temp);

assertNotNull(result);
assertArrayEquals(content.getBytes(StandardCharsets.UTF_8), result);

temp.delete();
}

@Test
public void getFileAsBytes_shouldReturnEmptyArrayForEmptyFile() throws Exception {
File temp = File.createTempFile("openmrs-test-empty-", ".txt");

byte[] result = OpenmrsUtil.getFileAsBytes(temp);

assertNotNull(result);
assertEquals(0, result.length);

temp.delete();
}

@Test
public void getFileAsBytes_shouldReturnNullForNullInput() throws Exception {
byte[] result = OpenmrsUtil.getFileAsBytes(null);
assertNull(result);
}

@Test
public void getFileAsBytes_shouldReturnNullForNonExistentFile() throws Exception {
File fake = new File("this/path/does/not/exist.txt");

byte[] result = null;
try {
result = OpenmrsUtil.getFileAsBytes(fake);
} catch (Exception ignored) { }

assertNull(result);
}


/**
* When given a null parameter, the {@link OpenmrsUtil#url2file(java.net.URL)} method should
Expand Down
18 changes: 18 additions & 0 deletions api/src/test/java/org/openmrs/validator/ValidateUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
package org.openmrs.validator;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
Expand Down Expand Up @@ -208,4 +209,21 @@ public void validate_shouldReturnThrowExceptionAlongWithAppropriateMessageIfTheO
ValidationException exception = assertThrows(ValidationException.class, () -> ValidateUtil.validate(drug));
assertTrue(exception.getMessage().contains("failed to validate with reason: name: This value exceeds the maximum length of 255 permitted for this field."));
}

//fixed :validating a location
@Test
public void validate_shouldExposeExactValidationMessage() {
// Arrange
Location loc = new Location(); // invalid: name missing
BindException errors = new BindException(loc, "location");

// Act
ValidationException ex = assertThrows(ValidationException.class,
() -> ValidateUtil.validate(loc));

// Assert
assertTrue(ex.getErrors().hasFieldErrors("name"));
assertEquals("error.name", ex.getErrors().getFieldError("name").getCode());
}

}