Skip to content

Commit

Permalink
feat: add mtconnect error and devices decode
Browse files Browse the repository at this point in the history
Signed-off-by: ZhangJian He <shoothzj@gmail.com>
  • Loading branch information
shoothzj committed Oct 12, 2024
1 parent e6f0235 commit fa3e559
Show file tree
Hide file tree
Showing 10 changed files with 231 additions and 3 deletions.
5 changes: 5 additions & 0 deletions ci/spotbugs/exclude.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,9 @@
<Class name="io.github.protocol.mtconnect.examples.MtConnectClientExample"/>
</Match>

<Match>
<Bug pattern="SIC_INNER_SHOULD_BE_STATIC"/>
<Class name="io.github.protocol.mtconnect.server.MtConnectServer$MtAssetsHandler"/>
</Match>

</FindBugsFilter>
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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<CuttingTool> cuttingTools;
}
Original file line number Diff line number Diff line change
@@ -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<Error> errors;
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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 = """
<?xml version="1.0" encoding="UTF-8"?>
<MTConnectAssets xmlns="urn:mtconnect.org:MTConnectAssets:1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:mtconnect.org:MTConnectAssets:1.2 ../MTConnectAssets_1.2.xsd">
<Header creationTime="2001-12-17T09:30:47Z" sender="localhost"
version="1.2" bufferSize="131000" instanceId="1" />
<Assets>
<CuttingTool serialNumber="1234" timestamp="2001-12-17T09:30:47Z" assetId="1234-112233">
<Description>Cutting Tool</Description>
<ToolDefinition>...</ToolDefinition>
<ToolLifeCycle deviceUuid="1222" toolId="1234">...</ToolLifeCycle>
</CuttingTool>
</Assets>
</MTConnectAssets>
""";

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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@

class MTConnectDevicesDecodeTest {
@Test
void testMTConnectDevicesParsing() throws Exception {
// The raw XML string
void testMTConnectDevicesDecode() throws Exception {
String xml = """
<?xml version="1.0" encoding="UTF-8"?>
<MTConnectDevices xmlns:m="urn:mtconnect.org:MTConnectDevices:1.1"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package io.github.protocol.mtconnect.server;

import io.github.protocol.mtconnect.api.MTConnectError;
import io.github.protocol.mtconnect.common.XmlUtil;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

class MTConnectErrorDecodeTest {

@Test
void testMTConnectErrorDecode() throws Exception {
// The raw XML string
String xml = """
<?xml version="1.0" encoding="UTF-8"?>
<MTConnectError xmlns="urn:mtconnect.org:MTConnectError:1.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:mtconnect.org:MTConnectError:1.1 http://www.mtconnect.org/schemas/MTConnectError_1.1.xsd">
<Header creationTime="2010-03-12T12:33:01" sender="localhost"
version="1.1" bufferSize="131072" instanceId="1268463594" />
<Errors>
<Error errorCode="OUT_OF_RANGE">Argument was out of range</Error>
<Error errorCode="INVALID_XPATH">Bad path</Error>
</Errors>
</MTConnectError>
""";

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());
}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<jacoco-maven-plugin.version>0.8.12</jacoco-maven-plugin.version>
<maven-compiler-plugin.version>3.13.0</maven-compiler-plugin.version>
<maven-enforcer-plugin.version>3.5.0</maven-enforcer-plugin.version>
<maven-enforce-plugin-maven.version>3.8.0</maven-enforce-plugin-maven.version>
<maven-enforce-plugin-maven.version>3.6.0</maven-enforce-plugin-maven.version>
<maven-gpg-plugin.version>3.2.5</maven-gpg-plugin.version>
<maven-javadoc-plugin.version>3.10.0</maven-javadoc-plugin.version>
<maven-release-plugin.version>3.1.1</maven-release-plugin.version>
Expand Down

0 comments on commit fa3e559

Please sign in to comment.