Search in sources :

Example 1 with Credentials

use of org.eclipse.hono.service.management.credentials.Credentials in project hono by eclipse.

the class MongoDbBasedCredentialServiceTest method testCredentialsDaoUsesIndex.

/**
 * Verifies that the credentials DAO uses a proper index to retrieve credentials by auth-id and type.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testCredentialsDaoUsesIndex(final VertxTestContext ctx) {
    final var tenantId = UUID.randomUUID().toString();
    final MongoClient mongoClient = MongoDbTestUtils.getMongoClient(vertx, DB_NAME);
    final var dto1 = CredentialsDto.forCreation(tenantId, UUID.randomUUID().toString(), List.of(Credentials.createPasswordCredential("device1a", "secret"), Credentials.createPSKCredential("device1b", "shared-secret")), UUID.randomUUID().toString());
    final var dto2 = CredentialsDto.forCreation(tenantId, UUID.randomUUID().toString(), List.of(Credentials.createPasswordCredential("device2a", "secret"), Credentials.createPSKCredential("device2b", "shared-secret")), UUID.randomUUID().toString());
    final var dto3 = CredentialsDto.forCreation(tenantId, UUID.randomUUID().toString(), List.of(Credentials.createPasswordCredential("device3a", "secret"), Credentials.createPSKCredential("device3b", "shared-secret")), UUID.randomUUID().toString());
    final var dto4 = CredentialsDto.forCreation(UUID.randomUUID().toString(), UUID.randomUUID().toString(), List.of(Credentials.createPasswordCredential("device1a", "secret"), Credentials.createPSKCredential("device1b", "shared-secret")), UUID.randomUUID().toString());
    credentialsDao.create(dto1, NoopSpan.INSTANCE.context()).compose(ok -> credentialsDao.create(dto2, NoopSpan.INSTANCE.context())).compose(ok -> credentialsDao.create(dto3, NoopSpan.INSTANCE.context())).compose(ok -> credentialsDao.create(dto4, NoopSpan.INSTANCE.context())).compose(ok -> {
        final Promise<JsonObject> resultHandler = Promise.promise();
        final var filter = MongoDbDocumentBuilder.builder().withTenantId(tenantId).withAuthId("device1a").withType(CredentialsConstants.SECRETS_TYPE_HASHED_PASSWORD).document();
        final var commandRight = new JsonObject().put("find", "credentials").put("batchSize", 1).put("singleBatch", true).put("filter", filter).put("projection", MongoDbBasedCredentialsDao.PROJECTION_CREDS_BY_TYPE_AND_AUTH_ID);
        final var explain = new JsonObject().put("explain", commandRight).put("verbosity", "executionStats");
        mongoClient.runCommand("explain", explain, resultHandler);
        return resultHandler.future();
    }).onComplete(ctx.succeeding(result -> {
        if (LOG.isTraceEnabled()) {
            LOG.trace("result:{}{}", System.lineSeparator(), result.encodePrettily());
        }
        ctx.verify(() -> {
            final var indexScan = (JsonObject) JsonPointer.from("/queryPlanner/winningPlan/inputStage/inputStage").queryJson(result);
            assertThat(indexScan.getString("indexName")).isEqualTo(MongoDbBasedCredentialsDao.IDX_CREDENTIALS_TYPE_AND_AUTH_ID);
            final var executionStats = result.getJsonObject("executionStats", new JsonObject());
            // there are two credentials with auth-id "device1a" and type "hashed-password"
            assertThat(executionStats.getInteger("totalKeysExamined")).isEqualTo(2);
            assertThat(executionStats.getInteger("totalDocsExamined")).isEqualTo(2);
        });
        ctx.completeNow();
    }));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) BeforeEach(org.junit.jupiter.api.BeforeEach) CredentialsManagementService(org.eclipse.hono.service.management.credentials.CredentialsManagementService) LoggerFactory(org.slf4j.LoggerFactory) Tenant(org.eclipse.hono.service.management.tenant.Tenant) Timeout(io.vertx.junit5.Timeout) AfterAll(org.junit.jupiter.api.AfterAll) TestInstance(org.junit.jupiter.api.TestInstance) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) BeforeAll(org.junit.jupiter.api.BeforeAll) JsonObject(io.vertx.core.json.JsonObject) TenantInformationService(org.eclipse.hono.deviceregistry.service.tenant.TenantInformationService) MongoClient(io.vertx.ext.mongo.MongoClient) MongoDbBasedRegistrationConfigProperties(org.eclipse.hono.deviceregistry.mongodb.config.MongoDbBasedRegistrationConfigProperties) UUID(java.util.UUID) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) TestInfo(org.junit.jupiter.api.TestInfo) Test(org.junit.jupiter.api.Test) List(java.util.List) SpringBasedHonoPasswordEncoder(org.eclipse.hono.auth.SpringBasedHonoPasswordEncoder) Optional(java.util.Optional) TenantKey(org.eclipse.hono.deviceregistry.service.tenant.TenantKey) Assertions(org.eclipse.hono.deviceregistry.util.Assertions) OperationResult(org.eclipse.hono.service.management.OperationResult) Checkpoint(io.vertx.junit5.Checkpoint) JsonPointer(io.vertx.core.json.pointer.JsonPointer) Mockito.mock(org.mockito.Mockito.mock) MongoDbDocumentBuilder(org.eclipse.hono.deviceregistry.mongodb.utils.MongoDbDocumentBuilder) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) VertxTestContext(io.vertx.junit5.VertxTestContext) Credentials(org.eclipse.hono.service.management.credentials.Credentials) CredentialsServiceTestBase(org.eclipse.hono.service.credentials.CredentialsServiceTestBase) DeviceManagementService(org.eclipse.hono.service.management.device.DeviceManagementService) CompositeFuture(io.vertx.core.CompositeFuture) MongoDbBasedCredentialsDao(org.eclipse.hono.deviceregistry.mongodb.model.MongoDbBasedCredentialsDao) RegistrationLimits(org.eclipse.hono.service.management.tenant.RegistrationLimits) Logger(org.slf4j.Logger) Promise(io.vertx.core.Promise) Vertx(io.vertx.core.Vertx) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) MongoDbBasedCredentialsConfigProperties(org.eclipse.hono.deviceregistry.mongodb.config.MongoDbBasedCredentialsConfigProperties) CredentialsConstants(org.eclipse.hono.util.CredentialsConstants) TimeUnit(java.util.concurrent.TimeUnit) AfterEach(org.junit.jupiter.api.AfterEach) CredentialsDto(org.eclipse.hono.service.management.credentials.CredentialsDto) CredentialsService(org.eclipse.hono.service.credentials.CredentialsService) MongoDbBasedDeviceDao(org.eclipse.hono.deviceregistry.mongodb.model.MongoDbBasedDeviceDao) NoopSpan(io.opentracing.noop.NoopSpan) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) MongoClient(io.vertx.ext.mongo.MongoClient) Promise(io.vertx.core.Promise) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.jupiter.api.Test)

Example 2 with Credentials

use of org.eclipse.hono.service.management.credentials.Credentials in project hono by eclipse.

the class CredentialsManagementIT method testAddCredentialsSucceedsForAdditionalProperties.

/**
 * Verifies that the service accepts an add credentials request containing a clear text password.
 *
 * @param context The vert.x test context.
 */
