From 3cf5f8ba9fe384697676a745a3ee9a763dcd6c53 Mon Sep 17 00:00:00 2001
From: Tor-Einar Skog <tor-einar.skog@nibio.no>
Date: Thu, 30 May 2024 13:16:26 +0200
Subject: [PATCH] Refactor metadata methods

---
 pom.xml                                       |  2 +-
 .../alternariamodel/AlternariaModel.java      | 40 +++++--------------
 src/main/resources/LICENSE_header.txt         | 16 ++++++++
 .../model/alternariamodel/texts.properties    | 32 ---------------
 .../model/alternariamodel/texts_nb.properties |  1 -
 src/main/resources/sample_config.json         | 31 ++++++++++++++
 .../alternariamodel/AlternariaModelTest.java  |  7 +---
 7 files changed, 60 insertions(+), 69 deletions(-)
 create mode 100644 src/main/resources/LICENSE_header.txt
 create mode 100644 src/main/resources/sample_config.json

diff --git a/pom.xml b/pom.xml
index 8998828..84e0249 100644
--- a/pom.xml
+++ b/pom.xml
@@ -17,7 +17,7 @@
         <dependency>
             <groupId>no.nibio.vips</groupId>
             <artifactId>VIPSCommon</artifactId>
-            <version>2.1.0</version>
+            <version>2.1.1</version>
         </dependency>
         <dependency>
             <groupId>org.junit.jupiter</groupId>
diff --git a/src/main/java/no/nibio/vips/model/alternariamodel/AlternariaModel.java b/src/main/java/no/nibio/vips/model/alternariamodel/AlternariaModel.java
index 4b3e53f..27eda26 100644
--- a/src/main/java/no/nibio/vips/model/alternariamodel/AlternariaModel.java
+++ b/src/main/java/no/nibio/vips/model/alternariamodel/AlternariaModel.java
@@ -39,13 +39,17 @@ package no.nibio.vips.model.alternariamodel;
 
 import com.fasterxml.jackson.core.type.TypeReference;
 import com.fasterxml.jackson.databind.ObjectMapper;
+
+import java.io.BufferedInputStream;
 import java.io.IOException;
+import java.nio.charset.StandardCharsets;
 import java.text.DecimalFormat;
 import java.util.ArrayList;
 import java.util.Calendar;
 import java.util.Collections;
 import java.util.Date;
 import java.util.List;
+import java.util.Scanner;
 import java.util.TimeZone;
 import java.util.logging.Level;
 import java.util.logging.Logger;
@@ -222,22 +226,10 @@ public class AlternariaModel extends I18nImpl implements Model {
         return this.getText("name", language);
     }
 
+
     @Override
     public String getLicense() {
-        /**
-         * method name : getLicense
-         * 
-         * @param :
-         * @return : java.lang.String
-         *
-         *         purpose :
-         *
-         *         date : Expression date is undefined on line 20, column 19 in
-         *         Templates/Classes/Code/GeneratedMethodBody. Expression time is
-         *         undefined on line 20, column 27 in
-         *         Templates/Classes/Code/GeneratedMethodBody.
-         */
-        return this.getText("license", Model.DEFAULT_LANGUAGE);
+        return this.modelUtil.getTextFromFileInJar(this.getClass(), "/LICENSE_header.txt");
     }
 
     @Override
