Commit 5db5b0a5 authored by Martti Käärik's avatar Martti Käärik
Browse files

ASN.1 converter test project with Ant/CLI support. Includes single-file...

ASN.1 converter test project with Ant/CLI support. Includes single-file (Rocket.asn) and grouped (ITS with cross-module IMPORTS) test inputs. Tests run via Eclipse antRunner or Maven.
parent 9cdb95a2
Loading
Loading
Loading
Loading
Loading
+23 −0
Original line number Diff line number Diff line
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: org.etsi.mts.tdl.asn1.converter.tests
Bundle-Vendor: ETSI
Bundle-Version: 1.0.0.qualifier
Bundle-SymbolicName: org.etsi.mts.tdl.asn1.converter.tests;singleton:=true
Bundle-ActivationPolicy: lazy
Require-Bundle: org.etsi.mts.tdl.tx,
 org.etsi.mts.tdl.model,
 org.eclipse.xtext.testing,
 org.eclipse.xtext.xbase.testing,
 org.eclipse.xtext.xbase.lib,
 org.eclipse.xtend.lib,
 junit-jupiter-api,
 junit-jupiter-engine,
 junit-platform-launcher,
 junit-platform-engine,
 org.eclipse.equinox.app,
 org.junit,
 org.hamcrest.core
Bundle-RequiredExecutionEnvironment: JavaSE-21
Bundle-ClassPath: .
Export-Package: org.etsi.mts.tdl.asn1.converter.tests;x-internal=true
+171 −0
Original line number Diff line number Diff line
# ASN.1 to TDL Converter Tests

Automated tests that validate the syntactic correctness of TDL output produced by the ASN.1-to-TDL converter (`ttf-045`).

## How it works

1. The test factory scans `src/asn1/` for inputs (see two modes below)
2. For each input, the Python converter is invoked:
   ```
   python asn1_cli.py convert --format tdl -o <temp.tdl> <input>
   ```
3. The generated TDL is parsed by the Xtext-based TDL parser (`org.etsi.mts.tdl.tx`)
4. The test asserts that parsing produces no syntax errors
5. On failure, the full generated TDL is dumped with line numbers alongside error details

### Single-file mode

`.asn` / `.asn1` files placed directly in `src/asn1/` are each converted independently. Use this for self-contained schemas without cross-module imports.

### Grouped mode

Subdirectories in `src/asn1/` are passed as a whole to the converter. The converter scans the directory recursively, compiles all schemas together, and produces one combined TDL output. Use this for schemas that have cross-module `IMPORTS`.

```
src/asn1/
  Rocket.asn                  → single-file test
  ITS/                        → grouped test (all files compiled together)
    CAM-PDU-Descriptions.asn
    DENM-PDU-Descriptions.asn
    ETSI-ITS-CDD.asn
```

## Prerequisites

- **Java 21** (for the Xtext parser / JUnit 5 runtime)
- **Python 3** with the converter's dependencies installed

### Setting up a virtual environment (recommended)

```bash
cd D:/Work/ETSI/TOP/ttf-045
python -m venv .venv

# Activate — pick one:
source .venv/Scripts/activate   # Git Bash on Windows
.venv\Scripts\activate          # cmd / PowerShell on Windows
source .venv/bin/activate       # Linux / macOS

pip install -r requirements.txt
```

The test auto-detects a `.venv` directory inside the converter project — no extra configuration needed. If the venv is elsewhere, point to it explicitly:
```
-Dpython.executable=/path/to/.venv/Scripts/python
```

## Running the tests

### From Eclipse

Right-click `ASN1ConverterParsingTest.java` > **Run As** > **JUnit Test**

If the converter directory is not found automatically, add a VM argument:
```
-Dasn1.converter.dir=D:/Work/ETSI/TOP/ttf-045
```

### Headless / CLI (recommended for quick runs)

The tests can be run standalone using the provided `build.xml` via the Eclipse `antRunner` application. This is the fastest way to run the tests (~15s) as it only compiles and runs this project.

**Command:**