@Test
public void testAddCredentialsSucceedsForAdditionalProperties(final VertxTestContext context) {
    final PasswordCredential credential = IntegrationTestSupport.createPasswordCredential(authId, "thePassword");
    credential.getExtensions().put("client-id", "MQTT-client-2384236854");
    registry.addCredentials(tenantId, deviceId, List.of(credential)).compose(httpResponse -> {
        context.verify(() -> assertResourceVersionHasChanged(resourceVersion, httpResponse.headers()));
        return registry.getCredentials(tenantId, deviceId);
    }).onComplete(context.succeeding(httpResponse -> {
        context.verify(() -> {
            final JsonArray response = httpResponse.bodyAsJsonArray();
            assertThat(response.size()).isEqualTo(1);
            final JsonObject credentialObject = response.getJsonObject(0);
            final var ext = credentialObject.getJsonObject(RegistryManagementConstants.FIELD_EXT);
            assertThat(ext).isNotNull();
            assertThat(ext.getString("client-id")).isEqualTo("MQTT-client-2384236854");
            // the device-id must not be part of the "ext" section
            assertThat(ext.getString("device-id")).isNull();
        });
        context.completeNow();
    }));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) VertxTestContext(io.vertx.junit5.VertxTestContext) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) BCryptPasswordEncoder(org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder) LoggerFactory(org.slf4j.LoggerFactory) MultiMap(io.vertx.core.MultiMap) Credentials(org.eclipse.hono.service.management.credentials.Credentials) GenericCredential(org.eclipse.hono.service.management.credentials.GenericCredential) PasswordSecret(org.eclipse.hono.service.management.credentials.PasswordSecret) X509CertificateCredential(org.eclipse.hono.service.management.credentials.X509CertificateCredential) OptionalInt(java.util.OptionalInt) AtomicReference(java.util.concurrent.atomic.AtomicReference) Timeout(io.vertx.junit5.Timeout) ArrayList(java.util.ArrayList) IntegrationTestSupport(org.eclipse.hono.tests.IntegrationTestSupport) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) PskCredential(org.eclipse.hono.service.management.credentials.PskCredential) Map(java.util.Map) Assertions(org.assertj.core.api.Assertions) HttpUtils(org.eclipse.hono.service.http.HttpUtils) JsonObject(io.vertx.core.json.JsonObject) RegistryManagementConstants(org.eclipse.hono.util.RegistryManagementConstants) Logger(org.slf4j.Logger) RecursiveComparisonConfiguration(org.assertj.core.api.recursive.comparison.RecursiveComparisonConfiguration) X509CertificateSecret(org.eclipse.hono.service.management.credentials.X509CertificateSecret) HttpHeaders(io.vertx.core.http.HttpHeaders) UUID(java.util.UUID) Truth.assertThat(com.google.common.truth.Truth.assertThat) Instant(java.time.Instant) VertxExtension(io.vertx.junit5.VertxExtension) Collectors(java.util.stream.Collectors) CredentialsConstants(org.eclipse.hono.util.CredentialsConstants) TestInfo(org.junit.jupiter.api.TestInfo) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) JsonArray(io.vertx.core.json.JsonArray) List(java.util.List) ChronoUnit(java.time.temporal.ChronoUnit) CommonCredential(org.eclipse.hono.service.management.credentials.CommonCredential) CrudHttpClient(org.eclipse.hono.tests.CrudHttpClient) DeviceRegistryHttpClient(org.eclipse.hono.tests.DeviceRegistryHttpClient) Optional(java.util.Optional) GenericSecret(org.eclipse.hono.service.management.credentials.GenericSecret) PasswordCredential(org.eclipse.hono.service.management.credentials.PasswordCredential) JsonArray(io.vertx.core.json.JsonArray) PasswordCredential(org.eclipse.hono.service.management.credentials.PasswordCredential) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.jupiter.api.Test)

