You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by sr...@apache.org on 2021/04/02 12:20:40 UTC

[plc4x] branch develop updated: plc4go: Ported ManualTestRunner.go from java

This is an automated email from the ASF dual-hosted git repository.

sruehl pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git


The following commit(s) were added to refs/heads/develop by this push:
     new ec1a16b  plc4go: Ported ManualTestRunner.go from java
ec1a16b is described below

commit ec1a16b90a89fbd3524475a3e08caf55dcaba25c
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Fri Apr 2 14:20:26 2021 +0200

    plc4go: Ported ManualTestRunner.go from java
    
    + This allows to run a manual test against a real plc
---
 go.mod                                             |   2 +-
 go.sum                                             |   4 +
 .../main/drivers/tests/manual_ads_driver_test.go   |  94 ++++++++++++
 .../plc4go/spi/testutils/ManualTestRunner.go       | 168 +++++++++++++++++++++
 4 files changed, 267 insertions(+), 1 deletion(-)

diff --git a/go.mod b/go.mod
index 3121534..cb0c6dc 100644
--- a/go.mod
+++ b/go.mod
@@ -21,6 +21,6 @@ module github.com/apache/plc4x
 go 1.15
 
 require (
-	github.com/apache/plc4x/plc4go v0.0.0-20210331131528-13579ee12950 // indirect
+	github.com/apache/plc4x/plc4go v0.0.0-20210402100047-03565df6b9ee // indirect
 	github.com/sirupsen/logrus v1.7.0 // indirect
 )
diff --git a/go.sum b/go.sum
index 2785884..891c3fa 100644
--- a/go.sum
+++ b/go.sum
@@ -20,6 +20,10 @@ github.com/apache/plc4x/plc4go v0.0.0-20210331081511-003c50ba4f7e h1:LFclJ70mgE7
 github.com/apache/plc4x/plc4go v0.0.0-20210331081511-003c50ba4f7e/go.mod h1:NfO8uGKPGwDxn1GqOb4oNhAtPF7St1A9LRk1J/qSlWU=
 github.com/apache/plc4x/plc4go v0.0.0-20210331131528-13579ee12950 h1:hAvcIzewcMmq7pCYUhD2ImON+lRO6dSBqaEpbzj3ecs=
 github.com/apache/plc4x/plc4go v0.0.0-20210331131528-13579ee12950/go.mod h1:NfO8uGKPGwDxn1GqOb4oNhAtPF7St1A9LRk1J/qSlWU=
+github.com/apache/plc4x/plc4go v0.0.0-20210402084608-9a035163ef03 h1:xg93Ig6/8OqE43hqO6N4440rMSpJtQ5MRx7L6bmbCcw=
+github.com/apache/plc4x/plc4go v0.0.0-20210402084608-9a035163ef03/go.mod h1:NfO8uGKPGwDxn1GqOb4oNhAtPF7St1A9LRk1J/qSlWU=
+github.com/apache/plc4x/plc4go v0.0.0-20210402100047-03565df6b9ee h1:+kli9AXi60YFkLQRcQ9CCG4b/QZAc+oZAcD3j0WlJf4=
+github.com/apache/plc4x/plc4go v0.0.0-20210402100047-03565df6b9ee/go.mod h1:NfO8uGKPGwDxn1GqOb4oNhAtPF7St1A9LRk1J/qSlWU=
 github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
 github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 github.com/dnephin/pflag v1.0.7/go.mod h1:uxE91IoWURlOiTUIA8Mq5ZZkAv3dPUfZNaT80Zm7OQE=
