Tony CHEMIT pushed to branch develop-9.3.x at ultreiaio / ird-observe

Commits:

12 changed files:

Changes:

  • client/core/src/main/java/fr/ird/observe/client/datasource/api/ObserveDataSourcesManager.java
    ... ... @@ -38,6 +38,8 @@ import fr.ird.observe.decoration.DecoratorService;
    38 38
     import fr.ird.observe.navigation.id.Project;
    
    39 39
     import fr.ird.observe.server.security.InvalidAuthenticationTokenException;
    
    40 40
     import fr.ird.observe.services.ObserveServiceMainFactory;
    
    41
    +import io.ultreia.java4all.bean.AbstractJavaBean;
    
    42
    +import io.ultreia.java4all.bean.spi.GenerateJavaBeanDefinition;
    
    41 43
     import io.ultreia.java4all.i18n.I18n;
    
    42 44
     import io.ultreia.java4all.util.sql.SqlScript;
    
    43 45
     import org.apache.logging.log4j.LogManager;
    
    ... ... @@ -60,9 +62,11 @@ import java.util.UUID;
    60 62
      * @author Tony Chemit - dev@tchemit.fr
    
    61 63
      * @since 5.0
    
    62 64
      */
    
    63
    -public class ObserveDataSourcesManager implements Closeable {
    
    65
    +@GenerateJavaBeanDefinition
    
    66
    +public class ObserveDataSourcesManager extends AbstractJavaBean implements Closeable {
    
    64 67
     
    
    65 68
         private static final Logger log = LogManager.getLogger(ObserveDataSourcesManager.class);
    
    69
    +    public static final String PROPERTY_MAIN_DATA_SOURCE = "mainDataSource";
    
    66 70
         private final List<ObserveSwingDataSource> dataSources = new LinkedList<>();
    
    67 71
         private final ClientConfig config;
    
    68 72
         private final ObserveServiceMainFactory serviceFactory;
    
    ... ... @@ -149,6 +153,7 @@ public class ObserveDataSourcesManager implements Closeable {
    149 153
     
    
    150 154
         public void setMainDataSource(ObserveSwingDataSource dataSource) {
    
    151 155
             this.dataSource = dataSource;
    
    156
    +        firePropertyChange(PROPERTY_MAIN_DATA_SOURCE, dataSource);
    
    152 157
         }
    
    153 158
     
    
    154 159
         public Optional<ObserveSwingDataSource> getOptionalMainDataSource() {
    

  • client/datasource/editor/ps/src/main/java/fr/ird/observe/client/datasource/editor/ps/data/dcp/presets/FloatingObjectPresetsUIBodyContent.java
    ... ... @@ -24,6 +24,7 @@ package fr.ird.observe.client.datasource.editor.ps.data.dcp.presets;
    24 24
     
    
    25 25
     import com.google.auto.service.AutoService;
    
    26 26
     import fr.ird.observe.client.ObserveSwingApplicationContext;
    
    27
    +import fr.ird.observe.client.datasource.api.ObserveDataSourcesManager;
    
    27 28
     import fr.ird.observe.client.datasource.api.ObserveSwingDataSource;
    
    28 29
     import fr.ird.observe.client.datasource.editor.ps.data.dcp.actions.ShowFloatingObjectPresetsUI;
    
    29 30
     import fr.ird.observe.client.main.ObserveMainUI;
    
    ... ... @@ -31,6 +32,7 @@ import fr.ird.observe.client.main.body.MainUIBodyContent;
    31 32
     import io.ultreia.java4all.util.SingletonSupplier;
    
    32 33
     import org.nuiton.jaxx.runtime.context.JAXXInitialContext;
    
    33 34
     
    
    35
    +import javax.swing.JMenuItem;
    
    34 36
     import java.util.function.Supplier;
    
    35 37
     
    
    36 38
     /**
    
    ... ... @@ -65,6 +67,13 @@ public class FloatingObjectPresetsUIBodyContent extends MainUIBodyContent<Floati
    65 67
         @Override
    
    66 68
         public void install(ObserveMainUI mainUI) {
    
    67 69
             super.install(mainUI);
    
    68
    -        ShowFloatingObjectPresetsUI.init(mainUI, mainUI.getShowFloatingObjectPresets(), new ShowFloatingObjectPresetsUI());
    
    70
    +        ObserveSwingApplicationContext applicationContext = (ObserveSwingApplicationContext) ObserveSwingApplicationContext.get();
    
    71
    +        ObserveDataSourcesManager dataSourcesManager = applicationContext.getDataSourcesManager();
    
    72
    +        JMenuItem editor = mainUI.getShowFloatingObjectPresets();
    
    73
    +        dataSourcesManager.addPropertyChangeListener(ObserveDataSourcesManager.PROPERTY_MAIN_DATA_SOURCE, evt -> {
    
    74
    +            ObserveSwingDataSource newValue = (ObserveSwingDataSource) evt.getNewValue();
    
    75
    +            editor.setEnabled(newValue != null);
    
    76
    +        });
    
    77
    +        ShowFloatingObjectPresetsUI.init(mainUI, editor, new ShowFloatingObjectPresetsUI());
    
    69 78
         }
    
    70 79
     }

  • toolkit/api-report/src/main/java/fr/ird/observe/report/ReportVariableSupport.java
    ... ... @@ -64,6 +64,10 @@ public abstract class ReportVariableSupport<V, D extends ReportVariableDefinitio
    64 64
             return definition().getName();
    
    65 65
         }
    
    66 66
     
    
    67
    +    public final String getComment() {
    
    68
    +        return definition().getComment();
    
    69
    +    }
    
    70
    +
    
    67 71
         public final Class<V> getType() {
    
    68 72
             return definition().getType();
    
    69 73
         }
    

  • toolkit/api-report/src/main/java/fr/ird/observe/report/json/ReportRepeatVariableAdapter.java
    ... ... @@ -23,15 +23,10 @@ package fr.ird.observe.report.json;
    23 23
      */
    
    24 24
     
    
    25 25
     import com.google.auto.service.AutoService;
    
    26
    -import com.google.gson.JsonDeserializationContext;
    
    27
    -import com.google.gson.JsonObject;
    
    28
    -import com.google.gson.reflect.TypeToken;
    
    29 26
     import fr.ird.observe.report.ReportRepeatVariable;
    
    30 27
     import fr.ird.observe.report.definition.ReportRepeatVariableDefinition;
    
    31 28
     import io.ultreia.java4all.util.json.JsonAdapter;
    
    32 29
     
    
    33
    -import java.lang.reflect.Type;
    
    34
    -
    
    35 30
     /**
    
    36 31
      * Created on 14/12/2022.
    
    37 32
      *
    
    ... ... @@ -46,16 +41,4 @@ public class ReportRepeatVariableAdapter<V> extends ReportVariableSupportAdapter
    46 41
             return ReportRepeatVariable.class;
    
    47 42
         }
    
    48 43
     
    
    49
    -    @Override
    
    50
    -    protected Type definitionType() {
    
    51
    -        return new TypeToken<ReportRepeatVariableDefinition<V>>() {
    
    52
    -        }.getType();
    
    53
    -    }
    
    54
    -
    
    55
    -    @Override
    
    56
    -    protected ReportRepeatVariable<V> deserialize(JsonDeserializationContext context, JsonObject jsonObject, ReportRepeatVariableDefinition<V> definition) {
    
    57
    -        ReportRepeatVariable<V> result = new ReportRepeatVariable<>(definition);
    
    58
    -        deserializeValues(context, jsonObject, result);
    
    59
    -        return result;
    
    60
    -    }
    
    61 44
     }

  • toolkit/api-report/src/main/java/fr/ird/observe/report/json/ReportVariableAdapter.java
    ... ... @@ -27,13 +27,11 @@ import com.google.auto.service.AutoService;
    27 27
     import com.google.gson.JsonDeserializationContext;
    
    28 28
     import com.google.gson.JsonElement;
    
    29 29
     import com.google.gson.JsonObject;
    
    30
    -import com.google.gson.reflect.TypeToken;
    
    30
    +import com.google.gson.JsonSerializationContext;
    
    31 31
     import fr.ird.observe.report.ReportVariable;
    
    32 32
     import fr.ird.observe.report.definition.ReportVariableDefinition;
    
    33 33
     import io.ultreia.java4all.util.json.JsonAdapter;
    
    34 34
     
    
    35
    -import java.lang.reflect.Type;
    
    36
    -
    
    37 35
     /**
    
    38 36
      * @author Tony Chemit - dev@tchemit.fr
    
    39 37
      */
    
    ... ... @@ -46,22 +44,24 @@ public class ReportVariableAdapter<V> extends ReportVariableSupportAdapter<V, Re
    46 44
         }
    
    47 45
     
    
    48 46
         @Override
    
    49
    -    protected Type definitionType() {
    
    50
    -        return new TypeToken<ReportVariableDefinition<V>>() {
    
    51
    -        }.getType();
    
    47
    +    protected void deserialize(JsonDeserializationContext context, JsonObject jsonObject, ReportVariable<V> variable) {
    
    48
    +        super.deserialize(context, jsonObject, variable);
    
    49
    +        deserializeSelectedValue(context, jsonObject, variable);
    
    52 50
         }
    
    53 51
     
    
    54 52
         @Override
    
    55
    -    protected ReportVariable<V> deserialize(JsonDeserializationContext context, JsonObject jsonObject, ReportVariableDefinition<V> definition) {
    
    56
    -        ReportVariable<V> result = new ReportVariable<>(definition);
    
    57
    -        deserializeValues(context, jsonObject, result);
    
    58
    -        deserializeSelectedValue(context, jsonObject, result);
    
    59
    -        return result;
    
    53
    +    public void serialize(ReportVariable<V> src, JsonSerializationContext context, JsonObject jsonObject) {
    
    54
    +        super.serialize(src, context, jsonObject);
    
    55
    +        if (src.getSelectedValue() != null) {
    
    56
    +            jsonObject.add(ReportVariable.PROPERTY_SELECTED_VALUE, context.serialize(src.getSelectedValue()));
    
    57
    +        }
    
    60 58
         }
    
    61 59
     
    
    62 60
         protected void deserializeSelectedValue(JsonDeserializationContext context, JsonObject jsonObject, ReportVariable<V> variable) {
    
    63 61
             JsonElement json = jsonObject.get(ReportVariable.PROPERTY_SELECTED_VALUE);
    
    64
    -        V selectedValue = deserializeValue(context, variable.getType(), json);
    
    65
    -        variable.setSelectedValue(selectedValue);
    
    62
    +        if (json != null) {
    
    63
    +            V selectedValue = deserializeValue(context, variable.getType(), json);
    
    64
    +            variable.setSelectedValue(selectedValue);
    
    65
    +        }
    
    66 66
         }
    
    67 67
     }

  • toolkit/api-report/src/main/java/fr/ird/observe/report/json/ReportVariableSupportAdapter.java
    ... ... @@ -28,6 +28,9 @@ import com.google.gson.JsonDeserializer;
    28 28
     import com.google.gson.JsonElement;
    
    29 29
     import com.google.gson.JsonObject;
    
    30 30
     import com.google.gson.JsonParseException;
    
    31
    +import com.google.gson.JsonSerializationContext;
    
    32
    +import com.google.gson.JsonSerializer;
    
    33
    +import com.google.gson.reflect.TypeToken;
    
    31 34
     import fr.ird.observe.report.ReportVariableSupport;
    
    32 35
     import fr.ird.observe.report.definition.ReportVariableDefinitionSupport;
    
    33 36
     import io.ultreia.java4all.util.json.JsonAdapter;
    
    ... ... @@ -44,21 +47,38 @@ import java.util.Set;
    44 47
      * @author Tony Chemit - dev@tchemit.fr
    
    45 48
      * @since 9.0.22
    
    46 49
      */
    
    47
    -public abstract class ReportVariableSupportAdapter<V, D extends ReportVariableDefinitionSupport<V>, R extends ReportVariableSupport<V, D>> implements JsonAdapter, JsonDeserializer<R> {
    
    50
    +public abstract class ReportVariableSupportAdapter<V, D extends ReportVariableDefinitionSupport<V>, R extends ReportVariableSupport<V, D>> implements JsonAdapter, JsonSerializer<R>, JsonDeserializer<R> {
    
    48 51
     
    
    49
    -    protected abstract Type definitionType();
    
    52
    +    @Override
    
    53
    +    public JsonElement serialize(R src, Type typeOfSrc, JsonSerializationContext context) {
    
    54
    +        JsonObject jsonObject = new JsonObject();
    
    55
    +        jsonObject.add("V", context.serialize(src.definition().getType()));
    
    56
    +        jsonObject.add("D", context.serialize(src.definition().getClass()));
    
    57
    +        serialize(src, context, jsonObject);
    
    58
    +        return jsonObject;
    
    59
    +    }
    
    50 60
     
    
    51
    -    protected abstract R deserialize(JsonDeserializationContext context, JsonObject jsonObject, D definition);
    
    61
    +    protected void serialize(R src, JsonSerializationContext context, JsonObject jsonObject) {
    
    62
    +        jsonObject.add(ReportVariableSupport.PROPERTY_DEFINITION, context.serialize(src.definition()));
    
    63
    +        jsonObject.add(ReportVariableSupport.PROPERTY_VALUES, context.serialize(src.getValues()));
    
    64
    +    }
    
    52 65
     
    
    53 66
         @Override
    
    54 67
         public final R deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    
    55 68
             JsonObject jsonObject = json.getAsJsonObject();
    
    56
    -        D definition = deserializeDefinition(context, jsonObject);
    
    57
    -        return deserialize(context, jsonObject, definition);
    
    69
    +        Class<V> vType = context.deserialize(jsonObject.get("V"), Class.class);
    
    70
    +        Class<D> dType = context.deserialize(jsonObject.get("D"), Class.class);
    
    71
    +        Type definitionType = TypeToken.getParameterized(dType, vType).getType();
    
    72
    +
    
    73
    +        D definition = context.deserialize(jsonObject.get(ReportVariableSupport.PROPERTY_DEFINITION), definitionType);
    
    74
    +        @SuppressWarnings("unchecked") R variable = (R) definition.toVariable();
    
    75
    +
    
    76
    +        deserialize(context, jsonObject, variable);
    
    77
    +        return variable;
    
    58 78
         }
    
    59 79
     
    
    60
    -    protected D deserializeDefinition(JsonDeserializationContext context, JsonObject jsonObject) {
    
    61
    -        return context.deserialize(jsonObject.get(ReportVariableSupport.PROPERTY_DEFINITION), definitionType());
    
    80
    +    protected void deserialize(JsonDeserializationContext context, JsonObject jsonObject, R variable) {
    
    81
    +        deserializeValues(context, jsonObject, variable);
    
    62 82
         }
    
    63 83
     
    
    64 84
         protected void deserializeValues(JsonDeserializationContext context, JsonObject jsonObject, R variable) {
    

  • toolkit/api-report/src/main/java/fr/ird/observe/report/renderers/HighlightIfAbsoluteDeltaIsPositive.java
    ... ... @@ -31,6 +31,7 @@ import org.jdesktop.swingx.decorator.ComponentAdapter;
    31 31
     import org.jdesktop.swingx.decorator.HighlightPredicate;
    
    32 32
     
    
    33 33
     import java.awt.Component;
    
    34
    +import java.util.Objects;
    
    34 35
     
    
    35 36
     import static fr.ird.observe.report.renderers.HighlightIfAbsoluteDeltaIsPositive.Parameters;
    
    36 37
     
    
    ... ... @@ -116,6 +117,19 @@ public class HighlightIfAbsoluteDeltaIsPositive implements ColumnRendererConsume
    116 117
             public double getErrorThreshHold() {
    
    117 118
                 return errorThreshHold;
    
    118 119
             }
    
    120
    +
    
    121
    +        @Override
    
    122
    +        public boolean equals(Object o) {
    
    123
    +            if (this == o) return true;
    
    124
    +            if (!(o instanceof Parameters)) return false;
    
    125
    +            Parameters that = (Parameters) o;
    
    126
    +            return getColumn1() == that.getColumn1() && getColumn2() == that.getColumn2() && Double.compare(getWarningThreshHold(), that.getWarningThreshHold()) == 0 && Double.compare(getErrorThreshHold(), that.getErrorThreshHold()) == 0;
    
    127
    +        }
    
    128
    +
    
    129
    +        @Override
    
    130
    +        public int hashCode() {
    
    131
    +            return Objects.hash(getColumn1(), getColumn2(), getWarningThreshHold(), getErrorThreshHold());
    
    132
    +        }
    
    119 133
         }
    
    120 134
     
    
    121 135
         static class ParametersHighlightPredicate implements HighlightPredicate {
    

  • toolkit/api-report/src/main/java/fr/ird/observe/report/renderers/HighlightIfEquals18nReferentialValue.java
    ... ... @@ -155,5 +155,18 @@ public class HighlightIfEquals18nReferentialValue implements ColumnRendererConsu
    155 155
             public Parameters setLabel(String labelError, String labelOk) {
    
    156 156
                 return new Parameters(columns, idError, idOk, labelError, labelOk);
    
    157 157
             }
    
    158
    +
    
    159
    +        @Override
    
    160
    +        public boolean equals(Object o) {
    
    161
    +            if (this == o) return true;
    
    162
    +            if (!(o instanceof Parameters)) return false;
    
    163
    +            Parameters that = (Parameters) o;
    
    164
    +            return Objects.equals(getColumns(), that.getColumns()) && Objects.equals(getIdError(), that.getIdError()) && Objects.equals(getIdOk(), that.getIdOk()) && Objects.equals(getLabelError(), that.getLabelError()) && Objects.equals(getLabelOk(), that.getLabelOk());
    
    165
    +        }
    
    166
    +
    
    167
    +        @Override
    
    168
    +        public int hashCode() {
    
    169
    +            return Objects.hash(getColumns(), getIdError(), getIdOk(), getLabelError(), getLabelOk());
    
    170
    +        }
    
    158 171
         }
    
    159 172
     }

  • toolkit/api-report/src/main/java/fr/ird/observe/report/renderers/HighlightIfNumericalValueIsPositive.java
    ... ... @@ -31,6 +31,7 @@ import org.jdesktop.swingx.decorator.ComponentAdapter;
    31 31
     import org.jdesktop.swingx.decorator.HighlightPredicate;
    
    32 32
     
    
    33 33
     import java.awt.Component;
    
    34
    +import java.util.Objects;
    
    34 35
     
    
    35 36
     import static fr.ird.observe.report.renderers.HighlightIfNumericalValueIsPositive.Parameters;
    
    36 37
     
    
    ... ... @@ -110,6 +111,19 @@ public class HighlightIfNumericalValueIsPositive implements ColumnRendererConsum
    110 111
             public double getWarningThreshHold() {
    
    111 112
                 return warningThreshHold;
    
    112 113
             }
    
    114
    +
    
    115
    +        @Override
    
    116
    +        public boolean equals(Object o) {
    
    117
    +            if (this == o) return true;
    
    118
    +            if (!(o instanceof Parameters)) return false;
    
    119
    +            Parameters that = (Parameters) o;
    
    120
    +            return getColumn() == that.getColumn() && Double.compare(getWarningThreshHold(), that.getWarningThreshHold()) == 0 && Double.compare(getErrorThreshHold(), that.getErrorThreshHold()) == 0;
    
    121
    +        }
    
    122
    +
    
    123
    +        @Override
    
    124
    +        public int hashCode() {
    
    125
    +            return Objects.hash(getColumn(), getWarningThreshHold(), getErrorThreshHold());
    
    126
    +        }
    
    113 127
         }
    
    114 128
     
    
    115 129
         static class ParametersHighlightPredicate implements HighlightPredicate {
    

  • toolkit/api-report/src/test/java/fr/ird/observe/report/json/ReportColumnRenderersParametersAdapterTest.java
    1
    +package fr.ird.observe.report.json;
    
    2
    +
    
    3
    +/*-
    
    4
    + * #%L
    
    5
    + * ObServe Toolkit :: API :: Report
    
    6
    + * %%
    
    7
    + * Copyright (C) 2008 - 2024 IRD, Ultreia.io
    
    8
    + * %%
    
    9
    + * This program is free software: you can redistribute it and/or modify
    
    10
    + * it under the terms of the GNU General Public License as
    
    11
    + * published by the Free Software Foundation, either version 3 of the
    
    12
    + * License, or (at your option) any later version.
    
    13
    + *
    
    14
    + * This program is distributed in the hope that it will be useful,
    
    15
    + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    
    16
    + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    
    17
    + * GNU General Public License for more details.
    
    18
    + *
    
    19
    + * You should have received a copy of the GNU General Public
    
    20
    + * License along with this program.  If not, see
    
    21
    + * <http://www.gnu.org/licenses/gpl-3.0.html>.
    
    22
    + * #L%
    
    23
    + */
    
    24
    +
    
    25
    +import com.google.gson.Gson;
    
    26
    +import com.google.gson.GsonBuilder;
    
    27
    +import fr.ird.observe.report.ReportColumnRenderersParameters;
    
    28
    +import fr.ird.observe.report.renderers.HighlightIfAbsoluteDeltaIsPositive;
    
    29
    +import fr.ird.observe.report.renderers.HighlightIfEquals18nReferentialValue;
    
    30
    +import fr.ird.observe.report.renderers.HighlightIfNumericalValueIsPositive;
    
    31
    +import io.ultreia.java4all.util.json.JsonAdapterProvider;
    
    32
    +import org.junit.Assert;
    
    33
    +import org.junit.Test;
    
    34
    +
    
    35
    +import java.util.List;
    
    36
    +
    
    37
    +/**
    
    38
    + * Created at 19/09/2024.
    
    39
    + *
    
    40
    + * @author Tony Chemit - dev@tchemit.fr
    
    41
    + * @since 9.3.7
    
    42
    + */
    
    43
    +public class ReportColumnRenderersParametersAdapterTest {
    
    44
    +
    
    45
    +    private static void assertEquals(ReportColumnRenderersParameters expected, ReportColumnRenderersParameters actual) {
    
    46
    +        Assert.assertEquals(expected.getColumnRendererParameters(), actual.getColumnRendererParameters());
    
    47
    +    }
    
    48
    +
    
    49
    +    @Test
    
    50
    +    public void test() {
    
    51
    +
    
    52
    +        GsonBuilder gsonBuilder = new GsonBuilder().serializeNulls().setPrettyPrinting();
    
    53
    +        JsonAdapterProvider.offers(gsonBuilder::registerTypeAdapter, gsonBuilder::registerTypeHierarchyAdapter);
    
    54
    +        Gson gson = gsonBuilder.create();
    
    55
    +
    
    56
    +        ReportColumnRenderersParameters expected = new ReportColumnRenderersParameters(List.of(
    
    57
    +                new HighlightIfAbsoluteDeltaIsPositive().parseParameters("0|1|0.5f|0.1f"),
    
    58
    +                new HighlightIfEquals18nReferentialValue().parseParameters("0,1,2|ok|error"),
    
    59
    +                new HighlightIfNumericalValueIsPositive().parseParameters("0|1.0f|2.5f")
    
    60
    +        ));
    
    61
    +
    
    62
    +        String json = gson.toJson(expected);
    
    63
    +        ReportColumnRenderersParameters actual = gson.fromJson(json, ReportColumnRenderersParameters.class);
    
    64
    +        assertEquals(expected, actual);
    
    65
    +    }
    
    66
    +}

  • toolkit/api-report/src/test/java/fr/ird/observe/report/json/ReportRepeatVariableAdapterTest.java
    1
    +package fr.ird.observe.report.json;
    
    2
    +
    
    3
    +/*-
    
    4
    + * #%L
    
    5
    + * ObServe Toolkit :: API :: Report
    
    6
    + * %%
    
    7
    + * Copyright (C) 2008 - 2024 IRD, Ultreia.io
    
    8
    + * %%
    
    9
    + * This program is free software: you can redistribute it and/or modify
    
    10
    + * it under the terms of the GNU General Public License as
    
    11
    + * published by the Free Software Foundation, either version 3 of the
    
    12
    + * License, or (at your option) any later version.
    
    13
    + *
    
    14
    + * This program is distributed in the hope that it will be useful,
    
    15
    + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    
    16
    + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    
    17
    + * GNU General Public License for more details.
    
    18
    + *
    
    19
    + * You should have received a copy of the GNU General Public
    
    20
    + * License along with this program.  If not, see
    
    21
    + * <http://www.gnu.org/licenses/gpl-3.0.html>.
    
    22
    + * #L%
    
    23
    + */
    
    24
    +
    
    25
    +import com.google.gson.Gson;
    
    26
    +import com.google.gson.GsonBuilder;
    
    27
    +import com.google.gson.reflect.TypeToken;
    
    28
    +import fr.ird.observe.report.ReportRepeatVariable;
    
    29
    +import fr.ird.observe.report.definition.ReportRepeatVariableDefinition;
    
    30
    +import io.ultreia.java4all.util.json.JsonAdapterProvider;
    
    31
    +import org.junit.Assert;
    
    32
    +import org.junit.Test;
    
    33
    +
    
    34
    +import java.util.Set;
    
    35
    +
    
    36
    +/**
    
    37
    + * Created at 19/09/2024.
    
    38
    + *
    
    39
    + * @author Tony Chemit - dev@tchemit.fr
    
    40
    + * @since 9.3.7
    
    41
    + */
    
    42
    +public class ReportRepeatVariableAdapterTest {
    
    43
    +
    
    44
    +    private static <V> void assertEquals(ReportRepeatVariable<V> expected, ReportRepeatVariable<V> actual) {
    
    45
    +        Assert.assertEquals(expected.definition(), actual.definition());
    
    46
    +        Assert.assertEquals(expected.getName(), actual.getName());
    
    47
    +        Assert.assertEquals(expected.getRequest(), actual.getRequest());
    
    48
    +        Assert.assertEquals(expected.getComment(), actual.getComment());
    
    49
    +        Assert.assertEquals(expected.getType(), actual.getType());
    
    50
    +        Assert.assertEquals(expected.getValues(), actual.getValues());
    
    51
    +        Assert.assertEquals(expected.isAddNullValue(), actual.isAddNullValue());
    
    52
    +    }
    
    53
    +
    
    54
    +    @Test
    
    55
    +    public void test() {
    
    56
    +
    
    57
    +        GsonBuilder gsonBuilder = new GsonBuilder().serializeNulls().setPrettyPrinting();
    
    58
    +        JsonAdapterProvider.offers(gsonBuilder::registerTypeAdapter, gsonBuilder::registerTypeHierarchyAdapter);
    
    59
    +        Gson gson = gsonBuilder.create();
    
    60
    +
    
    61
    +        ReportRepeatVariable<String> expected = new ReportRepeatVariable<>(new ReportRepeatVariableDefinition<>(
    
    62
    +                "name",
    
    63
    +                String.class,
    
    64
    +                "Request",
    
    65
    +                "Comment",
    
    66
    +                false
    
    67
    +        ));
    
    68
    +        expected.setValues(Set.of("a", "b", "c"));
    
    69
    +
    
    70
    +        String json = gson.toJson(expected);
    
    71
    +        ReportRepeatVariable<String> actual = gson.fromJson(json, TypeToken.getParameterized(ReportRepeatVariable.class, String.class).getType());
    
    72
    +        assertEquals(expected, actual);
    
    73
    +    }
    
    74
    +}

  • toolkit/api-report/src/test/java/fr/ird/observe/report/json/ReportVariableAdapterTest.java
    1
    +package fr.ird.observe.report.json;
    
    2
    +
    
    3
    +/*-
    
    4
    + * #%L
    
    5
    + * ObServe Toolkit :: API :: Report
    
    6
    + * %%
    
    7
    + * Copyright (C) 2008 - 2024 IRD, Ultreia.io
    
    8
    + * %%
    
    9
    + * This program is free software: you can redistribute it and/or modify
    
    10
    + * it under the terms of the GNU General Public License as
    
    11
    + * published by the Free Software Foundation, either version 3 of the
    
    12
    + * License, or (at your option) any later version.
    
    13
    + *
    
    14
    + * This program is distributed in the hope that it will be useful,
    
    15
    + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    
    16
    + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    
    17
    + * GNU General Public License for more details.
    
    18
    + *
    
    19
    + * You should have received a copy of the GNU General Public
    
    20
    + * License along with this program.  If not, see
    
    21
    + * <http://www.gnu.org/licenses/gpl-3.0.html>.
    
    22
    + * #L%
    
    23
    + */
    
    24
    +
    
    25
    +import com.google.gson.Gson;
    
    26
    +import com.google.gson.GsonBuilder;
    
    27
    +import com.google.gson.reflect.TypeToken;
    
    28
    +import fr.ird.observe.report.ReportVariable;
    
    29
    +import fr.ird.observe.report.definition.ReportVariableDefinition;
    
    30
    +import io.ultreia.java4all.util.json.JsonAdapterProvider;
    
    31
    +import org.junit.Assert;
    
    32
    +import org.junit.Test;
    
    33
    +
    
    34
    +import java.lang.reflect.Type;
    
    35
    +import java.util.Set;
    
    36
    +
    
    37
    +/**
    
    38
    + * Created at 19/09/2024.
    
    39
    + *
    
    40
    + * @author Tony Chemit - dev@tchemit.fr
    
    41
    + * @since 9.3.7
    
    42
    + */
    
    43
    +public class ReportVariableAdapterTest {
    
    44
    +
    
    45
    +    private static <V> void assertEquals(ReportVariable<V> expected, ReportVariable<V> actual) {
    
    46
    +        Assert.assertEquals(expected.definition(), actual.definition());
    
    47
    +        Assert.assertEquals(expected.getName(), actual.getName());
    
    48
    +        Assert.assertEquals(expected.getRequest(), actual.getRequest());
    
    49
    +        Assert.assertEquals(expected.getComment(), actual.getComment());
    
    50
    +        Assert.assertEquals(expected.getType(), actual.getType());
    
    51
    +        Assert.assertEquals(expected.getValues(), actual.getValues());
    
    52
    +        Assert.assertEquals(expected.getSelectedValue(), actual.getSelectedValue());
    
    53
    +    }
    
    54
    +
    
    55
    +    @Test
    
    56
    +    public void test() {
    
    57
    +        GsonBuilder gsonBuilder = new GsonBuilder().serializeNulls().setPrettyPrinting();
    
    58
    +        JsonAdapterProvider.offers(gsonBuilder::registerTypeAdapter, gsonBuilder::registerTypeHierarchyAdapter);
    
    59
    +        Gson gson = gsonBuilder.create();
    
    60
    +
    
    61
    +        ReportVariable<String> expected = new ReportVariable<>(new ReportVariableDefinition<>(
    
    62
    +                "name",
    
    63
    +                String.class,
    
    64
    +                "Request",
    
    65
    +                "Comment"
    
    66
    +        ));
    
    67
    +        expected.setValues(Set.of("a", "b", "c"));
    
    68
    +
    
    69
    +        String json = gson.toJson(expected);
    
    70
    +        Type type = TypeToken.getParameterized(ReportVariable.class, String.class).getType();
    
    71
    +        ReportVariable<String> actual = gson.fromJson(json, type);
    
    72
    +        Assert.assertEquals(expected, actual);
    
    73
    +
    
    74
    +        expected.setSelectedValue("a");
    
    75
    +        json = gson.toJson(expected);
    
    76
    +        actual = gson.fromJson(json, type);
    
    77
    +        assertEquals(expected, actual);
    
    78
    +
    
    79
    +    }
    
    80
    +}