Example 3 with Credentials

use of org.eclipse.hono.service.management.credentials.Credentials in project hono by eclipse.

the class CredentialsManagementIT method testAddCredentialsSucceeds.

/**
 * Verifies that a newly added device has an empty set of credentials and that the
 * service successfully adds arbitrary types of credentials.
 *
 * @param context The vert.x test context.
 */
@Test
public void testAddCredentialsSucceeds(final VertxTestContext context) {
    final PasswordCredential pwdCredential = IntegrationTestSupport.createPasswordCredential(authId, "thePassword");
    pwdCredential.getExtensions().put("client-id", "MQTT-client-2384236854");
    final PskCredential pskCredential = IntegrationTestSupport.createPskCredentials("psk-id", "psk-key");
    final var x509Credential = X509CertificateCredential.fromSubjectDn("emailAddress=foo@bar.com, CN=foo, O=bar", List.of(new X509CertificateSecret()));
    x509Credential.setComment("non-standard attribute type");
    final List<CommonCredential> credentials = List.of(pwdCredential, pskCredential, x509Credential);
    registry.getCredentials(tenantId, deviceId).compose(httpResponse -> {
        context.verify(() -> {
            assertResourceVersionHasChanged(resourceVersion, httpResponse.headers());
            assertThat(httpResponse.bodyAsJsonArray()).isEmpty();
        });
        return registry.addCredentials(tenantId, deviceId, credentials);
    }).compose(httpResponse -> {
        context.verify(() -> assertResourceVersionHasChanged(resourceVersion, httpResponse.headers()));
        return registry.getCredentials(tenantId, deviceId);
    }).onComplete(context.succeeding(httpResponse -> {
        context.verify(() -> {
            final CommonCredential[] credsOnRecord = httpResponse.bodyAsJson(CommonCredential[].class);
            assertThat(credsOnRecord).hasLength(3);
            Arrays.stream(credsOnRecord).forEach(creds -> {
                assertThat(creds.getExtensions().get("device-id")).isNull();
                if (creds instanceof PasswordCredential) {
                    assertThat(creds.getExtensions().get("client-id")).isEqualTo("MQTT-client-2384236854");
                } else if (creds instanceof X509CertificateCredential) {
                    assertThat(creds.getComment()).isEqualTo("non-standard attribute type");
                }
                creds.getSecrets().forEach(secret -> {
                    assertThat(secret.isEnabled()).isTrue();
                    assertThat(secret.getId()).isNotNull();
                });
            });
        });
        context.completeNow();
    }));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) VertxTestContext(io.vertx.junit5.VertxTestContext) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) BCryptPasswordEncoder(org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder) LoggerFactory(org.slf4j.LoggerFactory) MultiMap(io.vertx.core.MultiMap) Credentials(org.eclipse.hono.service.management.credentials.Credentials) GenericCredential(org.eclipse.hono.service.management.credentials.GenericCredential) PasswordSecret(org.eclipse.hono.service.management.credentials.PasswordSecret) X509CertificateCredential(org.eclipse.hono.service.management.credentials.X509CertificateCredential) OptionalInt(java.util.OptionalInt) AtomicReference(java.util.concurrent.atomic.AtomicReference) Timeout(io.vertx.junit5.Timeout) ArrayList(java.util.ArrayList) IntegrationTestSupport(org.eclipse.hono.tests.IntegrationTestSupport) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) PskCredential(org.eclipse.hono.service.management.credentials.PskCredential) Map(java.util.Map) Assertions(org.assertj.core.api.Assertions) HttpUtils(org.eclipse.hono.service.http.HttpUtils) JsonObject(io.vertx.core.json.JsonObject) RegistryManagementConstants(org.eclipse.hono.util.RegistryManagementConstants) Logger(org.slf4j.Logger) RecursiveComparisonConfiguration(org.assertj.core.api.recursive.comparison.RecursiveComparisonConfiguration) X509CertificateSecret(org.eclipse.hono.service.management.credentials.X509CertificateSecret) HttpHeaders(io.vertx.core.http.HttpHeaders) UUID(java.util.UUID) Truth.assertThat(com.google.common.truth.Truth.assertThat) Instant(java.time.Instant) VertxExtension(io.vertx.junit5.VertxExtension) Collectors(java.util.stream.Collectors) CredentialsConstants(org.eclipse.hono.util.CredentialsConstants) TestInfo(org.junit.jupiter.api.TestInfo) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) JsonArray(io.vertx.core.json.JsonArray) List(java.util.List) ChronoUnit(java.time.temporal.ChronoUnit) CommonCredential(org.eclipse.hono.service.management.credentials.CommonCredential) CrudHttpClient(org.eclipse.hono.tests.CrudHttpClient) DeviceRegistryHttpClient(org.eclipse.hono.tests.DeviceRegistryHttpClient) Optional(java.util.Optional) GenericSecret(org.eclipse.hono.service.management.credentials.GenericSecret) PasswordCredential(org.eclipse.hono.service.management.credentials.PasswordCredential) PskCredential(org.eclipse.hono.service.management.credentials.PskCredential) X509CertificateCredential(org.eclipse.hono.service.management.credentials.X509CertificateCredential) PasswordCredential(org.eclipse.hono.service.management.credentials.PasswordCredential) CommonCredential(org.eclipse.hono.service.management.credentials.CommonCredential) X509CertificateSecret(org.eclipse.hono.service.management.credentials.X509CertificateSecret) Test(org.junit.jupiter.api.Test)

