Repository : save testé

This commit is contained in:
Maximilien LEDOUX 2022-03-08 11:45:18 +01:00
parent ac04f86e72
commit d979fbd1f3
3 changed files with 61 additions and 5 deletions

View File

@ -12,10 +12,10 @@ public class Repository {
//static final String CONFIG_FILE_PATH = "../../resources/config.json";
void save(String filePath, Adapter adapter) {
static void save(String filePath, Adapter adapter) {
if (filePath != null) {
String jsonAppData = adapter.toString();
try (BufferedWriter bufferedWriter = Files.newBufferedWriter(Paths.get(filePath), StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
try (BufferedWriter bufferedWriter = Files.newBufferedWriter(Paths.get(filePath).toAbsolutePath(), StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
bufferedWriter.write(jsonAppData);
bufferedWriter.flush();
} catch (IOException e) {
@ -29,9 +29,9 @@ public class Repository {
return adapter.fromString(jsonString);
}
private String readFile(String filePath) {
static private String readFile(String filePath) {
StringBuilder builder = new StringBuilder();
try (BufferedReader reader = Files.newBufferedReader(Paths.get(filePath), StandardCharsets.UTF_8)) {
try (BufferedReader reader = Files.newBufferedReader(Paths.get(filePath).toAbsolutePath(), StandardCharsets.UTF_8)) {
while (reader.ready()) {
builder.append(reader.readLine());
}

View File

@ -10,7 +10,7 @@ import java.util.Set;
import static org.junit.jupiter.api.Assertions.*;
public class JsonAdapterTest {
public class JsonAdapterTests {
@Test
public void convertAppDataToJson() {

View File

@ -0,0 +1,56 @@
package lightcontainer.storage;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.*;
public class RepositoryTests {
@AfterEach
public void destroyTestFile() {
try {
Files.deleteIfExists(Paths.get("src", "test", "resources", "test.json").toAbsolutePath());
} catch (IOException e) {
System.out.println("Error while destroying file");
}
}
@Test
public void save() {
//GIVEN an AppData instance, a JsonAdapter and a Repository
AppData appData = AppData.getInstance();
AppConfig appConfig = AppConfig.getInstance();
appConfig.setUnicastPort(32000);
appConfig.setMulticastIp("224.25.0.1");
appConfig.setMulticastPort(15502);
appConfig.setNetworkInterface("My network interface");
appConfig.setTls(false);
Map<String, File> files = new HashMap<>();
Set<String> storage = new HashSet<>();
storage.add("StorBackEnd1");
File file1 = new File("File1", 15, "8d8d8d8d", storage);
files.put(file1.getName(), file1);
User user1 = new User("User1", "Password", "djdjjdj", files);
appData.setAppConfig(appConfig);
appData.addUser(user1);
JsonAdapter jsonAdapter = new JsonAdapter(appData);
//WHEN Repository calls save method
String filePath = "src/test/resources/test.json";
Repository.save(filePath, jsonAdapter);
//THEN
assertTrue(Files.exists(Paths.get("src/test/resources/test.json").toAbsolutePath()));
}
}