Repository : save testé
This commit is contained in:
parent
ac04f86e72
commit
d979fbd1f3
@ -12,10 +12,10 @@ public class Repository {
|
|||||||
|
|
||||||
//static final String CONFIG_FILE_PATH = "../../resources/config.json";
|
//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) {
|
if (filePath != null) {
|
||||||
String jsonAppData = adapter.toString();
|
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.write(jsonAppData);
|
||||||
bufferedWriter.flush();
|
bufferedWriter.flush();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@ -29,9 +29,9 @@ public class Repository {
|
|||||||
return adapter.fromString(jsonString);
|
return adapter.fromString(jsonString);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String readFile(String filePath) {
|
static private String readFile(String filePath) {
|
||||||
StringBuilder builder = new StringBuilder();
|
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()) {
|
while (reader.ready()) {
|
||||||
builder.append(reader.readLine());
|
builder.append(reader.readLine());
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,7 @@ import java.util.Set;
|
|||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
|
||||||
public class JsonAdapterTest {
|
public class JsonAdapterTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void convertAppDataToJson() {
|
public void convertAppDataToJson() {
|
@ -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()));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user