From fa3e55912ae52672b5a0f50d0b5f4a59b392b643 Mon Sep 17 00:00:00 2001 From: ZhangJian He Date: Sat, 12 Oct 2024 22:24:12 +0800 Subject: [PATCH] feat: add mtconnect error and devices decode Signed-off-by: ZhangJian He --- ci/spotbugs/exclude.xml | 5 ++ .../protocol/mtconnect/api/CuttingTool.java | 28 +++++++++ .../github/protocol/mtconnect/api/Error.java | 16 +++++ .../mtconnect/api/MTConnectAssets.java | 23 +++++++ .../mtconnect/api/MTConnectError.java | 21 +++++++ .../protocol/mtconnect/api/ToolLifeCycle.java | 20 ++++++ .../server/MTConnectAssetsDecodeTest.java | 63 +++++++++++++++++++ .../server/MTConnectDevicesDecodeTest.java | 3 +- .../server/MTConnectErrorDecodeTest.java | 53 ++++++++++++++++ pom.xml | 2 +- 10 files changed, 231 insertions(+), 3 deletions(-) create mode 100644 mtconnect-api/src/main/java/io/github/protocol/mtconnect/api/CuttingTool.java create mode 100644 mtconnect-api/src/main/java/io/github/protocol/mtconnect/api/Error.java create mode 100644 mtconnect-api/src/main/java/io/github/protocol/mtconnect/api/MTConnectAssets.java create mode 100644 mtconnect-api/src/main/java/io/github/protocol/mtconnect/api/MTConnectError.java create mode 100644 mtconnect-api/src/main/java/io/github/protocol/mtconnect/api/ToolLifeCycle.java create mode 100644 mtconnect-server/src/test/java/io/github/protocol/mtconnect/server/MTConnectAssetsDecodeTest.java create mode 100644 mtconnect-server/src/test/java/io/github/protocol/mtconnect/server/MTConnectErrorDecodeTest.java diff --git a/ci/spotbugs/exclude.xml b/ci/spotbugs/exclude.xml index e313525..39cc986 100644 --- a/ci/spotbugs/exclude.xml +++ b/ci/spotbugs/exclude.xml @@ -32,4 +32,9 @@ + + + + + diff --git a/mtconnect-api/src/main/java/io/github/protocol/mtconnect/api/CuttingTool.java b/mtconnect-api/src/main/java/io/github/protocol/mtconnect/api/CuttingTool.java new file mode 100644 index 0000000..4072487 --- /dev/null +++ b/mtconnect-api/src/main/java/io/github/protocol/mtconnect/api/CuttingTool.java @@ -0,0 +1,28 @@ +package io.github.protocol.mtconnect.api; + +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; +import lombok.Getter; +import lombok.Setter; + +@Setter +@Getter +public class CuttingTool { + + @JacksonXmlProperty(isAttribute = true, localName = "serialNumber") + private String serialNumber; + + @JacksonXmlProperty(isAttribute = true, localName = "timestamp") + private String timestamp; + + @JacksonXmlProperty(isAttribute = true, localName = "assetId") + private String assetId; + + @JacksonXmlProperty(localName = "Description") + private String description; + + @JacksonXmlProperty(localName = "ToolDefinition") + private String toolDefinition; + + @JacksonXmlProperty(localName = "ToolLifeCycle") + private ToolLifeCycle toolLifeCycle; +} diff --git a/mtconnect-api/src/main/java/io/github/protocol/mtconnect/api/Error.java b/mtconnect-api/src/main/java/io/github/protocol/mtconnect/api/Error.java new file mode 100644 index 0000000..866ec6a --- /dev/null +++ b/mtconnect-api/src/main/java/io/github/protocol/mtconnect/api/Error.java @@ -0,0 +1,16 @@ +package io.github.protocol.mtconnect.api; + +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlText; +import lombok.Getter; +import lombok.Setter; + +@Setter +@Getter +public class Error { + @JacksonXmlProperty(isAttribute = true, localName = "errorCode") + private String errorCode; + + @JacksonXmlText + private String text; +} diff --git a/mtconnect-api/src/main/java/io/github/protocol/mtconnect/api/MTConnectAssets.java b/mtconnect-api/src/main/java/io/github/protocol/mtconnect/api/MTConnectAssets.java new file mode 100644 index 0000000..3e04c5f --- /dev/null +++ b/mtconnect-api/src/main/java/io/github/protocol/mtconnect/api/MTConnectAssets.java @@ -0,0 +1,23 @@ +package io.github.protocol.mtconnect.api; + +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper; +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; +import lombok.Getter; +import lombok.Setter; + +import java.util.List; + +@Setter +@Getter +public class MTConnectAssets { + + @JacksonXmlProperty(isAttribute = true, localName = "schemaLocation") + private String schemaLocation; + + @JacksonXmlProperty(localName = "Header") + private Header header; + + @JacksonXmlElementWrapper(localName = "Assets") + @JacksonXmlProperty(localName = "CuttingTool") + private List cuttingTools; +} diff --git a/mtconnect-api/src/main/java/io/github/protocol/mtconnect/api/MTConnectError.java b/mtconnect-api/src/main/java/io/github/protocol/mtconnect/api/MTConnectError.java new file mode 100644 index 0000000..eb7557e --- /dev/null +++ b/mtconnect-api/src/main/java/io/github/protocol/mtconnect/api/MTConnectError.java @@ -0,0 +1,21 @@ +package io.github.protocol.mtconnect.api; + +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper; +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; +import lombok.Getter; +import lombok.Setter; + +import java.util.List; + +@Setter +@Getter +public class MTConnectError { + @JacksonXmlProperty(isAttribute = true, localName = "schemaLocation") + private String schemaLocation; + + @JacksonXmlProperty(localName = "Header") + private Header header; + + @JacksonXmlElementWrapper(localName = "Errors") + private List errors; +} diff --git a/mtconnect-api/src/main/java/io/github/protocol/mtconnect/api/ToolLifeCycle.java b/mtconnect-api/src/main/java/io/github/protocol/mtconnect/api/ToolLifeCycle.java new file mode 100644 index 0000000..05ea1ea --- /dev/null +++ b/mtconnect-api/src/main/java/io/github/protocol/mtconnect/api/ToolLifeCycle.java @@ -0,0 +1,20 @@ +package io.github.protocol.mtconnect.api; + +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlText; +import lombok.Getter; +import lombok.Setter; + +@Setter +@Getter +public class ToolLifeCycle { + + @JacksonXmlProperty(isAttribute = true, localName = "deviceUuid") + private String deviceUuid; + + @JacksonXmlProperty(isAttribute = true, localName = "toolId") + private String toolId; + + @JacksonXmlText + private String text; +} diff --git a/mtconnect-server/src/test/java/io/github/protocol/mtconnect/server/MTConnectAssetsDecodeTest.java b/mtconnect-server/src/test/java/io/github/protocol/mtconnect/server/MTConnectAssetsDecodeTest.java new file mode 100644 index 0000000..276bb03 --- /dev/null +++ b/mtconnect-server/src/test/java/io/github/protocol/mtconnect/server/MTConnectAssetsDecodeTest.java @@ -0,0 +1,63 @@ +package io.github.protocol.mtconnect.server; + +import io.github.protocol.mtconnect.api.CuttingTool; +import io.github.protocol.mtconnect.api.MTConnectAssets; +import io.github.protocol.mtconnect.common.XmlUtil; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +class MTConnectAssetsDecodeTest { + + @Test + void testMTConnectAssetsDecode() throws Exception { + String xml = """ + + + +
+ + + + Cutting Tool + ... + ... + + + + """; + + MTConnectAssets mtConnectAssets = XmlUtil.fromXml(xml, MTConnectAssets.class); + + // Validate MTConnectAssets object + Assertions.assertNotNull(mtConnectAssets); + Assertions.assertEquals("urn:mtconnect.org:MTConnectAssets:1.2 ../MTConnectAssets_1.2.xsd", mtConnectAssets.getSchemaLocation()); + + // Validate Header object + Assertions.assertNotNull(mtConnectAssets.getHeader()); + Assertions.assertEquals("2001-12-17T09:30:47Z", mtConnectAssets.getHeader().getCreationTime()); + Assertions.assertEquals("localhost", mtConnectAssets.getHeader().getSender()); + Assertions.assertEquals("1", mtConnectAssets.getHeader().getInstanceId()); + Assertions.assertEquals(131000, mtConnectAssets.getHeader().getBufferSize()); + Assertions.assertEquals("1.2", mtConnectAssets.getHeader().getVersion()); + + // Validate CuttingTool list + Assertions.assertNotNull(mtConnectAssets.getCuttingTools()); + Assertions.assertEquals(1, mtConnectAssets.getCuttingTools().size()); + + // Validate individual CuttingTool + CuttingTool cuttingTool = mtConnectAssets.getCuttingTools().get(0); + Assertions.assertEquals("1234", cuttingTool.getSerialNumber()); + Assertions.assertEquals("2001-12-17T09:30:47Z", cuttingTool.getTimestamp()); + Assertions.assertEquals("1234-112233", cuttingTool.getAssetId()); + Assertions.assertEquals("Cutting Tool", cuttingTool.getDescription()); + Assertions.assertEquals("...", cuttingTool.getToolDefinition()); + + // Validate ToolLifeCycle + Assertions.assertNotNull(cuttingTool.getToolLifeCycle()); + Assertions.assertEquals("1222", cuttingTool.getToolLifeCycle().getDeviceUuid()); + Assertions.assertEquals("1234", cuttingTool.getToolLifeCycle().getToolId()); + } +} diff --git a/mtconnect-server/src/test/java/io/github/protocol/mtconnect/server/MTConnectDevicesDecodeTest.java b/mtconnect-server/src/test/java/io/github/protocol/mtconnect/server/MTConnectDevicesDecodeTest.java index 55856aa..94287e0 100644 --- a/mtconnect-server/src/test/java/io/github/protocol/mtconnect/server/MTConnectDevicesDecodeTest.java +++ b/mtconnect-server/src/test/java/io/github/protocol/mtconnect/server/MTConnectDevicesDecodeTest.java @@ -8,8 +8,7 @@ class MTConnectDevicesDecodeTest { @Test - void testMTConnectDevicesParsing() throws Exception { - // The raw XML string + void testMTConnectDevicesDecode() throws Exception { String xml = """ + + +
+ + + Argument was out of range + Bad path + + + """; + + MTConnectError mtConnectError = XmlUtil.fromXml(xml, MTConnectError.class); + + // Validate MTConnectError object + Assertions.assertNotNull(mtConnectError); + Assertions.assertEquals("urn:mtconnect.org:MTConnectError:1.1 http://www.mtconnect.org/schemas/MTConnectError_1.1.xsd", mtConnectError.getSchemaLocation()); + + // Validate Header object + Assertions.assertNotNull(mtConnectError.getHeader()); + Assertions.assertEquals("2010-03-12T12:33:01", mtConnectError.getHeader().getCreationTime()); + Assertions.assertEquals("localhost", mtConnectError.getHeader().getSender()); + Assertions.assertEquals("1.1", mtConnectError.getHeader().getVersion()); + Assertions.assertEquals(131072, mtConnectError.getHeader().getBufferSize()); + Assertions.assertEquals("1268463594", mtConnectError.getHeader().getInstanceId()); + + // Validate Errors list + Assertions.assertNotNull(mtConnectError.getErrors()); + Assertions.assertEquals(2, mtConnectError.getErrors().size()); + + // Validate individual errors + Assertions.assertEquals("OUT_OF_RANGE", mtConnectError.getErrors().get(0).getErrorCode()); + Assertions.assertEquals("Argument was out of range", mtConnectError.getErrors().get(0).getText()); + Assertions.assertEquals("INVALID_XPATH", mtConnectError.getErrors().get(1).getErrorCode()); + Assertions.assertEquals("Bad path", mtConnectError.getErrors().get(1).getText()); + } +} diff --git a/pom.xml b/pom.xml index a35c69a..ab5cc5f 100644 --- a/pom.xml +++ b/pom.xml @@ -36,7 +36,7 @@ 0.8.12 3.13.0 3.5.0 - 3.8.0 + 3.6.0 3.2.5 3.10.0 3.1.1