```bash
java -jar <PATH_TO_ECLIPSE>/plugins/org.eclipse.equinox.launcher_<VERSION>.jar \
  -nosplash \
  -application org.eclipse.ant.core.antRunner \
  -buildfile build.xml \
```

To override configuration, pass `-D` flags to the `java` command:
```bash
java -Dasn1.converter.dir=/path/to/ttf-045 -Dpython.executable=python3 \
  -jar <PATH_TO_ECLIPSE>/plugins/org.eclipse.equinox.launcher_<VERSION>.jar \
  -nosplash \
  -application org.eclipse.ant.core.antRunner \
  -buildfile build.xml \
```

Test results are printed to the console and also written to `TEST-*.txt` and `TEST-*.xml` files in the project directory.

### From Maven (full reactor build)

Maven/Tycho requires a full reactor build because of cascading OSGi dependencies. Run from the parent project (`org.etsi.mts.tdl.parent`):

```bash
mvn verify
```

## Configuration

All settings are optional and controlled via system properties (`-D`):

| Property              | Default                          | Description                                |
|-----------------------|----------------------------------|--------------------------------------------|
| `python.executable`   | venv in converter dir, else `python` | Python interpreter path              |
| `asn1.converter.dir`  | auto-resolved relative to CWD   | Path to the `ttf-045` converter project    |
| `asn1.test.files`     | `src/asn1` within this project   | Folder containing ASN.1 input files        |
| `converter.timeout`   | `120` (seconds)                  | Max time to wait for the converter process |

### Path resolution details

The test auto-resolves paths based on `user.dir` (the JVM working directory). If a system property is set, it takes precedence and the auto-resolution is skipped.

**Converter directory** (`asn1.converter.dir`) — tries in order:

| Working directory                | Resolved path          | Scenario                     |
|----------------------------------|------------------------|------------------------------|
| `.../ide/plugins/<this project>` | `../../../ttf-045`     | Running from Eclipse         |
| `.../ide`                        | `../../ttf-045`        | Running from IDE root        |
| `.../TOP`                        | `ttf-045`              | Running from workspace root  |

**Python executable** (`python.executable`) — tries in order:

1. System property `-Dpython.executable=...`
2. `<converter dir>/.venv/Scripts/python.exe` (Windows venv)
3. `<converter dir>/.venv/bin/python` (Unix venv)
4. `python` on `PATH`

**ASN.1 test files** (`asn1.test.files`) — tries in order:

1. System property `-Dasn1.test.files=...`
2. Classpath resource `/asn1`
3. `<user.dir>/src/asn1`
4. `<user.dir>/plugins/org.etsi.mts.tdl.asn1.converter.tests/src/asn1`

The expected workspace layout is:
```
TOP/
  ttf-045/                    Converter project (with optional .venv/)
  ide/
    plugins/
      org.etsi.mts.tdl.asn1.converter.tests/
        src/asn1/             ASN.1 test inputs
```

## Adding test cases

No code changes required — the `@TestFactory` discovers inputs automatically.

- **Single file:** drop a self-contained `.asn` / `.asn1` file into `src/asn1/`
- **Group with imports:** create a subdirectory under `src/asn1/` and place all related schemas in it

To test against an external ASN.1 collection:
```
-Dasn1.test.files="D:/Work/ETSI/TOP/ASN.1 specs/Modules/ITS/r2"
```

## Project structure

```
org.etsi.mts.tdl.asn1.converter.tests/
  META-INF/MANIFEST.MF          OSGi bundle manifest
  pom.xml                        Maven/Tycho build config
  src/
    asn1/
      Rocket.asn                 Initial test input (simple schema)
    org/etsi/mts/tdl/asn1/converter/tests/
      ASN1ConverterParsingTest.java       Test class (@TestFactory)
      ASN1ConverterInjectorProvider.java  Xtext/Guice setup
```
+3 −0
Original line number Diff line number Diff line
source.. = src/
bin.includes = META-INF/,\
               .
