From 28459704c66e1b8920ed29cd50db3bbdf99a7753 Mon Sep 17 00:00:00 2001 From: Matt Date: Thu, 24 Sep 2020 15:34:47 -0700 Subject: [PATCH] Fix network config "Supported" bug (#130) * Fix network config bug * Use Jackson instead of hacky solution for management * run spotless * Add assertion --- .../common/configuration/NetworkConfig.java | 34 +++++++++++++------ .../configuration/NetworkConfigTest.java | 34 +++++++++++++++++++ 2 files changed, 57 insertions(+), 11 deletions(-) create mode 100644 photon-server/src/test/java/org/photonvision/common/configuration/NetworkConfigTest.java diff --git a/photon-server/src/main/java/org/photonvision/common/configuration/NetworkConfig.java b/photon-server/src/main/java/org/photonvision/common/configuration/NetworkConfig.java index 4f972bf40..2ca80278e 100644 --- a/photon-server/src/main/java/org/photonvision/common/configuration/NetworkConfig.java +++ b/photon-server/src/main/java/org/photonvision/common/configuration/NetworkConfig.java @@ -17,6 +17,10 @@ package org.photonvision.common.configuration; +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonGetter; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonSetter; import java.util.HashMap; import java.util.Map; import org.photonvision.common.hardware.Platform; @@ -29,24 +33,26 @@ public class NetworkConfig { public String hostname = "photonvision"; public boolean runNTServer = false; - public boolean shouldManage; + private boolean shouldManage; - public NetworkConfig() {} + public NetworkConfig() { + setShouldManage(false); + } + @JsonCreator public NetworkConfig( - int teamNumber, - NetworkMode connectionType, - String staticIp, - String hostname, - boolean runNTServer, - boolean shouldManage) { + @JsonProperty("teamNumber") int teamNumber, + @JsonProperty("connectionType") NetworkMode connectionType, + @JsonProperty("staticIp") String staticIp, + @JsonProperty("hostname") String hostname, + @JsonProperty("runNTServer") boolean runNTServer, + @JsonProperty("shouldManage") boolean shouldManage) { this.teamNumber = teamNumber; this.connectionType = connectionType; this.staticIp = staticIp; this.hostname = hostname; this.runNTServer = runNTServer; - - this.shouldManage = shouldManage; + setShouldManage(shouldManage); } public static NetworkConfig fromHashMap(Map map) { @@ -54,11 +60,11 @@ public class NetworkConfig { // staticIp (str), netmask (str), hostname (str) var ret = new NetworkConfig(); ret.teamNumber = Integer.parseInt(map.get("teamNumber").toString()); - ret.shouldManage = (Boolean) map.get("supported"); ret.connectionType = NetworkMode.values()[(Integer) map.get("connectionType")]; ret.staticIp = (String) map.get("staticIp"); ret.hostname = (String) map.get("hostname"); ret.runNTServer = (Boolean) map.get("runNTServer"); + ret.setShouldManage((Boolean) map.get("supported")); return ret; } @@ -73,7 +79,13 @@ public class NetworkConfig { return tmp; } + @JsonGetter("shouldManage") public boolean shouldManage() { return this.shouldManage || Platform.isRaspberryPi(); } + + @JsonSetter("shouldManage") + public void setShouldManage(boolean shouldManage) { + this.shouldManage = shouldManage || Platform.isRaspberryPi(); + } } diff --git a/photon-server/src/test/java/org/photonvision/common/configuration/NetworkConfigTest.java b/photon-server/src/test/java/org/photonvision/common/configuration/NetworkConfigTest.java new file mode 100644 index 000000000..2679af6ba --- /dev/null +++ b/photon-server/src/test/java/org/photonvision/common/configuration/NetworkConfigTest.java @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2020 Photon Vision. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.photonvision.common.configuration; + +import com.fasterxml.jackson.databind.ObjectMapper; +import java.io.IOException; +import java.nio.file.Path; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class NetworkConfigTest { + @Test + public void testSerialization() throws IOException { + var mapper = new ObjectMapper(); + var path = Path.of("netTest.json"); + mapper.writeValue(path.toFile(), new NetworkConfig()); + Assertions.assertDoesNotThrow(() -> mapper.readValue(path.toFile(), NetworkConfig.class)); + } +}