Skip to content

Commit

Permalink
server: Add tests for metadata serialization of time graph entries
Browse files Browse the repository at this point in the history
Change-Id: Ie0c2c7737bdd93a0b641af48b446fba2dcf997bf
Signed-off-by: Bernd Hufmann <bernd.hufmann@ericsson.com>
Reviewed-on: https://git.eclipse.org/r/c/tracecompass.incubator/org.eclipse.tracecompass.incubator/+/193949
Reviewed-by: Marco Miller <marco.miller@ericsson.com>
Tested-by: Trace Compass Bot <tracecompass-bot@eclipse.org>
  • Loading branch information
bhufmann committed Jun 14, 2022
1 parent fc169ab commit 320552a
Show file tree
Hide file tree
Showing 5 changed files with 241 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ Require-Bundle: org.eclipse.core.runtime,
org.eclipse.tracecompass.lttng2.kernel.core,
org.eclipse.tracecompass.tmf.analysis.xml.core.tests,
jakarta.xml.bind,
io.swagger.core.v3.swagger-jaxrs2,
io.swagger.core.v3.swagger-annotations,
org.eclipse.jdt.annotation;bundle-version="[2.0.0,3.0.0)";resolution:=optional
Export-Package: org.eclipse.tracecompass.incubator.trace.server.jersey.rest.core.tests,
org.eclipse.tracecompass.incubator.trace.server.jersey.rest.core.tests.services,
org.eclipse.tracecompass.incubator.trace.server.jersey.rest.core.tests.stubs,
org.eclipse.tracecompass.incubator.trace.server.jersey.rest.core.tests.stubs.webapp,
org.eclipse.tracecompass.incubator.trace.server.jersey.rest.core.tests.utils
Import-Package: com.fasterxml.jackson.annotation,
com.fasterxml.jackson.jaxrs.base,
Expand All @@ -35,5 +38,6 @@ Import-Package: com.fasterxml.jackson.annotation,
javax.ws.rs.core,
org.eclipse.tracecompass.testtraces.ctf,
org.eclipse.tracecompass.tmf.ctf.core.tests.shared,
org.eclipse.tracecompass.tmf.ctf.core.trace
org.eclipse.tracecompass.tmf.ctf.core.trace,
org.glassfish.jersey.server
Automatic-Module-Name: org.eclipse.tracecompass.incubator.trace.server.jersey.rest.core.tests
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
Expand All @@ -31,6 +33,7 @@
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;

import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.tracecompass.incubator.internal.trace.server.jersey.rest.core.model.views.QueryParameters;
import org.eclipse.tracecompass.incubator.internal.trace.server.jersey.rest.core.services.DataProviderService;
import org.eclipse.tracecompass.incubator.trace.server.jersey.rest.core.tests.stubs.ColumnHeaderEntryStub;
Expand All @@ -55,6 +58,7 @@
import org.eclipse.tracecompass.incubator.trace.server.jersey.rest.core.tests.stubs.XyModelStub;
import org.eclipse.tracecompass.incubator.trace.server.jersey.rest.core.tests.stubs.XyOutputResponseStub;
import org.eclipse.tracecompass.incubator.trace.server.jersey.rest.core.tests.stubs.XySeriesStub;
import org.eclipse.tracecompass.incubator.trace.server.jersey.rest.core.tests.stubs.webapp.TestDataProviderService;
import org.eclipse.tracecompass.incubator.trace.server.jersey.rest.core.tests.utils.RestServerTest;
import org.eclipse.tracecompass.internal.tmf.core.model.filters.FetchParametersUtils;
import org.eclipse.tracecompass.tmf.core.model.filters.TimeQueryFilter;
Expand Down Expand Up @@ -453,4 +457,74 @@ public void testTableDataProvider() throws InterruptedException {
}
}

