Skip to content

Commit

Permalink
#2144 Added migration test for OpcUaAdapterMigrationV2
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaakKrut committed Jan 17, 2024
1 parent cc9769b commit 8b1ade9
Show file tree
Hide file tree
Showing 6 changed files with 317 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ public MigrationResult<AdapterDescription> migrate(AdapterDescription element,
IStaticPropertyExtractor extractor) throws RuntimeException {
var newConfigs = element.getConfig().stream().map(config->{
if (isHostOrUrlConfig(config)){
return modifiedAlternatives();
var alternatives = modifiedAlternatives();
alternatives.setIndex(config.getIndex());
return alternatives;
} else {
return config;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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.opcua.adapter;

import org.apache.streampipes.extensions.connectors.opcua.adapter.config.OpcUaAdapterVersionedConfig;
import org.apache.streampipes.extensions.connectors.opcua.migration.OpcUaAdapterMigrationV2;

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 OpcUaAdapterMigrationTest {

private OpcUaAdapterMigrationV2 migrationV2;

@Before
public void setUp(){
migrationV2 = new OpcUaAdapterMigrationV2();
}

@Test
public void testMigrationV2(){
var v1 = OpcUaAdapterVersionedConfig.getOpcUaAdapterDescriptionV1();
var v2 = OpcUaAdapterVersionedConfig.getOpcUaAdapterDescriptionV2();

var migrationResult = migrationV2.migrate(v1, null);

assertTrue(migrationResult.success());
assertCollectionsEqual(v2.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,151 @@
/*
* 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.opcua.adapter.config;

import org.apache.streampipes.extensions.connectors.opcua.adapter.OpcUaAdapter;
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.Alternatives;
import org.apache.streampipes.sdk.helpers.Labels;
import org.apache.streampipes.sdk.helpers.Locales;
import org.apache.streampipes.sdk.utils.Assets;

import java.util.List;

import static org.apache.streampipes.extensions.connectors.opcua.adapter.OpcUaAdapter.ID;
import static org.apache.streampipes.extensions.connectors.opcua.utils.OpcUaLabels.ACCESS_MODE;
import static org.apache.streampipes.extensions.connectors.opcua.utils.OpcUaLabels.ADAPTER_TYPE;
import static org.apache.streampipes.extensions.connectors.opcua.utils.OpcUaLabels.AVAILABLE_NODES;
import static org.apache.streampipes.extensions.connectors.opcua.utils.OpcUaLabels.HOST_PORT;
import static org.apache.streampipes.extensions.connectors.opcua.utils.OpcUaLabels.OPC_HOST;
import static org.apache.streampipes.extensions.connectors.opcua.utils.OpcUaLabels.OPC_HOST_OR_URL;
import static org.apache.streampipes.extensions.connectors.opcua.utils.OpcUaLabels.OPC_SERVER_HOST;
import static org.apache.streampipes.extensions.connectors.opcua.utils.OpcUaLabels.OPC_SERVER_PORT;
import static org.apache.streampipes.extensions.connectors.opcua.utils.OpcUaLabels.OPC_SERVER_URL;
import static org.apache.streampipes.extensions.connectors.opcua.utils.OpcUaLabels.OPC_URL;
import static org.apache.streampipes.extensions.connectors.opcua.utils.OpcUaLabels.PASSWORD;
import static org.apache.streampipes.extensions.connectors.opcua.utils.OpcUaLabels.PULLING_INTERVAL;
import static org.apache.streampipes.extensions.connectors.opcua.utils.OpcUaLabels.PULL_MODE;
import static org.apache.streampipes.extensions.connectors.opcua.utils.OpcUaLabels.SUBSCRIPTION_MODE;
import static org.apache.streampipes.extensions.connectors.opcua.utils.OpcUaLabels.UNAUTHENTICATED;
import static org.apache.streampipes.extensions.connectors.opcua.utils.OpcUaLabels.USERNAME;
import static org.apache.streampipes.extensions.connectors.opcua.utils.OpcUaLabels.USERNAME_GROUP;

public class OpcUaAdapterVersionedConfig {

public static AdapterDescription getOpcUaAdapterDescriptionV1(){
var builder = AdapterConfigurationBuilder.create(ID, 1, OpcUaAdapter::new)
.withAssets(Assets.DOCUMENTATION, Assets.ICON)
.withLocales(Locales.EN)
.withCategory(AdapterType.Generic, AdapterType.Manufacturing)
.requiredAlternatives(Labels.withId(ADAPTER_TYPE),
Alternatives.from(Labels.withId(PULL_MODE),
StaticProperties.integerFreeTextProperty(
Labels.withId(PULLING_INTERVAL))),
Alternatives.from(Labels.withId(SUBSCRIPTION_MODE)));
var dependsOn = List.of(
ADAPTER_TYPE.name(),
ACCESS_MODE.name(),
OPC_HOST_OR_URL.name());
builder
.requiredAlternatives(Labels.withId(ACCESS_MODE),
Alternatives.from(Labels.withId(UNAUTHENTICATED)),
Alternatives.from(Labels.withId(USERNAME_GROUP),
StaticProperties.group(
Labels.withId(USERNAME_GROUP),
StaticProperties.stringFreeTextProperty(
Labels.withId(USERNAME)),
StaticProperties.secretValue(Labels.withId(PASSWORD))
))
)
.requiredAlternatives(Labels.withId(OPC_HOST_OR_URL),
Alternatives.from(
Labels.withId(OPC_URL),
StaticProperties.stringFreeTextProperty(
Labels.withId(OPC_SERVER_URL), "opc.tcp://localhost:4840"))
,
Alternatives.from(Labels.withId(OPC_HOST),
StaticProperties.group(
Labels.withId(HOST_PORT),
StaticProperties.stringFreeTextProperty(
Labels.withId(OPC_SERVER_HOST)),
StaticProperties.stringFreeTextProperty(
Labels.withId(OPC_SERVER_PORT))
))
)
.requiredRuntimeResolvableTreeInput(
Labels.withId(AVAILABLE_NODES.name()),
dependsOn,
true,
true
);
return builder.buildConfiguration().getAdapterDescription();
}

public static AdapterDescription getOpcUaAdapterDescriptionV2(){
var builder = AdapterConfigurationBuilder.create(ID, 2, OpcUaAdapter::new)
.withAssets(Assets.DOCUMENTATION, Assets.ICON)
.withLocales(Locales.EN)
.withCategory(AdapterType.Generic, AdapterType.Manufacturing)
.requiredAlternatives(Labels.withId(ADAPTER_TYPE),
Alternatives.from(Labels.withId(PULL_MODE),
StaticProperties.integerFreeTextProperty(
Labels.withId(PULLING_INTERVAL))),
Alternatives.from(Labels.withId(SUBSCRIPTION_MODE)));
var dependsOn = List.of(
ADAPTER_TYPE.name(),
ACCESS_MODE.name(),
OPC_HOST_OR_URL.name());
builder
.requiredAlternatives(Labels.withId(ACCESS_MODE),
Alternatives.from(Labels.withId(UNAUTHENTICATED)),
Alternatives.from(Labels.withId(USERNAME_GROUP),
StaticProperties.group(
Labels.withId(USERNAME_GROUP),
StaticProperties.stringFreeTextProperty(
Labels.withId(USERNAME)),
StaticProperties.secretValue(Labels.withId(PASSWORD))
))
)
.requiredAlternatives(Labels.withId(OPC_HOST_OR_URL),
Alternatives.from(
Labels.withId(OPC_URL),
StaticProperties.stringFreeTextProperty(
Labels.withId(OPC_SERVER_URL), "opc.tcp://localhost:4840"))
,
Alternatives.from(Labels.withId(OPC_HOST),
StaticProperties.group(
Labels.withId(HOST_PORT),
StaticProperties.stringFreeTextProperty(
Labels.withId(OPC_SERVER_HOST)),
StaticProperties.integerFreeTextProperty(
Labels.withId(OPC_SERVER_PORT))
))
)
.requiredRuntimeResolvableTreeInput(
Labels.withId(AVAILABLE_NODES.name()),
dependsOn,
true,
true
);
return builder.buildConfiguration().getAdapterDescription();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.apache.streampipes.model.util.Cloner;
import org.apache.streampipes.model.util.ElementIdGenerator;

import java.util.Objects;

public class StaticPropertyAlternative extends StaticProperty {

private String elementId;
Expand Down Expand Up @@ -76,4 +78,37 @@ public void setStaticProperty(StaticProperty staticProperty) {
public void accept(StaticPropertyVisitor visitor) {
visitor.visit(this);
}

/**
* equals() excludes elementId
*/
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof StaticPropertyAlternative that)) {
return false;
}
if (!super.equals(o)) {
return false;
}

if (selected != that.selected) {
return false;
}

return Objects.equals(staticProperty, that.staticProperty);
}

/**
* hashCode() excludes elementId
*/
@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + (selected ? 1 : 0);
result = 31 * result + (staticProperty != null ? staticProperty.hashCode() : 0);
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,36 @@ public void setAlternatives(List<StaticPropertyAlternative> alternatives) {
public void accept(StaticPropertyVisitor visitor) {
visitor.visit(this);
}

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

if (!(this.alternatives.size() == that.alternatives.size())) {
return false;
}

for (var i = 0; i < alternatives.size(); i++){
if (!alternatives.get(i).equals(that.alternatives.get(i))){
return false;
}
}

return true;
}

@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + (alternatives != null ? alternatives.hashCode() : 0);
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.streampipes.model.util.Cloner;

import java.util.List;
import java.util.Objects;

public class StaticPropertyGroup extends StaticProperty {

Expand Down Expand Up @@ -83,4 +84,41 @@ public void setHorizontalRendering(boolean horizontalRendering) {
public void accept(StaticPropertyVisitor visitor) {
visitor.visit(this);
}

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

if (horizontalRendering != that.horizontalRendering) {
return false;
}

if (!(this.staticProperties.size() == that.staticProperties.size())) {
return false;
}

for (var i = 0; i < staticProperties.size(); i++){
if (!staticProperties.get(i).equals(that.staticProperties.get(i))){
return false;
}
}
return Objects.equals(showLabel, that.showLabel);
}

@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + (staticProperties != null ? staticProperties.hashCode() : 0);
result = 31 * result + (showLabel != null ? showLabel.hashCode() : 0);
result = 31 * result + (horizontalRendering ? 1 : 0);
return result;
}
}

0 comments on commit 8b1ade9

Please sign in to comment.