/******************************************************************************* | |
* Copyright (c) 2021 Contributors to the Eclipse Foundation | |
* | |
* See the NOTICE file(s) distributed with this work for additional | |
* information regarding copyright ownership. | |
* | |
* This program and the accompanying materials are made available under the | |
* terms of the Eclipse Public License v. 2.0 which is available at | |
* http://www.eclipse.org/legal/epl-2.0. | |
* | |
* SPDX-License-Identifier: EPL-2.0 | |
*******************************************************************************/ | |
package org.eclipse.mdm.nodeprovider.entity; | |
import static org.assertj.core.api.Assertions.assertThat; | |
import java.util.Map; | |
import org.eclipse.mdm.api.atfxadapter.ATFXContextFactory; | |
import org.eclipse.mdm.api.base.ConnectionException; | |
import org.eclipse.mdm.api.base.adapter.Attribute; | |
import org.eclipse.mdm.api.base.adapter.EntityType; | |
import org.eclipse.mdm.api.base.adapter.ModelManager; | |
import org.eclipse.mdm.api.base.model.Channel; | |
import org.eclipse.mdm.api.base.model.ChannelGroup; | |
import org.eclipse.mdm.api.base.model.Environment; | |
import org.eclipse.mdm.api.base.model.Measurement; | |
import org.eclipse.mdm.api.base.model.TestStep; | |
import org.eclipse.mdm.api.base.search.ContextState; | |
import org.eclipse.mdm.api.dflt.ApplicationContext; | |
import org.eclipse.mdm.api.dflt.model.Project; | |
import org.eclipse.mdm.nodeprovider.utils.SerializationUtil; | |
import org.junit.After; | |
import org.junit.Before; | |
import org.junit.Test; | |
import com.google.common.collect.ImmutableMap; | |
public class NodeProviderRootTest { | |
private ApplicationContext context; | |
@Before | |
public void init() throws ConnectionException { | |
Map<String, String> map = ImmutableMap.of("atfxfile", "../../api/atfxadapter/src/test/resources/Right_Acc.atfx", | |
"freetext.active", "false"); | |
context = new ATFXContextFactory().connect(map); | |
} | |
@After | |
public void close() throws ConnectionException { | |
context.close(); | |
} | |
private NodeProviderRoot getNodeProviderRoot(ApplicationContext context) { | |
NodeLevel nl = createStructure(context, ContextState.MEASURED); | |
NodeProviderRoot npr = new NodeProviderRoot(); | |
npr.setId("generic_measured"); | |
npr.setName("Measured"); | |
npr.setContexts(ImmutableMap.of("*", nl)); | |
return npr; | |
} | |
@Test | |
public void testGetNodeLevel() throws ConnectionException { | |
NodeProviderRoot np = getNodeProviderRoot(context); | |
NodeLevel nl0 = np.getNodeLevel(context, "Environment", null); | |
assertThat(nl0.getEntityType().getName()).isEqualTo("Environment"); | |
assertThat(nl0.getFilterAttributes()).extracting(Attribute::getName).containsExactly("Id"); | |
assertThat(nl0.getContextState()).isNull(); | |
assertThat(nl0.isVirtual()).isFalse(); | |
NodeLevel nl1 = np.getNodeLevel(context, "Project", null); | |
assertThat(nl1.getEntityType().getName()).isEqualTo("Project"); | |
assertThat(nl1.getFilterAttributes()).extracting(Attribute::getName).containsExactly("Id"); | |
assertThat(nl1.getContextState()).isNull(); | |
assertThat(nl1.isVirtual()).isFalse(); | |
NodeLevel nl2 = np.getNodeLevel(context, "vehicle", "model"); | |
assertThat(nl2.getEntityType().getName()).isEqualTo("vehicle"); | |
assertThat(nl2.getFilterAttributes()).extracting(Attribute::getName).containsExactly("model"); | |
assertThat(nl2.getContextState()).isEqualTo(ContextState.MEASURED); | |
assertThat(nl2.isVirtual()).isTrue(); | |
NodeLevel nl3 = np.getNodeLevel(context, "Test", null); | |
assertThat(nl3.getEntityType().getName()).isEqualTo("Test"); | |
assertThat(nl3.getFilterAttributes()).extracting(Attribute::getName).containsExactly("Id"); | |
assertThat(nl3.getContextState()).isNull(); | |
assertThat(nl3.isVirtual()).isFalse(); | |
NodeLevel nl4 = np.getNodeLevel(context, "TestStep", null); | |
assertThat(nl4.getEntityType().getName()).isEqualTo("TestStep"); | |
assertThat(nl4.getFilterAttributes()).extracting(Attribute::getName).containsExactly("Id"); | |
assertThat(nl4.getContextState()).isNull(); | |
assertThat(nl4.isVirtual()).isFalse(); | |
NodeLevel nl5 = np.getNodeLevel(context, "Measurement", null); | |
assertThat(nl5.getEntityType().getName()).isEqualTo("MeaResult"); | |
assertThat(nl5.getFilterAttributes()).extracting(Attribute::getName).containsExactly("Id"); | |
assertThat(nl5.getContextState()).isNull(); | |
assertThat(nl5.isVirtual()).isFalse(); | |
NodeLevel nl6 = np.getNodeLevel(context, "ChannelGroup", null); | |
assertThat(nl6.getEntityType().getName()).isEqualTo("SubMatrix"); | |
assertThat(nl6.getFilterAttributes()).extracting(Attribute::getName).containsExactly("Id"); | |
assertThat(nl6.getContextState()).isNull(); | |
assertThat(nl6.isVirtual()).isFalse(); | |
NodeLevel nl7 = np.getNodeLevel(context, "Channel", null); | |
assertThat(nl7.getEntityType().getName()).isEqualTo("MeaQuantity"); | |
assertThat(nl7.getFilterAttributes()).extracting(Attribute::getName).containsExactly("Id"); | |
assertThat(nl7.getContextState()).isNull(); | |
assertThat(nl7.isVirtual()).isFalse(); | |
} | |
@Test | |
public void testGetNodeLevelInvalid() throws ConnectionException { | |
NodeProviderRoot np = getNodeProviderRoot(context); | |
assertThat(np.getNodeLevel(context, "InvalidType", null)).isNull(); | |
} | |
@Test | |
public void testGetParentNodeLevel() throws ConnectionException { | |
NodeProviderRoot np = getNodeProviderRoot(context); | |
NodeLevel nl0 = np.getNodeLevel(context, "Environment", null); | |
NodeLevel nl1 = np.getNodeLevel(context, "Project", null); | |
NodeLevel nl2 = np.getNodeLevel(context, "vehicle", "model"); | |
NodeLevel nl3 = np.getNodeLevel(context, "Test", null); | |
assertThat(np.getParentNodeLevel(context, nl0)).isNull(); | |
assertThat(np.getParentNodeLevel(context, nl1)).isEqualTo(nl0); | |
assertThat(np.getParentNodeLevel(context, nl2)).isEqualTo(nl1); | |
assertThat(np.getParentNodeLevel(context, nl3)).isEqualTo(nl2); | |
} | |
@Test | |
public void testGetChildNodeLevel() throws ConnectionException { | |
NodeProviderRoot np = getNodeProviderRoot(context); | |
NodeLevel nl0 = np.getNodeLevel(context, "Environment", null); | |
NodeLevel nl1 = np.getNodeLevel(context, "Project", null); | |
NodeLevel nl2 = np.getNodeLevel(context, "vehicle", "model"); | |
NodeLevel nl3 = np.getNodeLevel(context, "Test", null); | |
assertThat(np.getChildNodeLevel(context, null)).contains(nl0); | |
assertThat(np.getChildNodeLevel(context, | |
SerializationUtil.createNode("NVHDEMO", "Environment", "1", "Id", "", "Name"))).contains(nl1); | |
assertThat( | |
np.getChildNodeLevel(context, SerializationUtil.createNode("NVHDEMO", "Project", "", "Id", "", "Name"))) | |
.contains(nl2); | |
assertThat(np.getChildNodeLevel(context, | |
SerializationUtil.createNode("NVHDEMO", "vehicle", "", "model", "", "model"))).contains(nl3); | |
} | |
private static NodeLevel createStructure(ApplicationContext context, ContextState contextState) { | |
ModelManager modelManager = context.getModelManager().get(); | |
NodeLevel env = new NodeLevel(modelManager.getEntityType(Environment.class)); | |
NodeLevel project = new NodeLevel(modelManager.getEntityType(Project.class)); | |
EntityType vehicle = modelManager.getEntityType("vehicle"); | |
NodeLevel vehicleModel = new NodeLevel(vehicle, vehicle.getAttribute("model"), vehicle.getAttribute("model")); | |
vehicleModel.setVirtual(true); | |
vehicleModel.setContextState(contextState); | |
NodeLevel test = new NodeLevel(modelManager.getEntityType(org.eclipse.mdm.api.base.model.Test.class)); | |
env.setChild(project); | |
project.setChild(vehicleModel); | |
vehicleModel.setChild(test); | |
NodeLevel testStep = new NodeLevel(modelManager.getEntityType(TestStep.class)); | |
NodeLevel measurement = new NodeLevel(modelManager.getEntityType(Measurement.class)); | |
NodeLevel channelGroup = new NodeLevel(modelManager.getEntityType(ChannelGroup.class)); | |
NodeLevel channel = new NodeLevel(modelManager.getEntityType(Channel.class)); | |
test.setChild(testStep); | |
testStep.setChild(measurement); | |
measurement.setChild(channelGroup); | |
channelGroup.setChild(channel); | |
return env; | |
} | |
} |