/**
* Using the custom data provider verify that only allowed types (Number,
* String) are serialized in the metadata map of time graph entries.
*/
@Test
public void testTimeGraphMetaDataSerializer() {
@SuppressWarnings("resource")
Response treeResponse = null;
try {
ExperimentModelStub exp = assertPostExperiment(CONTEXT_SWITCHES_UST_STUB.getName(), CONTEXT_SWITCHES_UST_STUB);

// Test getting the time graph tree
WebTarget callstackTree = getTimeGraphTreeEndpoint(exp.getUUID().toString(), TestDataProviderService.INVALID_ENTRY_METADATA);

Map<String, Object> parameters = new HashMap<>();
TgTreeOutputResponseStub responseModel;
parameters.put(REQUESTED_TIMES_KEY, ImmutableList.of(1450193697034689597L, 1450193745774189602L));
treeResponse = callstackTree.request().post(Entity.json(new QueryParameters(parameters, Collections.emptyList())));
assertEquals("There should be a positive response for the data provider", 200, treeResponse.getStatus());
responseModel = treeResponse.readEntity(TgTreeOutputResponseStub.class);
assertNotNull(responseModel);

// Verify Time Graph Entries
Set<TimeGraphEntryStub> entries = responseModel.getModel().getEntries();
assertEquals(2, entries.size());
for (TimeGraphEntryStub entry : entries) {
verifyEntry(entry);
}
} catch (ProcessingException e) {
// The failure from this exception alone is not helpful. Use the
// suppressed exception's message be the failure message for more
// help debugging failed tests.
fail(e.getCause().getMessage());
} finally {
if (treeResponse != null) {
treeResponse.close();
}
}
}

private static void verifyMetadata(Map<String, Collection<Object>> metadata, String key, Class<?> clazz) {
Collection<Object> col = metadata.get(key);
assertNotNull(key, col);
assertTrue(key, col.stream().allMatch(clazz::isInstance));
}

