use of org.eclipse.hono.service.management.device.DeviceDto in project hono by eclipse.
the class MongoDbBasedDeviceDaoTest method testCreateSetsCreationDate.
/**
* Verifies that the DAO sets the initial version and creation date when creating a device.
*
* @param ctx The vert.x test context.
*/
@Test
public void testCreateSetsCreationDate(final VertxTestContext ctx) {
when(mongoClient.insert(anyString(), any(JsonObject.class))).thenReturn(Future.succeededFuture("initial-version"));
final var dto = DeviceDto.forCreation(DeviceDto::new, "tenantId", "deviceId", new Device(), "initial-version");
dao.create(dto, NoopSpan.INSTANCE.context()).onComplete(ctx.succeeding(version -> {
ctx.verify(() -> {
assertThat(version).isEqualTo("initial-version");
final var document = ArgumentCaptor.forClass(JsonObject.class);
verify(mongoClient).insert(eq("devices"), document.capture());
MongoDbBasedTenantDaoTest.assertCreationDocumentContainsStatusProperties(document.getValue(), "initial-version");
});
ctx.completeNow();
}));
}
use of org.eclipse.hono.service.management.device.DeviceDto in project hono by eclipse.
the class MongoDbBasedDeviceDao method update.
/**
* {@inheritDoc}
*/
@Override
public Future<String> update(final DeviceDto deviceConfig, final Optional<String> resourceVersion, final SpanContext tracingContext) {
Objects.requireNonNull(deviceConfig);
Objects.requireNonNull(resourceVersion);
final Span span = tracer.buildSpan("update Device").addReference(References.CHILD_OF, tracingContext).withTag(TracingHelper.TAG_TENANT_ID, deviceConfig.getTenantId()).withTag(TracingHelper.TAG_DEVICE_ID, deviceConfig.getDeviceId()).start();
resourceVersion.ifPresent(v -> TracingHelper.TAG_RESOURCE_VERSION.set(span, v));
final JsonObject updateDeviceQuery = MongoDbDocumentBuilder.builder().withVersion(resourceVersion).withTenantId(deviceConfig.getTenantId()).withDeviceId(deviceConfig.getDeviceId()).document();
final var document = JsonObject.mapFrom(deviceConfig);
if (LOG.isTraceEnabled()) {
LOG.trace("replacing existing device document with:{}{}", System.lineSeparator(), document.encodePrettily());
}
return mongoClient.findOneAndReplaceWithOptions(collectionName, updateDeviceQuery, document, new FindOptions(), new UpdateOptions().setReturningNewDocument(true)).compose(result -> {
if (result == null) {
return MongoDbBasedDao.checkForVersionMismatchAndFail(deviceConfig.getDeviceId(), resourceVersion, getById(deviceConfig.getTenantId(), deviceConfig.getDeviceId(), span));
} else {
span.log("successfully updated device");
return Future.succeededFuture(result.getString(DeviceDto.FIELD_VERSION));
}
}).onFailure(t -> TracingHelper.logError(span, "error updating device", t)).recover(this::mapError).onComplete(r -> span.finish());
}
use of org.eclipse.hono.service.management.device.DeviceDto in project hono by eclipse.
the class MongoDbBasedDeviceDaoTest method testUpdateSetsLastUpdate.
/**
* Verifies that the DAO sets the new version and last update time but also keeps the original
* creation date when updating a device.
*
* @param ctx The vert.x test context.
*/
@Test
public void testUpdateSetsLastUpdate(final VertxTestContext ctx) {
final var existingRecord = DeviceDto.forRead(DeviceDto::new, "tenantId", "deviceId", new Device(), false, false, Instant.now().minusSeconds(60).truncatedTo(ChronoUnit.SECONDS), null, "initial-version");
when(mongoClient.findOneAndReplaceWithOptions(anyString(), any(JsonObject.class), any(JsonObject.class), any(FindOptions.class), any(UpdateOptions.class))).thenReturn(Future.succeededFuture(new JsonObject().put(DeviceDto.FIELD_VERSION, "new-version")));
final var dto = DeviceDto.forUpdate(() -> existingRecord, "tenantId", "deviceId", new Device(), "new-version");
dao.update(dto, Optional.of(existingRecord.getVersion()), NoopSpan.INSTANCE.context()).onComplete(ctx.succeeding(newVersion -> {
ctx.verify(() -> {
final var document = ArgumentCaptor.forClass(JsonObject.class);
verify(mongoClient).findOneAndReplaceWithOptions(eq("devices"), any(JsonObject.class), document.capture(), any(FindOptions.class), any(UpdateOptions.class));
MongoDbBasedTenantDaoTest.assertUpdateDocumentContainsStatusProperties(document.getValue(), "new-version", existingRecord.getCreationTime());
});
ctx.completeNow();
}));
}
use of org.eclipse.hono.service.management.device.DeviceDto in project hono by eclipse.
the class MongoDbBasedDeviceDao method create.
/**
* {@inheritDoc}
*/
@Override
public Future<String> create(final DeviceDto deviceConfig, final SpanContext tracingContext) {
Objects.requireNonNull(deviceConfig);
final Span span = tracer.buildSpan("create Device").addReference(References.CHILD_OF, tracingContext).withTag(TracingHelper.TAG_TENANT_ID, deviceConfig.getTenantId()).withTag(TracingHelper.TAG_DEVICE_ID, deviceConfig.getDeviceId()).start();
return mongoClient.insert(collectionName, JsonObject.mapFrom(deviceConfig)).map(success -> {
span.log("successfully created device");
LOG.debug("successfully created device [tenant: {}, device-id: {}, resource-version: {}]", deviceConfig.getTenantId(), deviceConfig.getDeviceId(), deviceConfig.getVersion());
return deviceConfig.getVersion();
}).recover(error -> {
if (MongoDbBasedDao.isDuplicateKeyError(error)) {
LOG.debug("device [{}] already exists for tenant [{}]", deviceConfig.getDeviceId(), deviceConfig.getTenantId(), error);
TracingHelper.logError(span, "device already exists");
throw new ClientErrorException(deviceConfig.getTenantId(), HttpURLConnection.HTTP_CONFLICT, "device already exists");
} else {
TracingHelper.logError(span, "error creating device", error);
return mapError(error);
}
}).onComplete(r -> span.finish());
}
Aggregations