diff --git a/plc4go/cmd/main/drivers/tests/manual_ads_driver_test.go b/plc4go/cmd/main/drivers/tests/manual_ads_driver_test.go
new file mode 100644
index 0000000..5126858
--- /dev/null
+++ b/plc4go/cmd/main/drivers/tests/manual_ads_driver_test.go
@@ -0,0 +1,94 @@
+//
+// 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 tests
+
+import (
+	"github.com/apache/plc4x/plc4go/internal/plc4go/spi/testutils"
+	"testing"
+)
+
+func TestManualAds(t *testing.T) {
+	t.Skip()
+
+	/*
+		     * Test program code on the PLC with the test-data.
+		     *
+		     * Located in "main"
+		     *
+
+		    hurz_BOOL  := TRUE;
+			hurz_BYTE  := 42;
+			hurz_WORD  := 42424;
+			hurz_DWORD := 4242442424;
+			hurz_LWORD := 4242442424242424242;
+			hurz_SINT  := -42;
+			hurz_USINT := 42;
+			hurz_INT   := -2424;
+			hurz_UINT  := 42424;
+			hurz_DINT  := -242442424;
+			hurz_UDINT := 4242442424;
+			hurz_LINT  := -4242442424242424242;
+			hurz_ULINT := 4242442424242424242;
+			hurz_REAL  := 3.14159265359;
+			hurz_LREAL := 2.71828182846;
+			hurz_TIME  := T#1S234MS;
+			hurz_LTIME := LTIME#1000D15H23M12S34MS2US44NS;
+			hurz_DATE  := D#1978-03-28;
+			//hurz_LDATE:LDATE;
+			hurz_TIME_OF_DAY 	:= TIME_OF_DAY#15:36:30.123;
+			hurz_TOD         	:= TOD#16:17:18.123;
+			//hurz_LTIME_OF_DAY:LTIME_OF_DAY;
+			//hurz_LTOD:LTOD;
+			hurz_DATE_AND_TIME 	:= DATE_AND_TIME#1996-05-06-15:36:30;
+			hurz_DT				:= DT#1972-03-29-00:00:00;
+			//hurz_LDATE_AND_TIME:LDATE_AND_TIME;
+			//hurz_LDT:LDT;
+			hurz_STRING			:= 'hurz';
+			hurz_WSTRING		:= "wolf";
+
+		     *
+	*/
+
+	test := testutils.NewManualTestSuite("ads:tcp://192.168.23.20?sourceAmsNetId=192.168.23.200.1.1&sourceAmsPort=65534&targetAmsNetId=192.168.23.20.1.1&targetAmsPort=851")
+	test.AddTestCase("main.hurz_BOOL:BOOL", true)
+	test.AddTestCase("main.hurz_BYTE:BYTE", []bool{false, false, true, false, true, false, true, false})
+	test.AddTestCase("main.hurz_WORD:WORD", []bool{true, false, true, false, false, true, false, true, true, false, true, true, true, false, false, false})
+	test.AddTestCase("main.hurz_DWORD:DWORD", []bool{true, true, true, true, true, true, false, false, true, true, false, true, true, true, true, false, true, false, false, false, true, false, false, false, true, false, true, true, true, false, false, false})
+	test.AddTestCase("main.hurz_SINT:SINT", -42)
+	test.AddTestCase("main.hurz_USINT:USINT", 42)
+	test.AddTestCase("main.hurz_INT:INT", -2424)
+	test.AddTestCase("main.hurz_UINT:UINT", 42424)
+	test.AddTestCase("main.hurz_DINT:DINT", -242442424)
+	test.AddTestCase("main.hurz_UDINT:UDINT", 4242442424)
+	test.AddTestCase("main.hurz_LINT:LINT", -4242442424242424242)
+	test.AddTestCase("main.hurz_ULINT:ULINT", 4242442424242424242)
+	test.AddTestCase("main.hurz_REAL:REAL", 3.14159265359)
+	test.AddTestCase("main.hurz_LREAL:LREAL", 2.71828182846)
+	test.AddTestCase("main.hurz_STRING:STRING", "hurz")
+	test.AddTestCase("main.hurz_WSTRING:WSTRING", "wolf")
+	test.AddTestCase("main.hurz_TIME:TIME", "PT1.234S")
+	test.AddTestCase("main.hurz_LTIME:LTIME", "PT24015H23M12.034002044S")
+	test.AddTestCase("main.hurz_DATE:DATE", "1978-03-28")
+	test.AddTestCase("main.hurz_TIME_OF_DAY:TIME_OF_DAY", "15:36:30.123")
+	test.AddTestCase("main.hurz_TOD:TOD", "16:17:18.123")
+	test.AddTestCase("main.hurz_DATE_AND_TIME:DATE_AND_TIME", "1996-05-06T15:36:30")
+	test.AddTestCase("main.hurz_DT:DT", "1972-03-29T00:00")
+	test.Run()
+}
diff --git a/plc4go/internal/plc4go/spi/testutils/ManualTestRunner.go b/plc4go/internal/plc4go/spi/testutils/ManualTestRunner.go
new file mode 100644
index 0000000..733e298
--- /dev/null
+++ b/plc4go/internal/plc4go/spi/testutils/ManualTestRunner.go
@@ -0,0 +1,168 @@
+//
+// 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 testutils
+
+import (
+	"fmt"
+	"github.com/apache/plc4x/plc4go/pkg/plc4go"
+	"github.com/apache/plc4x/plc4go/pkg/plc4go/model"
+	"github.com/rs/zerolog/log"
+	"math/rand"
+	"strings"
+	"time"
+)
+
+type ManualTestCase struct {
+	Address           string
+	ExpectedReadValue interface{}
+	WriteValue        interface{}
+}
+
+type ManualTestSuite struct {
+	ConnectionString string
+	TestCases        []ManualTestCase
+}
+
+func NewManualTestSuite(connectionString string) *ManualTestSuite {
+	return &ManualTestSuite{
+		ConnectionString: connectionString,
+	}
+}
+
+func (m ManualTestSuite) AddTestCase(address string, expectedReadValue interface{}) {
+	m.TestCases = append(m.TestCases, ManualTestCase{address, expectedReadValue, nil})
+}
+
+func (m ManualTestSuite) Run() {
+	connectionResult := <-plc4go.NewPlcDriverManager().GetConnection(m.ConnectionString)
+	if connectionResult.Err != nil {
+		panic(connectionResult.Err)
+	}
+	connection := connectionResult
+	log.Info().Msg("Reading all types in separate requests")
+	// Run all entries separately:
+	for _, testCase := range m.TestCases {
+		fieldName := testCase.Address
+
+		{
+			// Prepare the read-request
+			readRequestBuilder := connection.Connection.ReadRequestBuilder()
+			readRequestBuilder.AddQuery(fieldName, testCase.Address)
+			readRequest, err := readRequestBuilder.Build()
+			if err != nil {
+				panic(err)
+			}
+
+			// Execute the read request
+			readResponseResult := <-readRequest.Execute()
+			if readResponseResult.Err != nil {
+				panic(err)
+			}
+			readResponse := readResponseResult.Response
+
+			// Check the result
+			assertEquals(1, len(readResponse.GetFieldNames()), fieldName)
+			assertEquals(fieldName, readResponse.GetFieldNames()[0], fieldName)
+			assertEquals(model.PlcResponseCode_OK, readResponse.GetResponseCode(fieldName), fieldName)
+			assertNotNil(readResponse.GetValue(fieldName), fieldName)
+			if readResponse.GetValue(fieldName).IsList() {
+				plcList := readResponse.GetValue(fieldName).GetList()
+				expectedValues := testCase.ExpectedReadValue.([]interface{})
+				for j := 0; j < len(expectedValues); j++ {
+					assertEquals(expectedValues[j], plcList[j].GetString(), fmt.Sprintf("%s[%d]", fieldName, j))
+				}
+			} else {
+				assertEquals(fmt.Sprint(testCase.ExpectedReadValue), readResponse.GetValue(fieldName).GetString(), fieldName)
+			}
+
+			log.Info().Msg("Success")
+		}
+
+		{
+			// Read all items in one big request.
+			// Shuffle the list of test cases and run the test 10 times.
+			log.Info().Msg("Reading all items together in random order")
+			for i := 0; i < 100; i++ {
+				log.Info().Msgf(" - run number %d of %d", i, 100)
+				shuffledTestcases := append(make([]ManualTestCase, 0), m.TestCases...)
+				rand.Seed(time.Now().UnixNano())
+				rand.Shuffle(len(shuffledTestcases), func(i, j int) {
+					shuffledTestcases[i], shuffledTestcases[j] = shuffledTestcases[j], shuffledTestcases[i]
+				})
+
+				sb := strings.Builder{}
+				for _, testCase := range shuffledTestcases {
+					sb.WriteString(testCase.Address)
+					sb.WriteString(", ")
+				}
+				log.Info().Msgf("       using order: %s", sb.String())
+
+				builder := connection.Connection.ReadRequestBuilder()
+				for _, testCase := range shuffledTestcases {
+					fieldName := testCase.Address
+					builder.AddQuery(fieldName, testCase.Address)
+				}
+				readRequest, err := builder.Build()
+				if err != nil {
+					panic(err)
+				}
+
+				// Execute the read request
+				readResponseResult := <-readRequest.Execute()
+				if readResponseResult.Err != nil {
+					panic(err)
+				}
+				readResponse := readResponseResult.Response
+
+				// Check the result
+				assertEquals(len(shuffledTestcases), len(readResponse.GetFieldNames()))
+				for _, testCase := range shuffledTestcases {
+					fieldName := testCase.Address
+					assertEquals(model.PlcResponseCode_OK, readResponse.GetResponseCode(fieldName))
+					assertNotNil(readResponse.GetValue(fieldName))
+					if readResponse.GetValue(fieldName).IsList() {
+						plcList := readResponse.GetValue(fieldName).GetList()
+						expectedValues := testCase.ExpectedReadValue.([]interface{})
+						for j := 0; j < len(expectedValues); j++ {
+							assertEquals(expectedValues[j], plcList[j].GetString(), fmt.Sprintf("%s[%d]", fieldName, j))
+						}
+					} else {
+						assertEquals(fmt.Sprint(testCase.ExpectedReadValue), readResponse.GetValue(fieldName).GetString(), fieldName)
+					}
+				}
+			}
+			log.Info().Msg("Success")
+		}
+	}
+}
+
+func assertEquals(expected interface{}, actual interface{}, message ...string) {
+	if expected != actual {
+		log.Error().Msgf("actual %s doesn't match expected %s\nmessage: %s", actual, expected, message)
+		panic("Assertion failed")
+	}
+}
+
+func assertNotNil(actual interface{}, message ...string) {
+	if nil != actual {
+		log.Error().Msgf("actual %s is nil\nmessage: %s", actual, message)
+		panic("Assertion failed")
+	}
+}