Example 4 with Credentials

use of org.eclipse.hono.service.management.credentials.Credentials in project hono by eclipse.

the class CredentialsManagementIT method testGetAllCredentialsForDeviceSucceeds.

/**
 * Verifies that the service returns all credentials registered for a given device regardless of
 * authentication identifier and type.
 * <p>
 * The returned JsonArray must contain exactly the same credentials as originally added.
 *
 * @param context The vert.x test context.
 */
@Test
public void testGetAllCredentialsForDeviceSucceeds(final VertxTestContext context) {
    final List<CommonCredential> credentialsListToAdd = new ArrayList<>();
    credentialsListToAdd.add(pskCredentials);
    credentialsListToAdd.add(hashedPasswordCredential);
    credentialsListToAdd.add(X509CertificateCredential.fromSubjectDn("CN=Acme", List.of(new X509CertificateSecret())));
    for (int i = 0; i < 3; i++) {
        final GenericSecret secret = new GenericSecret();
        secret.setAdditionalProperties(Map.of("field-" + i, "setec astronomy"));
        final GenericCredential credential = new GenericCredential("type-" + i, getRandomAuthId(PREFIX_AUTH_ID), List.of(secret));
        credentialsListToAdd.add(credential);
    }
    registry.addCredentials(tenantId, deviceId, credentialsListToAdd).compose(ar -> registry.getCredentials(tenantId, deviceId)).onComplete(context.succeeding(httpResponse -> {
        context.verify(() -> assertResponseBodyContainsAllCredentials(httpResponse.bodyAsJsonArray(), credentialsListToAdd));
        context.completeNow();
    }));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) VertxTestContext(io.vertx.junit5.VertxTestContext) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) BCryptPasswordEncoder(org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder) LoggerFactory(org.slf4j.LoggerFactory) MultiMap(io.vertx.core.MultiMap) Credentials(org.eclipse.hono.service.management.credentials.Credentials) GenericCredential(org.eclipse.hono.service.management.credentials.GenericCredential) PasswordSecret(org.eclipse.hono.service.management.credentials.PasswordSecret) X509CertificateCredential(org.eclipse.hono.service.management.credentials.X509CertificateCredential) OptionalInt(java.util.OptionalInt) AtomicReference(java.util.concurrent.atomic.AtomicReference) Timeout(io.vertx.junit5.Timeout) ArrayList(java.util.ArrayList) IntegrationTestSupport(org.eclipse.hono.tests.IntegrationTestSupport) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) PskCredential(org.eclipse.hono.service.management.credentials.PskCredential) Map(java.util.Map) Assertions(org.assertj.core.api.Assertions) HttpUtils(org.eclipse.hono.service.http.HttpUtils) JsonObject(io.vertx.core.json.JsonObject) RegistryManagementConstants(org.eclipse.hono.util.RegistryManagementConstants) Logger(org.slf4j.Logger) RecursiveComparisonConfiguration(org.assertj.core.api.recursive.comparison.RecursiveComparisonConfiguration) X509CertificateSecret(org.eclipse.hono.service.management.credentials.X509CertificateSecret) HttpHeaders(io.vertx.core.http.HttpHeaders) UUID(java.util.UUID) Truth.assertThat(com.google.common.truth.Truth.assertThat) Instant(java.time.Instant) VertxExtension(io.vertx.junit5.VertxExtension) Collectors(java.util.stream.Collectors) CredentialsConstants(org.eclipse.hono.util.CredentialsConstants) TestInfo(org.junit.jupiter.api.TestInfo) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) JsonArray(io.vertx.core.json.JsonArray) List(java.util.List) ChronoUnit(java.time.temporal.ChronoUnit) CommonCredential(org.eclipse.hono.service.management.credentials.CommonCredential) CrudHttpClient(org.eclipse.hono.tests.CrudHttpClient) DeviceRegistryHttpClient(org.eclipse.hono.tests.DeviceRegistryHttpClient) Optional(java.util.Optional) GenericSecret(org.eclipse.hono.service.management.credentials.GenericSecret) PasswordCredential(org.eclipse.hono.service.management.credentials.PasswordCredential) GenericCredential(org.eclipse.hono.service.management.credentials.GenericCredential) CommonCredential(org.eclipse.hono.service.management.credentials.CommonCredential) ArrayList(java.util.ArrayList) X509CertificateSecret(org.eclipse.hono.service.management.credentials.X509CertificateSecret) GenericSecret(org.eclipse.hono.service.management.credentials.GenericSecret) Test(org.junit.jupiter.api.Test)