+67 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="UTF-8"?>
<project name="ASN1ConverterTests" default="run">
    <property name="eclipse.home" value="D:/apps/eclipse-modeling-2025-12-R-win32-x86_64_dev/eclipse"/>
    <property name="plugins.dir" value="${eclipse.home}/plugins"/>
    <property name="workspace.dir" value="${basedir}/.."/>

    <!-- Converter settings — override with -Dasn1.converter.dir=... etc. -->
    <property name="asn1.converter.dir" value="${basedir}/../../../ttf-045"/>
    <property name="python.executable" value="python"/>
    <property name="converter.timeout" value="120"/>

    <path id="test.classpath">
        <fileset dir="${plugins.dir}">
            <include name="*.jar"/>
        </fileset>
        <fileset dir="${workspace.dir}/org.etsi.mts.tdl.standalone/target/libs">
            <include name="*.jar"/>
        </fileset>
        <pathelement location="${basedir}/bin"/>
        <pathelement location="${workspace.dir}/org.etsi.mts.tdl.tx/bin"/>
        <pathelement location="${workspace.dir}/org.etsi.mts.tdl.model/bin"/>
        <pathelement location="${workspace.dir}/org.etsi.mts.tdl.model/src-gen"/>
        <pathelement location="${workspace.dir}/org.etsi.mts.tdl.common/bin"/>
        <pathelement location="${workspace.dir}/org.etsi.mts.tdl.constraints/bin"/>
    </path>

    <target name="clean">
        <delete dir="${basedir}/bin"/>
    </target>

    <target name="compile" depends="clean">
        <echo message="Compiling tests and copying resources..."/>
        <mkdir dir="${basedir}/bin"/>
        <copy todir="${basedir}/bin">
            <fileset dir="${basedir}/src">
                <include name="**/*.asn"/>
                <include name="**/*.asn1"/>
            </fileset>
        </copy>
        <javac destdir="${basedir}/bin"
               debug="true"
               includeantruntime="false"
               failonerror="true">
            <src path="${basedir}/src"/>
            <classpath refid="test.classpath"/>
        </javac>
    </target>

    <target name="run" depends="compile">
        <echo message="Running ASN1ConverterParsingTest..."/>
        <echo message="  Converter dir: ${asn1.converter.dir}"/>
        <echo message="  Python: ${python.executable}"/>
        <junitlauncher failureProperty="tests.failed" printSummary="true">
            <classpath refid="test.classpath"/>
            <test name="org.etsi.mts.tdl.asn1.converter.tests.ASN1ConverterParsingTest"
                  outputDir="${basedir}">
                <listener type="legacy-plain" sendSysOut="true" sendSysErr="true"/>
                <listener type="legacy-xml" sendSysErr="true"/>
            </test>
        </junitlauncher>
        <concat taskname="test-report">
            <fileset dir="${basedir}"
                     includes="TEST-org.etsi.mts.tdl.asn1.converter.tests.ASN1ConverterParsingTest.txt"/>
        </concat>
        <fail if="tests.failed" message="Tests failed — see report above."/>
    </target>
</project>
+32 −0
Original line number Diff line number Diff line
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.etsi.mts.tdl</groupId>
		<artifactId>org.etsi.mts.tdl.parent</artifactId>
		<version>1.0.0-SNAPSHOT</version>
		<relativePath>../../org.etsi.mts.tdl.parent</relativePath>
	</parent>
	<artifactId>org.etsi.mts.tdl.asn1.converter.tests</artifactId>
	<packaging>eclipse-test-plugin</packaging>

	<name>ASN.1 to TDL Converter Tests</name>

	<build>
		<plugins>
			<plugin>
				<groupId>org.eclipse.xtend</groupId>
				<artifactId>xtend-maven-plugin</artifactId>
			</plugin>
			<plugin>
				<groupId>org.eclipse.tycho</groupId>
				<artifactId>tycho-surefire-plugin</artifactId>
				<configuration>
					<useUIHarness>false</useUIHarness>
					<useUIThread>false</useUIThread>
					<failIfNoTests>false</failIfNoTests>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>
Loading