From 0a5a4e217dbd0170743393c3da0fea6023e86bf5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sim=C3=A3o=20Mata?= Date: Tue, 4 Feb 2020 18:26:32 +0100 Subject: [PATCH] Allow provision of device with same deviceId but different ecus MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This should most likely not be allowed, but the old director allows it and it's not possible to determine if anyone is doing this. Signed-off-by: Simão Mata --- .../director/db/Repository.scala | 14 +++++++++++ .../director/http/DeviceResourceSpec.scala | 24 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/main/scala/com/advancedtelematic/director/db/Repository.scala b/src/main/scala/com/advancedtelematic/director/db/Repository.scala index c9d4658..4c09a62 100644 --- a/src/main/scala/com/advancedtelematic/director/db/Repository.scala +++ b/src/main/scala/com/advancedtelematic/director/db/Repository.scala @@ -39,7 +39,21 @@ trait DeviceRepositorySupport extends DatabaseSupport { protected class DeviceRepository()(implicit val db: Database, val ec: ExecutionContext) { def create(ns: Namespace, deviceId: DeviceId, primaryEcuId: EcuIdentifier, ecus: Seq[Ecu]): Future[Unit] = { + val ecusDeleteIO = + Schema.ecus + .filter(_.namespace === ns) + .filter(_.deviceId === deviceId) + .filter(_.ecuSerial.inSet(ecus.map(_.ecuSerial))) + .delete + + val deviceDeleteIo = + Schema.devices + .filter(_.namespace === ns) + .filter(_.id === deviceId) + .delete + val io = for { + _ <- ecusDeleteIO.andThen(deviceDeleteIo) // This is a bad idea and will fail if device has assignments, see DeviceResourceSpec _ <- Schema.ecus ++= ecus _ <- Schema.devices += Device(ns, deviceId, primaryEcuId) } yield () diff --git a/src/test/scala/com/advancedtelematic/director/http/DeviceResourceSpec.scala b/src/test/scala/com/advancedtelematic/director/http/DeviceResourceSpec.scala index 956b8de..7edf5dd 100644 --- a/src/test/scala/com/advancedtelematic/director/http/DeviceResourceSpec.scala +++ b/src/test/scala/com/advancedtelematic/director/http/DeviceResourceSpec.scala @@ -91,6 +91,30 @@ class DeviceResourceSpec extends DirectorSpec registerDeviceOk() } + // TODO: Legacy, this should not be possible + // https://saeljira.it.here.com/browse/OTA-441 + // https://saeljira.it.here.com/browse/OTA-2517 + testWithNamespace("registering the same device id with different ecus works") { implicit ns => + createRepoOk() + + val deviceId = DeviceId.generate() + val ecus = GenRegisterEcu.generate + val primaryEcu = ecus.ecu_serial + val req = RegisterDevice(deviceId.some, primaryEcu, Seq(ecus)) + + val ecus2 = GenRegisterEcu.generate + val primaryEcu2 = ecus2.ecu_serial + val req2 = RegisterDevice(deviceId.some, primaryEcu2, Seq(ecus2)) + + Post(apiUri(s"device/${deviceId.show}/ecus"), req).namespaced ~> routes ~> check { + status shouldBe StatusCodes.Created + } + + Post(apiUri(s"device/${deviceId.show}/ecus"), req2).namespaced ~> routes ~> check { + status shouldBe StatusCodes.Created + } + } + testWithRepo("fails when primary ecu is not defined in ecus") { implicit ns => val ecus = GenRegisterEcu.generate val primaryEcu = GenEcuIdentifier.generate