Skip to content

Commit

Permalink
refactor: move test xml to resources dir
Browse files Browse the repository at this point in the history
Signed-off-by: zhiheng123 <[email protected]>
  • Loading branch information
zhiheng123 committed Nov 16, 2024
1 parent 805663d commit 105b0f3
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 191 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.fasterxml.jackson.dataformat.xml.XmlMapper;

import java.io.InputStream;

public class XmlUtil {

private static final XmlMapper xmlMapper = new XmlMapper();
Expand All @@ -10,6 +12,10 @@ public static <T> T fromXml(String xml, Class<T> clazz) throws Exception {
return xmlMapper.readValue(xml, clazz);
}

public static <T> T fromXml(InputStream xml, Class<T> clazz) throws Exception {
return xmlMapper.readValue(xml, clazz);
}

public static String toXml(Object obj) throws Exception {
return xmlMapper.writeValueAsString(obj);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,15 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.io.InputStream;

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);

InputStream xmlInputStream = MTConnectFileUtil.readFile("mtConnectAssets.xml");
MTConnectAssets mtConnectAssets = XmlUtil.fromXml(xmlInputStream, MTConnectAssets.class);

// Validate MTConnectAssets object
Assertions.assertNotNull(mtConnectAssets);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,126 +6,14 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.io.InputStream;

class MTConnectDevicesDecodeTest {
@Test
void testMTConnectDevicesDecode() throws Exception {
String xml = """
<?xml version="1.0" encoding="UTF-8"?>
<MTConnectDevices xmlns:m="urn:mtconnect.org:MTConnectDevices:1.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="urn:mtconnect.org:MTConnectDevices:1.1"
xsi:schemaLocation="urn:mtconnect.org:MTConnectDevices:1.1
http://www.mtconnect.org/schemas/MTConnectDevices_1.1.xsd">
<Header creationTime="2010-03-13T08:02:38+00:00" sender="localhost"
instanceId="1268463594" bufferSize="131072" version="1.1" />
<Devices>
<Device id="dev" name="VMC-4Axis" uuid="XXX111">
<DataItems>
<DataItem category="EVENT" id="avail" type="AVAILABILITY" />
</DataItems>
<Components>
<Axes id="axes" name="axes">
<Components>
<Linear id="x" name="X">
<DataItems>
<DataItem category="SAMPLE" id="Xact" nativeUnits="MILLIMETER"
subType="ACTUAL" type="POSITION" units="MILLIMETER" />
<DataItem category="SAMPLE" id="Xload" nativeUnits="PERCENT"
type="LOAD" units="PERCENT" />
<DataItem category="CONDITION" id="Xtravel" type="POSITION" />
<DataItem category="CONDITION" id="Xovertemp" type="TEMPERATURE" />
<DataItem category="CONDITION" id="Xservo" type="ACTUATOR" />
</DataItems>
</Linear>
<Linear id="y" name="Y">
<DataItems>
<DataItem category="SAMPLE" id="Yact" nativeUnits="MILLIMETER"
subType="ACTUAL" type="POSITION" units="MILLIMETER" />
<DataItem category="SAMPLE" id="Yload" nativeUnits="PERCENT"
type="LOAD" units="PERCENT" />
<DataItem category="CONDITION" id="Ytravel" type="POSITION" />
<DataItem category="CONDITION" id="Yovertemp" type="TEMPERATURE" />
<DataItem category="CONDITION" id="Yservo" type="ACTUATOR" />
</DataItems>
</Linear>
<Linear id="z" name="Z">
<DataItems>
<DataItem category="SAMPLE" id="Zact" nativeUnits="MILLIMETER"
subType="ACTUAL" type="POSITION" units="MILLIMETER" />
<DataItem category="SAMPLE" id="Zload" nativeUnits="PERCENT"
type="LOAD" units="PERCENT" />
<DataItem category="CONDITION" id="Ztravel" type="POSITION" />
<DataItem category="CONDITION" id="Zovertemp" type="TEMPERATURE" />
<DataItem category="CONDITION" id="Zservo" type="ACTUATOR" />
</DataItems>
</Linear>
<Rotary id="a" name="A">
<DataItems>
<DataItem category="SAMPLE" id="Aact" nativeUnits="DEGREE"
subType="ACTUAL" type="ANGLE" units="DEGREE" />
<DataItem category="SAMPLE" id="Aload" nativeUnits="PERCENT"
type="LOAD" units="PERCENT" />
<DataItem category="CONDITION" id="Atravel" type="POSITION" />
<DataItem category="CONDITION" id="Aovertemp" type="TEMPERATURE" />
<DataItem category="CONDITION" id="Aservo" type="ACTUATOR" />
</DataItems>
</Rotary>
<Rotary id="c" name="C" nativeName="S1">
<DataItems>
<DataItem category="SAMPLE" id="S1speed" nativeUnits="REVOLUTION/MINUTE"
type="SPINDLE_SPEED" units="REVOLUTION/MINUTE" />
<DataItem category="EVENT" id="S1mode" type="ROTARY_MODE">
<Constraints>
<Value>SPINDLE</Value>
</Constraints>
</DataItem>
<DataItem category="SAMPLE" id="S1load" nativeUnits="PERCENT"
type="LOAD" units="PERCENT" />
<DataItem category="CONDITION" id="spindle" type="SYSTEM" />
</DataItems>
</Rotary>
</Components>
</Axes>
<Controller id="cont" name="controller">
<DataItems>
<DataItem category="CONDITION" id="logic" type="LOGIC_PROGRAM" />
<DataItem category="EVENT" id="estop" type="EMERGENCY_STOP" />
<DataItem category="CONDITION" id="servo" type="ACTUATOR" />
<DataItem category="EVENT" id="message" type="MESSAGE" />
<DataItem category="CONDITION" id="comms" type="COMMUNICATIONS" />
</DataItems>
<Components>
<Path id="path" name="path">
<DataItems>
<DataItem category="SAMPLE" id="SspeedOvr" nativeUnits="PERCENT"
subType="OVERRIDE" type="SPINDLE_SPEED" units="PERCENT" />
<DataItem category="EVENT" id="block" type="BLOCK" />
<DataItem category="EVENT" id="execution" type="EXECUTION" />
<DataItem category="EVENT" id="program" type="PROGRAM" />
<DataItem category="SAMPLE" id="path_feedrate" nativeUnits="MILLIMETER/SECOND"
type="PATH_FEEDRATE" units="MILLIMETER/SECOND" />
<DataItem category="EVENT" id="mode" type="CONTROLLER_MODE" />
<DataItem category="EVENT" id="line" type="LINE" />
<DataItem category="SAMPLE" id="path_pos" nativeUnits="MILLIMETER_3D"
subType="ACTUAL" type="PATH_POSITION" units="MILLIMETER_3D" />
<DataItem category="SAMPLE" id="probe" nativeUnits="MILLIMETER_3D"
subType="PROBE" type="PATH_POSITION" units="MILLIMETER_3D" />
<DataItem category="EVENT" id="part" type="PART_ID" />
<DataItem category="CONDITION" id="motion" type="MOTION_PROGRAM" />
<DataItem category="CONDITION" id="system" type="SYSTEM" />
</DataItems>
</Path>
</Components>
</Controller>
</Components>
</Device>
</Devices>
</MTConnectDevices>
""";

MTConnectDevices mtConnectDevices = XmlUtil.fromXml(xml, MTConnectDevices.class);
InputStream xmlInputStream = MTConnectFileUtil.readFile("mtConnectDevices.xml");
MTConnectDevices mtConnectDevices = XmlUtil.fromXml(xmlInputStream, MTConnectDevices.class);

Assertions.assertNotNull(mtConnectDevices);
Assertions.assertNotNull(mtConnectDevices.getHeader());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,14 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.io.InputStream;

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);
InputStream xmlInputStream = MTConnectFileUtil.readFile("mtConnectError.xml");
MTConnectError mtConnectError = XmlUtil.fromXml(xmlInputStream, MTConnectError.class);

// Validate MTConnectError object
Assertions.assertNotNull(mtConnectError);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.github.protocol.mtconnect.server;

import java.io.InputStream;

public class MTConnectFileUtil {
public static InputStream readFile(String filePath) {
return MTConnectFileUtil.class.getClassLoader().getResourceAsStream(filePath);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,16 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.io.InputStream;

class MTConnectStreamsDecodeTest {

@Test
void testMTConnectStreamsParsing() throws Exception {
String xml = """
<?xml version="1.0" encoding="UTF-8"?>
<MTConnectStreams xmlns:m="urn:mtconnect.org:MTConnectStreams:1.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="urn:mtconnect.org:MTConnectStreams:1.1"
xsi:schemaLocation="urn:mtconnect.org:MTConnectStreams:1.1 http://www.mtconnect.org/schemas/MTConnectStreams_1.1.xsd">
<Header creationTime="2010-04-06T06:53:34+00:00" sender="localhost"
instanceId="1270534765" bufferSize="16" version="1.1" nextSequence="19"
firstSequence="3" lastSequence="18" />
<Streams>
<DeviceStream name="minimal" uuid="1">
<ComponentStream component="Device" name="minimal" componentId="d">
<Events>
<Availability dataItemId="avail" sequence="5" timestamp="2010-04-06T06:19:35.153141">AVAILABLE</Availability>
</Events>
</ComponentStream>
<ComponentStream component="Controller" name="controller" componentId="c1">
<Events>
<EmergencyStop dataItemId="estop" sequence="9" timestamp="2010-04-06T06:20:05.153230">RESET</EmergencyStop>
</Events>
<Condition>
<Normal dataItemId="system" sequence="13" timestamp="2010-04-06T06:21:35.153784" type="SYSTEM" />
</Condition>
</ComponentStream>

<ComponentStream component="Path" name="path" componentId="p1">
<Events>
<Execution dataItemId="execution" sequence="14" timestamp="2010-04-06T06:22:05.153741">ACTIVE</Execution>
</Events>
</ComponentStream>
</DeviceStream>
</Streams>
</MTConnectStreams>
""";
InputStream xmlInputStream = MTConnectFileUtil.readFile("mtConnectStreams.xml");

MTConnectStreams mtConnectStreams = XmlUtil.fromXml(xml, MTConnectStreams.class);
MTConnectStreams mtConnectStreams = XmlUtil.fromXml(xmlInputStream, MTConnectStreams.class);

// Validate MTConnectStreams object
Assertions.assertNotNull(mtConnectStreams);
Expand Down

0 comments on commit 105b0f3

Please sign in to comment.