private static void verifyEntry(TimeGraphEntryStub entry) {
assertFalse(entry.getLabels().isEmpty());
if (entry.getLabels().get(0).equals(TestDataProviderService.ENTRY_NAME_WITH_METADATA)) {
// Verify supported values for metadata in entry
@Nullable Map<String, Collection<Object>> metadata = entry.getMetadata();
assertNotNull(metadata);
verifyMetadata(metadata, TestDataProviderService.VALID_TEST_KEY_BYTE, Number.class);
verifyMetadata(metadata, TestDataProviderService.VALID_TEST_KEY_SHORT, Number.class);
verifyMetadata(metadata, TestDataProviderService.VALID_TEST_KEY_INT, Number.class);
verifyMetadata(metadata, TestDataProviderService.VALID_TEST_KEY_LONG, Number.class);
verifyMetadata(metadata, TestDataProviderService.VALID_TEST_KEY_FLOAT, Number.class);
verifyMetadata(metadata, TestDataProviderService.VALID_TEST_KEY_DOUBLE, Number.class);
verifyMetadata(metadata, TestDataProviderService.VALID_TEST_KEY_STRING, String.class);

// Verify unsupported object
Collection<Object> col = metadata.get(TestDataProviderService.INVALID_TEST_KEY);
assertNull(TestDataProviderService.INVALID_TEST_KEY, col);
}

if (entry.getLabels().get(0).equals(TestDataProviderService.ENTRY_NAME_WITHOUT_METADATA)) {
// Verify that entry doesn't have metadata
assertNull(entry.getMetadata());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/**********************************************************************
* Copyright (c) 2022 Ericsson
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License 2.0 which
* accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
**********************************************************************/

package org.eclipse.tracecompass.incubator.trace.server.jersey.rest.core.tests.stubs.webapp;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.ws.rs.core.Response;

import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.tracecompass.incubator.internal.trace.server.jersey.rest.core.model.views.QueryParameters;
import org.eclipse.tracecompass.incubator.internal.trace.server.jersey.rest.core.model.views.TreeModelWrapper;
import org.eclipse.tracecompass.incubator.internal.trace.server.jersey.rest.core.services.DataProviderService;
import org.eclipse.tracecompass.incubator.internal.trace.server.jersey.rest.core.webapp.TimeGraphEntryModelSerializer;
import org.eclipse.tracecompass.tmf.core.model.CommonStatusMessage;
import org.eclipse.tracecompass.tmf.core.model.timegraph.TimeGraphEntryModel;
import org.eclipse.tracecompass.tmf.core.model.tree.ITmfTreeDataModel;
import org.eclipse.tracecompass.tmf.core.model.tree.TmfTreeModel;
import org.eclipse.tracecompass.tmf.core.response.ITmfResponse;
import org.eclipse.tracecompass.tmf.core.response.TmfModelResponse;
import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Multimap;

/**
* Custom data provider service used for testing
*/
@SuppressWarnings("null")
public class TestDataProviderService extends DataProviderService {

/** Data provider to test {@link TimeGraphEntryModelSerializer} */
public static final String INVALID_ENTRY_METADATA = "rest.core.test.valid.entry.metadata.dp";
/** Metadata Key for bytes */
public static final String VALID_TEST_KEY_BYTE = "test-key-byte";
/** Metadata Key for shorts */
public static final String VALID_TEST_KEY_SHORT = "test-key-short";
/** Metadata Key for integers */
public static final String VALID_TEST_KEY_INT = "test-key-int";
/** Metadata Key for longs */
public static final String VALID_TEST_KEY_LONG = "test-key-long";
/** Metadata Key for floats */
public static final String VALID_TEST_KEY_FLOAT = "test-key-float";
/** Metadata Key for doubles */
public static final String VALID_TEST_KEY_DOUBLE = "test-key-double";
/** Metadata Key for strings */
public static final String VALID_TEST_KEY_STRING = "test-key-string";
/** Metadata Key for unsupported object */
public static final String INVALID_TEST_KEY = "invalid-test-key";
/** Entry name for entry with metadata */
public static final String ENTRY_NAME_WITH_METADATA = "test-with-metadata";
/** Entry name for entry without metadata */
public static final String ENTRY_NAME_WITHOUT_METADATA = "test-without-metadata";

@Override
public Response getTimeGraphTree(UUID expUUID, String outputId, QueryParameters queryParameters) {
if (outputId.equals(INVALID_ENTRY_METADATA)) {
TestTimeGraphEntryModel entry = new TestTimeGraphEntryModel(1, 0, ENTRY_NAME_WITH_METADATA, 0, 100, true);
@NonNull List<@NonNull ITmfTreeDataModel> list = new ArrayList<>();
list.add(entry);
entry = new TestTimeGraphEntryModel(1, 0, ENTRY_NAME_WITHOUT_METADATA, 0, 100, false);
list.add(entry);
TmfTreeModel<@NonNull ITmfTreeDataModel> model = new TmfTreeModel<>(ImmutableList.of("test"), list);
return Response.ok(new TmfModelResponse<>(new TreeModelWrapper(model), ITmfResponse.Status.COMPLETED, CommonStatusMessage.COMPLETED)).build();
}
return super.getTimeGraphTree(expUUID, outputId, queryParameters);
}

private static class TestTimeGraphEntryModel extends TimeGraphEntryModel {
private final boolean fWithMetadata;

public TestTimeGraphEntryModel(long id, long parentId, String name, long startTime, long endTime, boolean withMetadata) {
super(id, parentId, name, startTime, endTime);
fWithMetadata = withMetadata;
}

@Override
public @NonNull Multimap<@NonNull String, @NonNull Object> getMetadata() {
Multimap<@NonNull String, @NonNull Object> metadata = HashMultimap.create();
if (fWithMetadata) {
for (byte i = 0; i < 3; i++) {
metadata.put(VALID_TEST_KEY_BYTE, Byte.valueOf(i));
metadata.put(VALID_TEST_KEY_SHORT, Short.valueOf(i));
metadata.put(VALID_TEST_KEY_INT, Integer.valueOf(i));
metadata.put(VALID_TEST_KEY_LONG, Long.valueOf(i));
metadata.put(VALID_TEST_KEY_FLOAT, Float.valueOf(i));
metadata.put(VALID_TEST_KEY_DOUBLE, Double.valueOf(i));
metadata.put(VALID_TEST_KEY_STRING, String.valueOf(i));
metadata.put(INVALID_TEST_KEY, TmfTimestamp.fromSeconds(i));
}
}
return metadata;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**********************************************************************
* Copyright (c) 2022 Ericsson
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License 2.0 which
* accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
**********************************************************************/

package org.eclipse.tracecompass.incubator.trace.server.jersey.rest.core.tests.stubs.webapp;

import org.eclipse.tracecompass.incubator.internal.trace.server.jersey.rest.core.services.ExperimentManagerService;
import org.eclipse.tracecompass.incubator.internal.trace.server.jersey.rest.core.services.FilterService;
import org.eclipse.tracecompass.incubator.internal.trace.server.jersey.rest.core.services.HealthService;
import org.eclipse.tracecompass.incubator.internal.trace.server.jersey.rest.core.services.TraceManagerService;
import org.eclipse.tracecompass.incubator.internal.trace.server.jersey.rest.core.services.XmlManagerService;
import org.eclipse.tracecompass.incubator.internal.trace.server.jersey.rest.core.webapp.CORSFilter;
import org.eclipse.tracecompass.incubator.internal.trace.server.jersey.rest.core.webapp.JacksonObjectMapperProvider;
import org.eclipse.tracecompass.incubator.internal.trace.server.jersey.rest.core.webapp.TraceServerConfiguration;
import org.eclipse.tracecompass.incubator.internal.trace.server.jersey.rest.core.webapp.WebApplication;
import org.glassfish.jersey.server.ResourceConfig;

import io.swagger.v3.jaxrs2.integration.resources.OpenApiResource;

/**
* Test WebApplication to create custom replies to for serializer testing
*/
public class TestWebApplication extends WebApplication {

/**
* Constructor to provide a configuration to the server
*
* @param config
* Server configuration
*/
public TestWebApplication(TraceServerConfiguration config) {
super(config);
}

@Override
protected void registerResourcesAndMappers(ResourceConfig rc) {
rc.register(TraceManagerService.class);
rc.register(ExperimentManagerService.class);
rc.register(TestDataProviderService.class);
rc.register(FilterService.class);
rc.register(HealthService.class);
rc.register(XmlManagerService.class);
rc.register(CORSFilter.class);
rc.register(JacksonObjectMapperProvider.class);
rc.register(OpenApiResource.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.eclipse.tracecompass.incubator.trace.server.jersey.rest.core.tests.stubs.DataProviderDescriptorStub;
import org.eclipse.tracecompass.incubator.trace.server.jersey.rest.core.tests.stubs.ExperimentModelStub;
import org.eclipse.tracecompass.incubator.trace.server.jersey.rest.core.tests.stubs.TraceModelStub;
import org.eclipse.tracecompass.incubator.trace.server.jersey.rest.core.tests.stubs.webapp.TestWebApplication;
import org.eclipse.tracecompass.testtraces.ctf.CtfTestTrace;
import org.eclipse.tracecompass.tmf.core.dataprovider.IDataProviderDescriptor.ProviderType;
import org.junit.After;
Expand All @@ -54,7 +55,7 @@
*/
public abstract class RestServerTest {
private static final String SERVER = "http://localhost:8378/tsp/api"; //$NON-NLS-1$
private static final WebApplication fWebApp = new WebApplication(new TraceServerConfiguration(TraceServerConfiguration.TEST_PORT, false, null, null));
private static final WebApplication fWebApp = new TestWebApplication(new TraceServerConfiguration(TraceServerConfiguration.TEST_PORT, false, null, null));
/**
* Traces endpoint path (relative to application).
*/
Expand Down

0 comments on commit 320552a

Please sign in to comment.