Skip to content

Commit

Permalink
utils file test
Browse files Browse the repository at this point in the history
  • Loading branch information
grada84 committed Nov 4, 2024
1 parent 9808df4 commit 38368a6
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 15 deletions.
25 changes: 10 additions & 15 deletions src/main/java/net/lacnic/portal/auth/client/LoginData.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,22 @@ public LoginData() {
}

public LoginData(String error) {
this.authenticated = false;
this.roles = new ArrayList<String>();
this.username = "";
this.error = error;

setAuthenticated(false);
setRoles(new ArrayList<String>());
setUsername("");
setError(error);
}

public LoginData(List<String> roles) {
this.authenticated = !roles.isEmpty();
this.roles = roles;
setAuthenticated(!roles.isEmpty());
setRoles(roles);
}

public LoginData(List<String> roles, String username) {
this.authenticated = !roles.isEmpty();
this.roles = roles;
this.username = username;
this.error = authenticated ? "" : "Error: verifique usuario y/o contraseña";
}

public boolean getAuthenticated() {
return authenticated;
setAuthenticated(!roles.isEmpty());
setRoles(roles);
setUsername(username);
setError(authenticated ? "" : "Error: verifique usuario y/o contraseña");
}

public boolean isAuthenticated() {
Expand Down
127 changes: 127 additions & 0 deletions src/test/java/net/lacnic/portal/auth/client/UtilsFilesTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package net.lacnic.portal.auth.client;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

public class UtilsFilesTest {

private static final String TEMP_DIR_KEY = "jboss.server.temp.dir";
private Path tempDirectory;

@BeforeEach
void setUp() throws IOException {
// Create a temporary directory for testing
tempDirectory = Files.createTempDirectory("testTempDir");
System.setProperty(TEMP_DIR_KEY, tempDirectory.toString());
}

@Test
void testObtenerFile_createsNewFileWithGivenBytes() throws IOException {
byte[] fileBytes = "Test data".getBytes();
String extension = "txt";

// Call the method under test
File resultFile = UtilsFiles.obtenerFile(fileBytes, extension);

// Validate the file creation
assertNotNull(resultFile, "File should not be null");
assertTrue(resultFile.exists(), "File should exist");

// Verify the file's path is within the temporary directory
assertEquals(tempDirectory.resolve(resultFile.getName()).toString(), resultFile.getPath(), "File path should match expected directory");

// Validate file content
byte[] readBytes = Files.readAllBytes(resultFile.toPath());
assertArrayEquals(fileBytes, readBytes, "File content should match the written bytes");

// Clean up the created file after the test
resultFile.delete();
}

@Test
void testCalcularRutaImgQR_returnsCorrectPath() {
String secretKey = "mockSecretKey";

// Get the temporary directory path from the system property
String tempDir = System.getProperty("jboss.server.temp.dir");
String expectedPath = tempDir + File.separator + secretKey + ".jpg";

// Calculate the actual path using the method
String actualPath = UtilsFiles.calcularRutaImgQR(secretKey);

// Verify that the calculated path matches the expected path
assertEquals(expectedPath, actualPath, "The generated image path should match the expected path");
}

@Test
void testGetBytesFromFile_withFilePath() throws IOException {
Path tempFile = Files.createTempFile("testFile", ".txt");
byte[] mockBytes = "test content".getBytes();
Files.write(tempFile, mockBytes);

byte[] result = UtilsFiles.getBytesFromFile(tempFile.toString());
assertArrayEquals(mockBytes, result);

Files.delete(tempFile);
}

@Test
void testGetBytesFromFile_withFileObject() throws IOException {
Path tempFile = Files.createTempFile("testFile", ".txt");
byte[] mockBytes = "test content".getBytes();
Files.write(tempFile, mockBytes);

byte[] result = UtilsFiles.getBytesFromFile(tempFile.toFile());
assertArrayEquals(mockBytes, result, "File bytes should match the expected content");

Files.delete(tempFile);
}

@Test
void testObtenerFile_returnsNullOnIOException() {
byte[] fileBytes = "Test data".getBytes();
String extension = "txt";

// Set a non-writable directory to simulate IOException
System.setProperty(TEMP_DIR_KEY, "/non_writable_directory");

// Call the method under test
File resultFile = UtilsFiles.obtenerFile(fileBytes, extension);

// Validate that the file is null due to IOException
assertNull(resultFile, "File should be null on IOException");
}

@Test
void testGetBytesFromFile_fileTooLarge() throws IOException {
// Create a temporary file
Path largeFile = Files.createTempFile("largeFile", ".txt");

// Use Mockito to mock the length of the file
File mockFile = Mockito.spy(largeFile.toFile());
Mockito.when(mockFile.length()).thenReturn((long) Integer.MAX_VALUE + 1);

// Verify that an IOException is thrown when attempting to get bytes from a
// too-large file
IOException exception = assertThrows(IOException.class, () -> {
UtilsFiles.getBytesFromFile(mockFile);
});

assertEquals("File is too large to process", exception.getMessage(), "Expected message for file too large");

Files.deleteIfExists(largeFile);
}
}

0 comments on commit 38368a6

Please sign in to comment.