@@ -377,26 +369,13 @@ public class AlternariaModel extends I18nImpl implements Model {
     }
 
     /**
-     * TODO
      * 
-     * @return
+     * 
+     * @return a Json configuration example
      */
     @Override
     public String getSampleConfig() {
-        /**
-         * method name : getSampleConfig
-         * 
-         * @param :
-         * @return : java.lang.String
-         *
-         *         purpose :
-         *
-         *         date : Expression date is undefined on line 20, column 19 in
-         *         Templates/Classes/Code/GeneratedMethodBody. Expression time is
-         *         undefined on line 20, column 27 in
-         *         Templates/Classes/Code/GeneratedMethodBody.
-         */
-        return this.getText("sampleconfig", Model.DEFAULT_LANGUAGE);
+        return this.modelUtil.getTextFromFileInJar(this.getClass(), "/sample_config.json");
     }
 
     /**
@@ -743,4 +722,5 @@ public class AlternariaModel extends I18nImpl implements Model {
 
         return result;
     }
+
 }
diff --git a/src/main/resources/LICENSE_header.txt b/src/main/resources/LICENSE_header.txt
new file mode 100644
index 0000000..76e92f2
--- /dev/null
+++ b/src/main/resources/LICENSE_header.txt
@@ -0,0 +1,16 @@
+
+
+    Copyright (C) 2024 Tor-Einar Skog <tor-einar.skog@nibio.no>
+
+    This program is free software: you can redistribute it and/or modify
+    it under the terms of the GNU Affero 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 Affero General Public License for more details.
+
+    You should have received a copy of the GNU Affero General Public License
+    along with this program.  If not, see <https://www.gnu.org/licenses/>.
diff --git a/src/main/resources/no/nibio/vips/model/alternariamodel/texts.properties b/src/main/resources/no/nibio/vips/model/alternariamodel/texts.properties
index e3e3495..ff1798c 100644
--- a/src/main/resources/no/nibio/vips/model/alternariamodel/texts.properties
+++ b/src/main/resources/no/nibio/vips/model/alternariamodel/texts.properties
@@ -26,35 +26,3 @@ observations - list of weather data. The following parameters are required:\n\
 * TM - Average temperature (Degrees Celcius)\n\
 * BT - Leaf wetness (Minutes/hour) (IF you don't have leaf wetness, you must provide UM (relative humidity). Then we will calculate the leaf wetness.\n\
 sprayingDate - (Optional) Farmers can choose multiple spraying dates (upto four spraying dates).
-license= Copyright (c) 2020 NIBIO <http://www.nibio.no/>. \n            \nThis file is part of AlternariaModel.  \nThis program is free software: you can redistribute it and/or modify\nit under the terms of the GNU Affero General Public License as published by\nthe Free Software Foundation, either version 3 of the License, or\n(at your option) any later version.\n\nThis program is distributed in the hope that it will be useful,\nbut WITHOUT ANY WARRANTY; without even the implied warranty of\nMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\nGNU Affero General Public License for more details.\n\nYou should have received a copy of the GNU Affero General Public License\nalong with this program.  If not, see <https://www.gnu.org/licenses/>.
-
-sampleconfig={                                                          \n\
-\t    "loginInfo": {                                                      \n\
-\t\t        "username": "example",                                          \n\
-\t\t        "password": "example"                                           \n\
-\t    },                                                                  \n\
-\t    "modelId": "ALTERNARIA",                                            \n\
-\t    "configParameters": {                                               \n\
-\t        "timeZone": "Europe/Oslo",                                      \n\
-\t        "observations": [                                               \n\
-\t            {                                                           \n\
-\t\t                "elementMeasurementTypeId": "TM",                       \n\
-\t\t                "logIntervalId": 1,                                     \n\
-\t\t                "timeMeasured": "2018-08-11T00:00:00+02:00",            \n\
-\t\t                "value": 10.46                                          \n\
-\t            },                                                          \n\
-\t            {                                                           \n\
-\t\t                "elementMeasurementTypeId": "BT",                       \n\
-\t\t                "logIntervalId": 1,                                     \n\
-\t\t                "timeMeasured": "2018-08-11T00:00:00+02:00",            \n\
-\t\t                "value": 60.0                                           \n\
-\t            }                                                           \n\
-\t        ],                                                               \n\
-\t        "sprayingDates": [                                                \n\
-\t\t            "2018-07-02T22:00:00Z",                                     \n\
-\t\t            "2018-07-30T22:00:00Z",                                     \n\
-\t\t            "2018-09-04T22:00:00Z"                                      \n\
-\t        ]                                                                 \n\
-\t    }                                                                   \n\
- }                                                                      \n\
-
diff --git a/src/main/resources/no/nibio/vips/model/alternariamodel/texts_nb.properties b/src/main/resources/no/nibio/vips/model/alternariamodel/texts_nb.properties
index b79332c..0fa4f36 100644
--- a/src/main/resources/no/nibio/vips/model/alternariamodel/texts_nb.properties
+++ b/src/main/resources/no/nibio/vips/model/alternariamodel/texts_nb.properties
@@ -19,4 +19,3 @@ name=TOMCAST (FAST - Dew) modell for Alternaria
 description=  <p>{{filename="/images/ef-20200421-altenaria.jpg" description="Photo: Bilde: S. Abrahamsen, NLR"}}</p>\n<p>TOMCAST er basert p&aring; en modell utviklet for varsling av bladflekksjukdommer i tomat. Versjonen som blir brukt her er testet og tilpasset t&oslash;rrflekksjuke i potet (Alternaria solani) i Danmark. Modellen beregner daglige risikoverdier (DSV: Disease Severity Value) basert p&aring; temperatur og bladfuktighet siste d&oslash;gn. DSV representerer risikoen for angrep av t&oslash;rrflekksjuke siste d&oslash;gn. Daglige DSV-verdier blir summert frem til en terskel er n&aring;dd og behandling er anbefalt. Modellen er laget slik at akkumulering av DSV nullstilles n&aring;r spr&oslash;yting gjennomf&oslash;res, og akkumulering starter p&aring; nytt fra 0.</p>\n<p>Modellen er under utpr&oslash;ving i 2020, og vil bli testet b&aring;de i gulrot og i potet. Det vil kun v&aelig;re mulig &aring; starte private varler for denne modellen. Basert p&aring; erfaringer fra andre land er terskelverdien for r&oslash;dt varsel satt til 20.</p>\n<h3>Oppstart av varsel</h3>\n<p>Potetplanter varierer i mottakelighet for t&oslash;rrflekksjuke gjennom sesongen, og de blir gradvis mer og mer mottakelige. Dette betyr at behovet for beskyttelse ogs&aring; varierer tilsvarende. Modellen startes n&aring;r man ser de f&oslash;rste symptomene p&aring; t&oslash;rrflekksjuke og gjennomf&oslash;rer f&oslash;rste behandling (under 0,1% angrep).</p>\n<h3>Tolking av varsel</h3>\n<p>Modellen vil vise gr&oslash;nt (ingen fare) frem til den akkumulerte risikoverdien er 15. Fra denne verdien frem til terskelverdien (20) vil varselet v&aelig;re gult (mulig fare), og deretter g&aring; over til r&oslash;dt (fare).</p>
 statusInterpretation=Modellen vil vise gr\u00f8nt (ingen fare) frem til den akkumulerte risikoverdien er 15. Fra denne verdien frem til terskelverdien (20) vil varselet v\u00e6re gult (mulig fare), og deretter g\u00e5 over til r\u00f8dt (fare).
 usage=TODO
-license=Copyright (c) 2020 NIBIO <http://www.nibio.no/>. \n            \nThis file is part of AlternariaModel.  \nThis program is free software: you can redistribute it and/or modify\nit under the terms of the GNU Affero General Public License as published by\nthe Free Software Foundation, either version 3 of the License, or\n(at your option) any later version.\n\nThis program is distributed in the hope that it will be useful,\nbut WITHOUT ANY WARRANTY; without even the implied warranty of\nMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\nGNU Affero General Public License for more details.\n\nYou should have received a copy of the GNU Affero General Public License\nalong with this program.  If not, see <https://www.gnu.org/licenses/>.
diff --git a/src/main/resources/sample_config.json b/src/main/resources/sample_config.json
new file mode 100644
index 0000000..f71ed86
--- /dev/null
+++ b/src/main/resources/sample_config.json
@@ -0,0 +1,31 @@
+{
+    "loginInfo": {
+        "username": "example",
+        "password": "example"
+    },
+    "modelId": "ALTERNARIA",
+    "configParameters": {
+        "sprayingDates": [
+            "2018-07-02T22:00:00Z",
+            "2018-07-30T22:00:00Z",
+            "2018-09-04T22:00:00Z"
+        ],
+        "thresholdDSVTempMin": "10.0",
+        "thresholdDSVMax": "20.0",
+        "timeZone": "Europe/Oslo",
+        "observations": [
+            {
+                "elementMeasurementTypeId": "TM",
+                "logIntervalId": 1,
+                "timeMeasured": "2018-08-11T00:00:00+02:00",
+                "value": 10.46
+            },
+            {
+                "elementMeasurementTypeId": "BT",
+                "logIntervalId": 1,
+                "timeMeasured": "2018-08-11T00:00:00+02:00",
+                "value": 60
+            }
+        ]
+    }
+}
\ No newline at end of file
diff --git a/src/test/java/no/nibio/vips/model/alternariamodel/AlternariaModelTest.java b/src/test/java/no/nibio/vips/model/alternariamodel/AlternariaModelTest.java
index e4b6a86..001010c 100644
--- a/src/test/java/no/nibio/vips/model/alternariamodel/AlternariaModelTest.java
+++ b/src/test/java/no/nibio/vips/model/alternariamodel/AlternariaModelTest.java
@@ -246,9 +246,8 @@ public class AlternariaModelTest {
     public void testGetLicense() {
         System.out.println("getLicense");
         AlternariaModel instance = new AlternariaModel();
-        String expResult = "";
         String result = instance.getLicense();
-        assertNotNull(result);
+        assertTrue(result.contains("GNU Affero"));
     }
 
     /**
@@ -348,15 +347,13 @@ public class AlternariaModelTest {
 
     /**
      * Test of getSampleConfig method, of class AlternariaModel.
-     * TODO - Recheck the functionality
      */
     @org.junit.jupiter.api.Test
     public void testGetSampleConfig() {
         System.out.println("getSampleConfig");
         AlternariaModel instance = new AlternariaModel();
-        String expResult = "";
         String result = instance.getSampleConfig();
-        assertNotNull(result);
+        assertTrue(result.contains("configParameters"));
     }
 
     /**
-- 
GitLab