Skip to content

Commit

Permalink
apache#2144 Added migration test for Plc4xModbusAdapter
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaakKrut committed Jan 9, 2024
1 parent 803af51 commit 28a522e
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@ public MigrationResult<AdapterDescription> migrate(AdapterDescription element,
var newConfigs = element.getConfig().stream().map(config->{
if (isPortConfig(config)){
var label = Labels.withId(PLC_PORT);
return new FreeTextStaticProperty(label.getInternalId(),
label.getLabel(),
label.getDescription(),
XSD.INTEGER);
var staticProperty = new FreeTextStaticProperty(label.getInternalId(),
label.getLabel(),
label.getDescription(),
XSD.INTEGER);
staticProperty.setIndex(config.getIndex());
return staticProperty;
} else {
return config;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,14 @@ public class Plc4xModbusAdapter implements StreamPipesAdapter, IPullAdapter {
/**
* Keys of user configuration parameters
*/
private static final String PLC_IP = "plc_ip";
public static final String PLC_IP = "plc_ip";
public static final String PLC_PORT = "plc_port";

private static final String PLC_NODES = "plc_nodes";
private static final String PLC_NODE_ID = "plc_node_id";
private static final String PLC_NODE_RUNTIME_NAME = "plc_node_runtime_name";
private static final String PLC_NODE_ADDRESS = "plc_node_address";
private static final String PLC_NODE_TYPE = "plc_node_type";
public static final String PLC_NODES = "plc_nodes";
public static final String PLC_NODE_ID = "plc_node_id";
public static final String PLC_NODE_RUNTIME_NAME = "plc_node_runtime_name";
public static final String PLC_NODE_ADDRESS = "plc_node_address";
public static final String PLC_NODE_TYPE = "plc_node_type";
private static final String CONFIGURE = "configure";

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.apache.streampipes.extensions.connectors.plc.adapter.migration;

import org.apache.streampipes.extensions.connectors.plc.adapter.migration.config.Plc4xModbusAdapterVersionedConfig;

import org.junit.Before;
import org.junit.Test;

import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class Plc4xModbusAdapterMigrationTest {

private Plc4xModbusAdapterMigrationV1 migrationV1;

@Before
public void setUp() {
migrationV1 = new Plc4xModbusAdapterMigrationV1();
}

@Test
public void testMigrationV1(){
var v0 = Plc4xModbusAdapterVersionedConfig.getPlc4xModbusAdapterDescriptionV0();
var v1 = Plc4xModbusAdapterVersionedConfig.getPlc4xModbusAdapterDescriptionV1();

var migrationResult = migrationV1.migrate(v0, null);

assertTrue(migrationResult.success());
assertCollectionsEqual(v1.getConfig(), migrationResult.element().getConfig());
}

private <T> void assertCollectionsEqual(List<T> list1, List<T> list2) {
assertEquals(list1.size(), list2.size());
assertTrue(list1.containsAll(list2));
assertTrue(list2.containsAll(list1));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.apache.streampipes.extensions.connectors.plc.adapter.migration.config;

import org.apache.streampipes.extensions.connectors.plc.adapter.modbus.Plc4xModbusAdapter;
import org.apache.streampipes.model.AdapterType;
import org.apache.streampipes.model.connect.adapter.AdapterDescription;
import org.apache.streampipes.sdk.StaticProperties;
import org.apache.streampipes.sdk.builder.adapter.AdapterConfigurationBuilder;
import org.apache.streampipes.sdk.helpers.Labels;
import org.apache.streampipes.sdk.helpers.Locales;
import org.apache.streampipes.sdk.helpers.Options;
import org.apache.streampipes.sdk.utils.Assets;

import static org.apache.streampipes.extensions.connectors.plc.adapter.modbus.Plc4xModbusAdapter.ID;
import static org.apache.streampipes.extensions.connectors.plc.adapter.modbus.Plc4xModbusAdapter.PLC_IP;
import static org.apache.streampipes.extensions.connectors.plc.adapter.modbus.Plc4xModbusAdapter.PLC_NODES;
import static org.apache.streampipes.extensions.connectors.plc.adapter.modbus.Plc4xModbusAdapter.PLC_NODE_ADDRESS;
import static org.apache.streampipes.extensions.connectors.plc.adapter.modbus.Plc4xModbusAdapter.PLC_NODE_ID;
import static org.apache.streampipes.extensions.connectors.plc.adapter.modbus.Plc4xModbusAdapter.PLC_NODE_RUNTIME_NAME;
import static org.apache.streampipes.extensions.connectors.plc.adapter.modbus.Plc4xModbusAdapter.PLC_NODE_TYPE;
import static org.apache.streampipes.extensions.connectors.plc.adapter.modbus.Plc4xModbusAdapter.PLC_PORT;

public class Plc4xModbusAdapterVersionedConfig {

public static AdapterDescription getPlc4xModbusAdapterDescriptionV0(){
return AdapterConfigurationBuilder.create(ID, 0, Plc4xModbusAdapter::new)
.withLocales(Locales.EN)
.withAssets(Assets.DOCUMENTATION, Assets.ICON)
.withCategory(AdapterType.Manufacturing)
.requiredTextParameter(Labels.withId(PLC_IP)).requiredTextParameter(Labels.withId(PLC_PORT))
.requiredTextParameter(Labels.withId(PLC_NODE_ID)).requiredCollection(Labels.withId(PLC_NODES),
StaticProperties.stringFreeTextProperty(Labels.withId(PLC_NODE_RUNTIME_NAME)),
StaticProperties.integerFreeTextProperty(Labels.withId(PLC_NODE_ADDRESS)),
StaticProperties.singleValueSelection(Labels.withId(PLC_NODE_TYPE),
Options.from("DiscreteInput", "Coil", "InputRegister", "HoldingRegister")))
.buildConfiguration().getAdapterDescription();
}

public static AdapterDescription getPlc4xModbusAdapterDescriptionV1(){
return AdapterConfigurationBuilder.create(ID, 1, Plc4xModbusAdapter::new)
.withLocales(Locales.EN)
.withAssets(Assets.DOCUMENTATION, Assets.ICON)
.withCategory(AdapterType.Manufacturing)
.requiredTextParameter(Labels.withId(PLC_IP)).requiredIntegerParameter(Labels.withId(PLC_PORT))
.requiredTextParameter(Labels.withId(PLC_NODE_ID)).requiredCollection(Labels.withId(PLC_NODES),
StaticProperties.stringFreeTextProperty(Labels.withId(PLC_NODE_RUNTIME_NAME)),
StaticProperties.integerFreeTextProperty(Labels.withId(PLC_NODE_ADDRESS)),
StaticProperties.singleValueSelection(Labels.withId(PLC_NODE_TYPE),
Options.from("DiscreteInput", "Coil", "InputRegister", "HoldingRegister")))
.buildConfiguration().getAdapterDescription();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;

import java.util.Objects;

@JsonSubTypes({
@JsonSubTypes.Type(AnyStaticProperty.class),
@JsonSubTypes.Type(CodeInputStaticProperty.class),
Expand Down Expand Up @@ -150,4 +152,45 @@ public <T extends StaticProperty> T as(Class<T> targetClass) {

public abstract void accept(StaticPropertyVisitor visitor);

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof StaticProperty that)) {
return false;
}

if (valueRequired != that.valueRequired) {
return false;
}
if (index != that.index) {
return false;
}
if (predefined != that.predefined) {
return false;
}
if (staticPropertyType != that.staticPropertyType) {
return false;
}
if (!Objects.equals(label, that.label)) {
return false;
}
if (!Objects.equals(description, that.description)) {
return false;
}
return Objects.equals(internalName, that.internalName);
}

@Override
public int hashCode() {
int result = (valueRequired ? 1 : 0);
result = 31 * result + (staticPropertyType != null ? staticPropertyType.hashCode() : 0);
result = 31 * result + index;
result = 31 * result + (label != null ? label.hashCode() : 0);
result = 31 * result + (description != null ? description.hashCode() : 0);
result = 31 * result + (internalName != null ? internalName.hashCode() : 0);
result = 31 * result + (predefined ? 1 : 0);
return result;
}
}

0 comments on commit 28a522e

Please sign in to comment.