|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025 Ericsson and others |
| 3 | + * |
| 4 | + * All rights reserved. This program and the accompanying materials are |
| 5 | + * made available under the terms of the Eclipse Public License 2.0 which |
| 6 | + * accompanies this distribution, and is available at |
| 7 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: EPL-2.0 |
| 10 | + *******************************************************************************/ |
| 11 | + |
| 12 | +package org.eclipse.tracecompass.incubator.trace.server.jersey.rest.core.tests.webapp; |
| 13 | + |
| 14 | +import static org.junit.Assert.assertEquals; |
| 15 | +import static org.junit.Assert.assertNotNull; |
| 16 | +import static org.junit.Assert.assertNull; |
| 17 | +import static org.junit.Assert.assertTrue; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import org.eclipse.tracecompass.incubator.internal.trace.server.jersey.rest.core.webapp.OutputElementStyleSerializer; |
| 22 | +import org.eclipse.tracecompass.incubator.internal.trace.server.jersey.rest.core.webapp.SeriesModelSerializer; |
| 23 | +import org.eclipse.tracecompass.incubator.trace.server.jersey.rest.core.tests.stubs.OutputElementStyleStub; |
| 24 | +import org.eclipse.tracecompass.incubator.trace.server.jersey.rest.core.tests.stubs.XySeriesStub; |
| 25 | +import org.eclipse.tracecompass.tmf.core.model.OutputElementStyle; |
| 26 | +import org.eclipse.tracecompass.tmf.core.model.SeriesModel.SeriesModelBuilder; |
| 27 | +import org.eclipse.tracecompass.tmf.core.model.StyleProperties; |
| 28 | +import org.eclipse.tracecompass.tmf.core.model.xy.ISeriesModel; |
| 29 | +import org.eclipse.tracecompass.tmf.core.model.xy.ISeriesModel.DisplayType; |
| 30 | +import org.junit.Test; |
| 31 | + |
| 32 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 33 | +import com.fasterxml.jackson.databind.module.SimpleModule; |
| 34 | + |
| 35 | +/** |
| 36 | + * Test the {@link SeriesModelSerializer} |
| 37 | + * |
| 38 | + * @author Bernd Hufmann |
| 39 | + */ |
| 40 | +public class SeriesModelSerializerTest extends AbstractSerializerTest { |
| 41 | + |
| 42 | + private static final long ID = 0; |
| 43 | + private static final String TITLE = "valid-styles"; |
| 44 | + private static final long[] times = { 0, 1, 2, 3 }; |
| 45 | + private static final double[] fValues = { 0.1, 0.2, 0.3, 0.4 }; |
| 46 | + |
| 47 | + /** |
| 48 | + * Verify that series models are serialized properly |
| 49 | + * |
| 50 | + * @throws JsonProcessingException |
| 51 | + * if an error occurs |
| 52 | + */ |
| 53 | + @SuppressWarnings("null") |
| 54 | + @Test |
| 55 | + public void testValidStyles() throws JsonProcessingException { |
| 56 | + |
| 57 | + SeriesModelBuilder builder = new SeriesModelBuilder(ID, TITLE, times, fValues); |
| 58 | + ISeriesModel lineModel = builder.build(); |
| 59 | + builder.seriesDisplayType(DisplayType.SCATTER); |
| 60 | + ISeriesModel scatterModel = builder.build(); |
| 61 | + |
| 62 | + SimpleModule module = new SimpleModule(); |
| 63 | + module.addSerializer(ISeriesModel.class, new SeriesModelSerializer()); |
| 64 | + module.addSerializer(OutputElementStyle.class, new OutputElementStyleSerializer()); |
| 65 | + fMapper.registerModule(module); |
| 66 | + |
| 67 | + String json = fMapper.writeValueAsString(lineModel); |
| 68 | + XySeriesStub deserialized = fMapper.readValue(json, XySeriesStub.class); |
| 69 | + assertNotNull(deserialized); |
| 70 | + assertEquals(TITLE, deserialized.getName()); |
| 71 | + assertEquals(ID, deserialized.getId()); |
| 72 | + List<Long> xValues = deserialized.getXValues(); |
| 73 | + assertTrue(times.length == xValues.size()); |
| 74 | + for (int i = 0; i < times.length; i++) { |
| 75 | + assertEquals(i, times[i], xValues.get(i)); |
| 76 | + } |
| 77 | + List<Double> yValues = deserialized.getYValues(); |
| 78 | + assertTrue(fValues.length == yValues.size()); |
| 79 | + for (int i = 0; i < fValues.length; i++) { |
| 80 | + assertEquals(fValues[i], yValues.get(i), 0.000001); |
| 81 | + } |
| 82 | + OutputElementStyleStub style = deserialized.getStyle(); |
| 83 | + assertNotNull(style); |
| 84 | + assertNull(style.getParentKey()); |
| 85 | + assertEquals(StyleProperties.SeriesType.LINE, style.getStyleValues().get(StyleProperties.SERIES_TYPE)); |
| 86 | + |
| 87 | + json = fMapper.writeValueAsString(scatterModel); |
| 88 | + deserialized = fMapper.readValue(json, XySeriesStub.class); |
| 89 | + assertNotNull(deserialized); |
| 90 | + style = deserialized.getStyle(); |
| 91 | + assertNotNull(style); |
| 92 | + assertNull(style.getParentKey()); |
| 93 | + assertEquals(StyleProperties.SeriesType.SCATTER, style.getStyleValues().get(StyleProperties.SERIES_TYPE)); |
| 94 | + } |
| 95 | +} |
0 commit comments