Example 5 with Credentials

use of org.eclipse.hono.service.management.credentials.Credentials in project hono by eclipse.

the class CredentialsManagementIT method testUpdateCredentialsSucceeds.

/**
 * Verifies that the service accepts an update credentials request for existing credentials.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testUpdateCredentialsSucceeds(final VertxTestContext ctx) {
    final AtomicReference<PasswordSecret> originalSecret = new AtomicReference<>();
    // GIVEN a device with a set of hashed password credentials
    registry.addCredentials(tenantId, deviceId, List.of(hashedPasswordCredential)).compose(ok -> registry.getCredentials(tenantId, deviceId)).compose(httpResponse -> {
        ctx.verify(() -> assertResourceVersionHasChanged(resourceVersion, httpResponse.headers()));
        // WHEN updating the existing password
        final JsonArray bodyAsJsonArray = httpResponse.bodyAsJsonArray();
        LOG.debug("received original credentials list: {}", bodyAsJsonArray.encodePrettily());
        ctx.verify(() -> assertThat(bodyAsJsonArray).hasSize(1));
        final PasswordCredential existingCredentials = bodyAsJsonArray.getJsonObject(0).mapTo(PasswordCredential.class);
        ctx.verify(() -> {
            assertThat(existingCredentials.getSecrets()).hasSize(1);
            final PasswordSecret existingSecret = existingCredentials.getSecrets().get(0);
            assertThat(existingSecret.getId()).isNotNull();
            originalSecret.set(existingSecret);
        });
        final PasswordSecret changedSecret = new PasswordSecret();
        changedSecret.setId(originalSecret.get().getId());
        changedSecret.setPasswordPlain("completely-different-password");
        changedSecret.setComment("updated");
        // and adding a new one
        final PasswordSecret newSecret = new PasswordSecret();
        newSecret.setPasswordPlain("future-password");
        newSecret.setNotBefore(Instant.now().plus(1, ChronoUnit.DAYS));
        final PasswordCredential updatedCredentials = new PasswordCredential(existingCredentials.getAuthId(), List.of(changedSecret, newSecret));
        return registry.updateCredentialsWithVersion(tenantId, deviceId, List.of(updatedCredentials), resourceVersion.get(), HttpURLConnection.HTTP_NO_CONTENT);
    }).compose(httpResponse -> {
        ctx.verify(() -> assertResourceVersionHasChanged(resourceVersion, httpResponse.headers()));
        return registry.getCredentials(tenantId, deviceId);
    }).onComplete(ctx.succeeding(httpResponse -> {
        final JsonArray bodyAsJsonArray = httpResponse.bodyAsJsonArray();
        LOG.debug("received updated credentials list: {}", bodyAsJsonArray.encodePrettily());
        ctx.verify(() -> {
            assertThat(bodyAsJsonArray).hasSize(1);
            final PasswordCredential updatedCredentials = bodyAsJsonArray.getJsonObject(0).mapTo(PasswordCredential.class);
            assertThat(updatedCredentials.getSecrets()).hasSize(2);
            // THEN the original secret has been updated
            final PasswordSecret updatedSecret = updatedCredentials.getSecrets().stream().filter(s -> originalSecret.get().getId().equals(s.getId())).findAny().orElse(null);
            assertThat(updatedSecret).isNotNull();
            assertThat(updatedSecret.getComment()).isEqualTo("updated");
        });
        ctx.completeNow();
    }));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) VertxTestContext(io.vertx.junit5.VertxTestContext) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) BCryptPasswordEncoder(org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder) LoggerFactory(org.slf4j.LoggerFactory) MultiMap(io.vertx.core.MultiMap) Credentials(org.eclipse.hono.service.management.credentials.Credentials) GenericCredential(org.eclipse.hono.service.management.credentials.GenericCredential) PasswordSecret(org.eclipse.hono.service.management.credentials.PasswordSecret) X509CertificateCredential(org.eclipse.hono.service.management.credentials.X509CertificateCredential) OptionalInt(java.util.OptionalInt) AtomicReference(java.util.concurrent.atomic.AtomicReference) Timeout(io.vertx.junit5.Timeout) ArrayList(java.util.ArrayList) IntegrationTestSupport(org.eclipse.hono.tests.IntegrationTestSupport) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) PskCredential(org.eclipse.hono.service.management.credentials.PskCredential) Map(java.util.Map) Assertions(org.assertj.core.api.Assertions) HttpUtils(org.eclipse.hono.service.http.HttpUtils) JsonObject(io.vertx.core.json.JsonObject) RegistryManagementConstants(org.eclipse.hono.util.RegistryManagementConstants) Logger(org.slf4j.Logger) RecursiveComparisonConfiguration(org.assertj.core.api.recursive.comparison.RecursiveComparisonConfiguration) X509CertificateSecret(org.eclipse.hono.service.management.credentials.X509CertificateSecret) HttpHeaders(io.vertx.core.http.HttpHeaders) UUID(java.util.UUID) Truth.assertThat(com.google.common.truth.Truth.assertThat) Instant(java.time.Instant) VertxExtension(io.vertx.junit5.VertxExtension) Collectors(java.util.stream.Collectors) CredentialsConstants(org.eclipse.hono.util.CredentialsConstants) TestInfo(org.junit.jupiter.api.TestInfo) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) JsonArray(io.vertx.core.json.JsonArray) List(java.util.List) ChronoUnit(java.time.temporal.ChronoUnit) CommonCredential(org.eclipse.hono.service.management.credentials.CommonCredential) CrudHttpClient(org.eclipse.hono.tests.CrudHttpClient) DeviceRegistryHttpClient(org.eclipse.hono.tests.DeviceRegistryHttpClient) Optional(java.util.Optional) GenericSecret(org.eclipse.hono.service.management.credentials.GenericSecret) PasswordCredential(org.eclipse.hono.service.management.credentials.PasswordCredential) JsonArray(io.vertx.core.json.JsonArray) PasswordSecret(org.eclipse.hono.service.management.credentials.PasswordSecret) PasswordCredential(org.eclipse.hono.service.management.credentials.PasswordCredential) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.jupiter.api.Test)

Aggregations

VertxTestContext (io.vertx.junit5.VertxTestContext)12 HttpURLConnection (java.net.HttpURLConnection)12 List (java.util.List)12 UUID (java.util.UUID)12 Credentials (org.eclipse.hono.service.management.credentials.Credentials)12 Test (org.junit.jupiter.api.Test)12 Truth.assertThat (com.google.common.truth.Truth.assertThat)11 JsonObject (io.vertx.core.json.JsonObject)11 Timeout (io.vertx.junit5.Timeout)11 TimeUnit (java.util.concurrent.TimeUnit)11 CredentialsConstants (org.eclipse.hono.util.CredentialsConstants)11 BeforeEach (org.junit.jupiter.api.BeforeEach)11 Logger (org.slf4j.Logger)11 LoggerFactory (org.slf4j.LoggerFactory)11 Instant (java.time.Instant)9 OptionalInt (java.util.OptionalInt)9 CommonCredential (org.eclipse.hono.service.management.credentials.CommonCredential)9 PasswordCredential (org.eclipse.hono.service.management.credentials.PasswordCredential)9 NoopSpan (io.opentracing.noop.NoopSpan)8 Vertx (io.vertx.core.Vertx)7