Skip to main content

Testing Module Endpoints

This is the 4th article of the Build Java Module for Mango series. See the full series in the Module Development Overview.

Automated tests help ensure that your endpoints work as expected. Mango uses Mocha (a Node.js test framework) along with the mango-module-tools package to test REST API endpoints against a running Mango instance.

Setup

Create an api-test directory in your module root, and inside it create a device.spec.js file (leave it blank for now).

Create a package.json file in the module root with the following content:

{
"private": true,
"name": "energy-metering",
"version": "4.0.0-SNAPSHOT",
"description": "Energy metering module",
"dependencies": {},
"devDependencies": {
"@infinite-automation/mango-module-tools": "^2.0.5"
},
"author": "YOUR_NAME <YOUR_EMAIL>",
"license": "UNLICENSED",
"com_infiniteautomation": {
"moduleName": "energyMetering"
}
}

Install the dependencies by running:

yarn

Writing Your First Test

Now, add the following test to device.spec.js:

const {createClient, login, uuid, defer, delay} = require('@infinite-automation/mango-module-tools/test-helper/testHelper');
const client = createClient();

describe('Device endpoint tests', () => {
before('Login', () => {
return login.call(this, client);
});

function newDevice() {
return {
xid: uuid(),
name: 'Test device',
protocol: 'MODBUS',
make: 'Test',
model: 'Device',
data: {
foo: 'bar'
},
readPermissions: ['user'],
editPermissions: ['superadmin']
};
}

function assertDevice(saved, local) {
assert.equal(saved.xid, local.xid);
assert.equal(saved.name, local.name);
assert.equal(saved.protocol, local.protocol);
assert.equal(saved.make, local.make);
assert.equal(saved.model, local.model);
assert.equal(saved.data.foo, local.data.foo);
}

it('Creates a device', () => {
const device = newDevice();

return client.restRequest({
path: '/rest/latest/enmet-devices',
method: 'POST',
data: device
})
.then(response => assertDevice(response.data, device))
.finally(() => {
return client.restRequest({
path: `/rest/latest/enmet-devices/${device.xid}`,
method: 'DELETE'
});
});
});
});

This test creates a device via POST, asserts the response matches the input, and cleans up by deleting the device. The finally block ensures cleanup happens even if the assertion fails.

Adding a DELETE Endpoint

Before running the test, you need a DELETE endpoint so the device can be cleaned up after testing. Update DeviceRestController.java to add the delete method:

@ApiOperation(value = "Delete a device")
@RequestMapping(method = RequestMethod.DELETE, value = "/{xid}")
public DeviceModel delete(
@ApiParam(value = "Valid device XID", required = true, allowMultiple = false)
@PathVariable String xid,
@AuthenticationPrincipal User user
) {
return mapping.map(service.delete(xid), user, mapper);
}

Running the Tests

After building the module with the new delete endpoint, you can run the tests. You need a running Mango instance (by default it connects to localhost:8080, but you can change this in the client configuration).

Run mocha from the project root directory:

mocha api-test/*

You should see output like this:

  Device endpoint tests
✓ Creates a device

1 passing (89ms)

Your endpoint is now tested. You can add more tests following the same pattern -- create data, verify it, and clean up.

Continue to RQL Queries to add query and filtering support to your endpoint.