Skip to content
Snippets Groups Projects
Commit af4df424 authored by Hallvard Trætteberg's avatar Hallvard Trætteberg
Browse files

integrasjonstest

parent 74088ee2
No related branches found
No related tags found
No related merge requests found
Showing
with 316 additions and 63 deletions
......@@ -24,6 +24,19 @@
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.11.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.openjfx/javafx-fxml -->
<dependency>
<groupId>org.openjfx</groupId>
......@@ -38,12 +51,6 @@
<version>4.7.0-9.1.2</version>
</dependency>
<dependency>
<groupId>org.tomlj</groupId>
<artifactId>tomlj</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
......
package todolist.ui;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import fxutil.doc.DocumentListener;
import fxutil.doc.FileMenuController;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
import org.tomlj.Toml;
import org.tomlj.TomlParseResult;
import javafx.fxml.FXML;
import todolist.core.TodoModel;
......@@ -47,7 +51,6 @@ public class TodoDocumentAppController implements DocumentListener<TodoModel, Fi
*
* fileMenu.recentFiles = [ ... ]
*/
private TomlParseResult config;
@FXML
private void initialize() {
......@@ -59,46 +62,82 @@ public class TodoDocumentAppController implements DocumentListener<TodoModel, Fi
}
}
private JsonNode config;
private ObjectMapper configMapper = new ObjectMapper();
private void applyConfig() {
Path configPath = Paths.get(System.getProperty("user.home"), userAppConfigPath);
if (userAppConfigPath != null) {
try {
Path configPath = Paths.get(System.getProperty("user.home"), userAppConfigPath);
config = Toml.parse(configPath);
config = configMapper.readTree(configPath.toFile());
} catch (IOException ioex) {
System.err.println("Fant ingen " + userAppConfigPath + " på hjemmeområdet");
}
}
if (config == null) {
try {
config = Toml.parse(getClass().getResourceAsStream("todo-config.toml"));
try (InputStream input = getClass().getResourceAsStream("todo-config.json")) {
config = configMapper.readTree(input);
} catch (IOException e) {
// ignore
}
}
if (config == null) {
config = JsonNodeFactory.instance.objectNode();
}
if (config != null) {
if (config.contains("fileMenu.recentFiles")) {
List<File> recentFiles = config.getArray("fileMenu.recentFiles").toList().stream()
.map(o -> new File(o.toString())).collect(Collectors.toList());
fileMenuController.addRecentFiles(recentFiles.toArray(new File[recentFiles.size()]));
ArrayNode recentFiles = getConfigProperty("fileMenu", "recentFiles");
if (recentFiles != null) {
List<File> recentFilesList = new ArrayList<>();
recentFiles.forEach(element -> recentFilesList.add(new File(element.asText())));
fileMenuController.addRecentFiles(recentFilesList.toArray(new File[0]));
}
}
}
void writeConfig() {
// TODO
Path configPath = Paths.get(System.getProperty("user.home"), userAppConfigPath);
try (FileWriter writer = new FileWriter(configPath.toFile())) {
writer.write("[fileMenu]\n");
writer.write("recentFiles = [ ");
writer.write(fileMenuController.getRecentFiles().stream()
.map(o -> "\"" + o + "\"")
.collect(Collectors.joining(", ")));
writer.write(" ]\n");
ArrayNode recentFilesArray = JsonNodeFactory.instance.arrayNode();
for (File file : fileMenuController.getRecentFiles()) {
recentFilesArray.add(JsonNodeFactory.instance.textNode(file.toString()));
}
setConfigProperty(recentFilesArray, "fileMenu", "recentFiles");
try {
configMapper.writeValue(configPath.toFile(), config);
} catch (IOException ioe) {
System.out.println("Fikk ikke skrevet konfigurasjon til " + userAppConfigPath + " på hjemmeområdet");
}
}
private <T extends JsonNode> T getConfigProperty(String... path) {
JsonNode node = config;
for (String segment : path) {
if (node instanceof ObjectNode) {
node = ((ObjectNode) node).get(segment);
} else {
return null;
}
}
return (T) node;
}
private void setConfigProperty(JsonNode newNode, String... path) {
JsonNode node = config;
Iterator<String> segments = List.of(path).iterator();
while (segments.hasNext()) {
String segment = segments.next();
if (node instanceof ObjectNode) {
if (! segments.hasNext()) {
((ObjectNode) node).set(segment, newNode);
return;
} else {
ObjectNode objectNode = JsonNodeFactory.instance.objectNode();
((ObjectNode) node).set(segment, objectNode);
node = objectNode;
}
}
}
}
// DocumentListener
@Override
......
{
"fileMenu": {
"recentFiles": []
}
}
[fileMenu]
recentFiles = []
<?xml version="1.0" encoding="UTF-8" ?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>it1901.todolist</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>integrationtests</artifactId>
<packaging>war</packaging>
<properties>
<slf4jVersion>1.7.25</slf4jVersion>
<jerseyVersion>2.28</jerseyVersion>
</properties>
<dependencies>
<dependency>
<groupId>it1901.todolist</groupId>
<artifactId>core</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>it1901.todolist</groupId>
<artifactId>fxui</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!--
https://pragmaticintegrator.wordpress.com/2010/10/22/using-a-war-module-as-dependency-in-maven/
-->
<dependency>
<groupId>it1901.todolist</groupId>
<artifactId>restserver</artifactId>
<version>0.0.1-SNAPSHOT</version>
<classifier>classes</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-core -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</dependency>
<!-- Test with TextFX -->
<dependency>
<groupId>org.testfx</groupId>
<artifactId>testfx-core</artifactId>
<version>4.0.16-alpha</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testfx</groupId>
<artifactId>testfx-junit5</artifactId>
<version>4.0.16-alpha</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>${jerseyVersion}</version>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.0.0-M1</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.4.31.v20200723</version>
<configuration>
<httpConnector>
<port>8999</port>
</httpConnector>
<stopKey>quit</stopKey>
<stopPort>9000</stopPort>
</configuration>
<executions>
<execution>
<id>start-jetty</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>stop-jetty</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
package todolist.ui;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import todolist.json.TodoPersistence;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.fail;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.testfx.framework.junit5.ApplicationTest;
public class TodoAppIT extends ApplicationTest {
private TodoModelController controller;
@Override
public void start(final Stage stage) throws Exception {
final FXMLLoader loader = new FXMLLoader(getClass().getResource("TodoApp_it.fxml"));
final Parent root = loader.load();
this.controller = loader.getController();
stage.setScene(new Scene(root));
stage.show();
}
private TodoPersistence todoPersistence = new TodoPersistence();
@BeforeEach
public void setupItems() {
// same as in test-todolist.json (should perhaps read it instead)
try (Reader reader = new InputStreamReader(getClass().getResourceAsStream("it-todomodel.json"))) {
this.controller.setTodoModel(todoPersistence.readTodoModel(reader));
} catch (IOException ioe) {
fail(ioe.getMessage());
}
}
@Test
public void testController_initial() {
assertNotNull(this.controller);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.String?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Tooltip?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.ListView?>
<?import de.jensd.fx.glyphs.fontawesome.FontAwesomeIconView?>
<VBox xmlns:fx="http://javafx.com/fxml/1" fx:controller="todolist.ui.TodoModelController">
<HBox>
<ComboBox fx:id="todoListsView"/>
</HBox>
<fx:include fx:id="todoListView" source="TodoList_it.fxml"/>
</VBox>
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.String?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.ListView?>
<VBox xmlns:fx="http://javafx.com/fxml/1" fx:controller="todolist.ui.TodoListController">
<HBox>
<Button fx:id="newTodoItemButton" text="New Item" onAction="#handleNewTodoItemAction"/>
<TextField fx:id="newTodoItemText" promptText="Skriv inn tekst her" onAction="#handleNewTodoItemAction"/>
</HBox>
<ListView fx:id="todoItemsView">
</ListView>
<HBox>
<Button fx:id="deleteTodoItemButton" text="Delete Item" onAction="#handleDeleteItemAction"/>
</HBox>
</VBox>
{
"lists" : [ {
"name" : "todo",
"deadline" : "2020-10-01T14:53:11",
"items" : [ {
"text" : "Item 1",
"checked" : true
}, {
"text" : "Item 2",
"checked" : false
} ]
} ]
}
\ No newline at end of file
......@@ -142,5 +142,6 @@
<module>fxui</module>
<module>restapi</module>
<module>restserver</module>
<module>integrationtests</module>
</modules>
</project>
package todolist.restapi;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import todolist.core.TodoList;
import todolist.core.TodoModel;
......
......@@ -10,7 +10,7 @@
</parent>
<artifactId>restserver</artifactId>
<packaging>war</packaging>
<packaging>jar</packaging>
<properties>
<slf4jVersion>1.7.25</slf4jVersion>
......@@ -169,33 +169,15 @@
<artifactId>jacoco-maven-plugin</artifactId>
</plugin>
<!--
https://pragmaticintegrator.wordpress.com/2010/10/22/using-a-war-module-as-dependency-in-maven/
-->
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.4.31.v20200723</version>
<artifactId>maven-war-plugin</artifactId>
<version>2.1-beta-1</version>
<configuration>
<httpConnector>
<port>8999</port>
</httpConnector>
<stopKey>quit</stopKey>
<stopPort>9000</stopPort>
<attachClasses>true</attachClasses>
</configuration>
<executions>
<execution>
<id>start-jetty</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>stop-jetty</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
......
......@@ -2,6 +2,7 @@ package todolist.restserver;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import org.glassfish.hk2.utilities.binding.AbstractBinder;
import org.glassfish.jersey.jackson.JacksonFeature;
import org.glassfish.jersey.server.ResourceConfig;
......@@ -51,7 +52,7 @@ public class TodoConfig extends ResourceConfig {
TodoPersistence todoPersistence = new TodoPersistence();
try {
return todoPersistence
.readTodoModel(new InputStreamReader(TodoConfig.class.getResourceAsStream("default-todomodel.json")));
.readTodoModel(new InputStreamReader(TodoConfig.class.getResourceAsStream("default-todomodel.json"), StandardCharsets.UTF_8));
} catch (IOException e) {
System.out.println("Couldn't read default-todomodel.json, so rigging TodoModel manually (" + e + ")");
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment