|
1 | | -import { beforeEachTruncateCollections } from "../../../hooks"; |
| 1 | +import { |
| 2 | + beforeEachLoadFixtures, |
| 3 | + beforeEachTruncateCollections, |
| 4 | +} from "../../../hooks"; |
2 | 5 | import { ApiDecoderListRequest, ApiDecoderListResult } from "../../../../index"; |
3 | 6 |
|
4 | 7 | import { useSdk, sendPayloads } from "../../../helpers"; |
@@ -123,4 +126,111 @@ describe("DecodersController", () => { |
123 | 126 | ]), |
124 | 127 | ); |
125 | 128 | }); |
| 129 | + it("Reroute a payload to the corresponding handler", async () => { |
| 130 | + await beforeEachLoadFixtures(sdk); |
| 131 | + await sdk.query({ |
| 132 | + controller: "device-manager/decoders", |
| 133 | + action: "route", |
| 134 | + body: { |
| 135 | + deviceEUI: "linked2", |
| 136 | + deviceModel: "DummyTempPosition", |
| 137 | + temperature: 45, |
| 138 | + location: { lat: 12, lon: 12, accuracy: 2100 }, |
| 139 | + battery: 0.1, |
| 140 | + }, |
| 141 | + }); |
| 142 | + |
| 143 | + await expect( |
| 144 | + sdk.document.get( |
| 145 | + "device-manager", |
| 146 | + "devices", |
| 147 | + "DummyTempPosition-linked2", |
| 148 | + ), |
| 149 | + ).resolves.toMatchObject({ |
| 150 | + _source: { |
| 151 | + reference: "linked2", |
| 152 | + model: "DummyTempPosition", |
| 153 | + measures: { |
| 154 | + temperature: { type: "temperature", values: { temperature: 45 } }, |
| 155 | + position: { |
| 156 | + type: "position", |
| 157 | + values: { position: { lat: 12, lon: 12 }, accuracy: 2100 }, |
| 158 | + }, |
| 159 | + battery: { type: "battery", values: { battery: 10 } }, |
| 160 | + }, |
| 161 | + engineId: "engine-ayse", |
| 162 | + assetId: "Container-linked2", |
| 163 | + }, |
| 164 | + }); |
| 165 | + }); |
| 166 | + |
| 167 | + it("Reroute and reject with error a DummyTemp payload", async () => { |
| 168 | + const promise = sdk.query({ |
| 169 | + controller: "device-manager/decoders", |
| 170 | + action: "route", |
| 171 | + body: { deviceEUI: null, deviceModel: "DummyTemp", temperature: 21 }, |
| 172 | + }); |
| 173 | + await expect(promise).rejects.toMatchObject({ |
| 174 | + message: 'Invalid payload: missing "deviceEUI"', |
| 175 | + }); |
| 176 | + }); |
| 177 | + it("Reroute and reject if deviceModel missing", async () => { |
| 178 | + const noModelPayload = { deviceEUI: "linked1", temperature: 21 }; |
| 179 | + const promise = sdk.query({ |
| 180 | + controller: "device-manager/decoders", |
| 181 | + action: "route", |
| 182 | + body: noModelPayload, |
| 183 | + }); |
| 184 | + |
| 185 | + await expect(promise).rejects.toMatchObject({ |
| 186 | + message: "Payload must specify the deviceModel for proper routing", |
| 187 | + }); |
| 188 | + await sdk.collection.refresh("device-manager", "payloads"); |
| 189 | + const payloadReceived = await sdk.query({ |
| 190 | + controller: "document", |
| 191 | + action: "search", |
| 192 | + index: "device-manager", |
| 193 | + collection: "payloads", |
| 194 | + sort: { |
| 195 | + "_kuzzle_info.createdAt": "DESC", |
| 196 | + }, |
| 197 | + size: 1, |
| 198 | + }); |
| 199 | + expect(payloadReceived.result.hits[0]._source.payload).toMatchObject( |
| 200 | + noModelPayload, |
| 201 | + ); |
| 202 | + }); |
| 203 | + it("Reroute and reject if decoder missing", async () => { |
| 204 | + const noDecoderPayload = { |
| 205 | + deviceEUI: "linked1", |
| 206 | + deviceModel: "unknownDecoder", |
| 207 | + temperature: 21, |
| 208 | + }; |
| 209 | + const promise = sdk.query({ |
| 210 | + controller: "device-manager/decoders", |
| 211 | + action: "route", |
| 212 | + body: noDecoderPayload, |
| 213 | + }); |
| 214 | + |
| 215 | + await expect(promise).rejects.toMatchObject({ |
| 216 | + message: "The specified device model is unknown", |
| 217 | + }); |
| 218 | + await sdk.collection.refresh("device-manager", "payloads"); |
| 219 | + const payloadReceived = await sdk.query({ |
| 220 | + controller: "document", |
| 221 | + action: "search", |
| 222 | + index: "device-manager", |
| 223 | + collection: "payloads", |
| 224 | + sort: { |
| 225 | + "_kuzzle_info.createdAt": "DESC", |
| 226 | + }, |
| 227 | + size: 1, |
| 228 | + }); |
| 229 | + expect(payloadReceived.result.hits[0]._source.deviceModel).toBe( |
| 230 | + noDecoderPayload.deviceModel, |
| 231 | + ); |
| 232 | + expect(payloadReceived.result.hits[0]._source.payload).toMatchObject( |
| 233 | + noDecoderPayload, |
| 234 | + ); |
| 235 | + }); |
126 | 236 | }); |
0 commit comments