Search in sources :

Example 41 with ArgumentCaptor

use of org.mockito.ArgumentCaptor in project hono by eclipse.

the class FileBasedCredentialsServiceTest method testLoadCredentialsCanReadOutputOfSaveToFile.

/**
 * Verifies that the file written by the registry when persisting the registry's contents can
 * be loaded in again.
 *
 * @param ctx The vert.x test context.
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testLoadCredentialsCanReadOutputOfSaveToFile(final TestContext ctx) {
    // GIVEN a service configured to persist credentials to file
    // that contains some credentials
    props.setFilename(FILE_NAME);
    props.setSaveToFile(true);
    when(fileSystem.existsBlocking(FILE_NAME)).thenReturn(Boolean.TRUE);
    final Async add = ctx.async(2);
    final CredentialsObject hashedPassword = CredentialsObject.fromHashedPassword("4700", "bumlux", "secret", "sha-512", null, null, null);
    final CredentialsObject psk = CredentialsObject.fromPresharedKey("4711", "sensor1", "sharedkey".getBytes(StandardCharsets.UTF_8), null, null);
    svc.add(Constants.DEFAULT_TENANT, JsonObject.mapFrom(psk), ctx.asyncAssertSuccess(s -> {
        ctx.assertEquals(HttpURLConnection.HTTP_CREATED, s.getStatus());
        add.countDown();
    }));
    svc.add("OTHER_TENANT", JsonObject.mapFrom(hashedPassword), ctx.asyncAssertSuccess(s -> {
        ctx.assertEquals(HttpURLConnection.HTTP_CREATED, s.getStatus());
        add.countDown();
    }));
    add.await(2000);
    // WHEN saving the registry content to the file and clearing the registry
    final Async write = ctx.async();
    doAnswer(invocation -> {
        Handler handler = invocation.getArgument(2);
        handler.handle(Future.succeededFuture());
        write.complete();
        return null;
    }).when(fileSystem).writeFile(eq(FILE_NAME), any(Buffer.class), any(Handler.class));
    svc.saveToFile();
    write.await(2000);
    ArgumentCaptor<Buffer> buffer = ArgumentCaptor.forClass(Buffer.class);
    verify(fileSystem).writeFile(eq(FILE_NAME), buffer.capture(), any(Handler.class));
    svc.clear();
    assertNotRegistered(svc, Constants.DEFAULT_PATH_SEPARATOR, "sensor1", CredentialsConstants.SECRETS_TYPE_PRESHARED_KEY, ctx);
    // THEN the credentials can be loaded back in from the file
    final Async read = ctx.async();
    doAnswer(invocation -> {
        Handler handler = invocation.getArgument(1);
        handler.handle(Future.succeededFuture(buffer.getValue()));
        read.complete();
        return null;
    }).when(fileSystem).readFile(eq(FILE_NAME), any(Handler.class));
    svc.loadCredentials();
    read.await(2000);
    assertRegistered(svc, Constants.DEFAULT_TENANT, "sensor1", CredentialsConstants.SECRETS_TYPE_PRESHARED_KEY, ctx);
    assertRegistered(svc, "OTHER_TENANT", "bumlux", CredentialsConstants.SECRETS_TYPE_HASHED_PASSWORD, ctx);
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) HttpURLConnection(java.net.HttpURLConnection) TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) RunWith(org.junit.runner.RunWith) Constants(org.eclipse.hono.util.Constants) Context(io.vertx.core.Context) Assert.assertThat(org.junit.Assert.assertThat) ArgumentCaptor(org.mockito.ArgumentCaptor) EventBus(io.vertx.core.eventbus.EventBus) Matchers.eq(org.mockito.Matchers.eq) JsonObject(io.vertx.core.json.JsonObject) Before(org.junit.Before) Vertx(io.vertx.core.Vertx) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Future(io.vertx.core.Future) StandardCharsets(java.nio.charset.StandardCharsets) CredentialsConstants(org.eclipse.hono.util.CredentialsConstants) Matchers.any(org.mockito.Matchers.any) Mockito(org.mockito.Mockito) JsonArray(io.vertx.core.json.JsonArray) Buffer(io.vertx.core.buffer.Buffer) FileSystem(io.vertx.core.file.FileSystem) CredentialsService(org.eclipse.hono.service.credentials.CredentialsService) Handler(io.vertx.core.Handler) CredentialsObject(org.eclipse.hono.util.CredentialsObject) Buffer(io.vertx.core.buffer.Buffer) Async(io.vertx.ext.unit.Async) CredentialsObject(org.eclipse.hono.util.CredentialsObject) Handler(io.vertx.core.Handler) Test(org.junit.Test)

Example 42 with ArgumentCaptor

use of org.mockito.ArgumentCaptor in project hono by eclipse.

the class RegistrationClientImplTest method testAssertRegistrationInvokesServiceIfNoCacheConfigured.

/**
 * Verifies that the client retrieves registration information from the
 * Device Registration service if no cache is configured.
 *
 * @param ctx The vert.x test context.
 */
@SuppressWarnings("unchecked")
@Test
public void testAssertRegistrationInvokesServiceIfNoCacheConfigured(final TestContext ctx) {
    // GIVEN an adapter with no cache configured
    final JsonObject registrationAssertion = newRegistrationAssertionResult();
    final Message response = ProtonHelper.message(registrationAssertion.encode());
    MessageHelper.addProperty(response, MessageHelper.APP_PROPERTY_STATUS, HttpURLConnection.HTTP_OK);
    // WHEN getting registration information
    final Async assertion = ctx.async();
    client.assertRegistration("device").setHandler(ctx.asyncAssertSuccess(result -> assertion.complete()));
    final ArgumentCaptor<Message> messageCaptor = ArgumentCaptor.forClass(Message.class);
    verify(sender).send(messageCaptor.capture(), any(Handler.class));
    response.setCorrelationId(messageCaptor.getValue().getMessageId());
    final ProtonDelivery delivery = mock(ProtonDelivery.class);
    client.handleResponse(delivery, response);
    // THEN the registration information has been retrieved from the service
    assertion.await();
    // and not been put to the cache
    verify(cache, never()).put(any(), any(RegistrationResult.class), any(Duration.class));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) HttpURLConnection(java.net.HttpURLConnection) CacheDirective(org.eclipse.hono.util.CacheDirective) TestContext(io.vertx.ext.unit.TestContext) ProtonReceiver(io.vertx.proton.ProtonReceiver) Async(io.vertx.ext.unit.Async) ProtonDelivery(io.vertx.proton.ProtonDelivery) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) RunWith(org.junit.runner.RunWith) ExpiringValueCache(org.eclipse.hono.cache.ExpiringValueCache) Context(io.vertx.core.Context) Assert.assertThat(org.junit.Assert.assertThat) ArgumentCaptor(org.mockito.ArgumentCaptor) Duration(java.time.Duration) RegistrationResult(org.eclipse.hono.util.RegistrationResult) Timeout(org.junit.rules.Timeout) SignatureAlgorithm(io.jsonwebtoken.SignatureAlgorithm) Message(org.apache.qpid.proton.message.Message) JsonObject(io.vertx.core.json.JsonObject) RequestResponseClientConfigProperties(org.eclipse.hono.client.RequestResponseClientConfigProperties) Before(org.junit.Before) TriTuple(org.eclipse.hono.util.TriTuple) Vertx(io.vertx.core.Vertx) RegistrationConstants(org.eclipse.hono.util.RegistrationConstants) Test(org.junit.Test) ProtonHelper(io.vertx.proton.ProtonHelper) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Instant(java.time.Instant) MessageHelper(org.eclipse.hono.util.MessageHelper) Date(java.sql.Date) Mockito(org.mockito.Mockito) Rule(org.junit.Rule) Jwts(io.jsonwebtoken.Jwts) ProtonSender(io.vertx.proton.ProtonSender) Handler(io.vertx.core.Handler) Message(org.apache.qpid.proton.message.Message) ProtonDelivery(io.vertx.proton.ProtonDelivery) Async(io.vertx.ext.unit.Async) JsonObject(io.vertx.core.json.JsonObject) Handler(io.vertx.core.Handler) Duration(java.time.Duration) RegistrationResult(org.eclipse.hono.util.RegistrationResult) Test(org.junit.Test)

Example 43 with ArgumentCaptor

use of org.mockito.ArgumentCaptor in project hono by eclipse.

the class TenantClientImplTest method testGetTenantAddsInfoToCacheOnCacheMiss.

/**
 * Verifies that on a cache miss the adapter retrieves tenant information
 * from the Tenant service and puts it to the cache.
 *
 * @param ctx The vert.x test context.
 */
@SuppressWarnings("unchecked")
@Test
public void testGetTenantAddsInfoToCacheOnCacheMiss(final TestContext ctx) {
    // GIVEN an adapter with an empty cache
    client.setResponseCache(cache);
    final JsonObject tenantResult = newTenantResult("tenant");
    // WHEN getting tenant information
    final Async get = ctx.async();
    client.get("tenant").setHandler(ctx.asyncAssertSuccess(tenant -> get.complete()));
    final ArgumentCaptor<Message> messageCaptor = ArgumentCaptor.forClass(Message.class);
    verify(sender).send(messageCaptor.capture(), any(Handler.class));
    final Message response = ProtonHelper.message(tenantResult.encode());
    MessageHelper.addProperty(response, MessageHelper.APP_PROPERTY_STATUS, HttpURLConnection.HTTP_OK);
    MessageHelper.addCacheDirective(response, CacheDirective.maxAgeDirective(60));
    response.setCorrelationId(messageCaptor.getValue().getMessageId());
    final ProtonDelivery delivery = mock(ProtonDelivery.class);
    client.handleResponse(delivery, response);
    // THEN the tenant result has been added to the cache
    get.await();
    verify(cache).put(eq(TriTuple.of(TenantAction.get, "tenant", null)), any(TenantResult.class), any(Duration.class));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) HttpURLConnection(java.net.HttpURLConnection) CacheDirective(org.eclipse.hono.util.CacheDirective) TestContext(io.vertx.ext.unit.TestContext) ProtonReceiver(io.vertx.proton.ProtonReceiver) Async(io.vertx.ext.unit.Async) ProtonDelivery(io.vertx.proton.ProtonDelivery) TenantConstants(org.eclipse.hono.util.TenantConstants) RunWith(org.junit.runner.RunWith) CoreMatchers.startsWith(org.hamcrest.CoreMatchers.startsWith) ExpiringValueCache(org.eclipse.hono.cache.ExpiringValueCache) Context(io.vertx.core.Context) Assert.assertThat(org.junit.Assert.assertThat) ArgumentCaptor(org.mockito.ArgumentCaptor) TenantAction(org.eclipse.hono.util.TenantConstants.TenantAction) Duration(java.time.Duration) Timeout(org.junit.rules.Timeout) Message(org.apache.qpid.proton.message.Message) JsonObject(io.vertx.core.json.JsonObject) RequestResponseClientConfigProperties(org.eclipse.hono.client.RequestResponseClientConfigProperties) Before(org.junit.Before) TenantResult(org.eclipse.hono.util.TenantResult) TriTuple(org.eclipse.hono.util.TriTuple) Vertx(io.vertx.core.Vertx) Test(org.junit.Test) ProtonHelper(io.vertx.proton.ProtonHelper) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) MessageHelper(org.eclipse.hono.util.MessageHelper) TenantObject(org.eclipse.hono.util.TenantObject) Mockito(org.mockito.Mockito) Rule(org.junit.Rule) ProtonSender(io.vertx.proton.ProtonSender) Handler(io.vertx.core.Handler) Message(org.apache.qpid.proton.message.Message) ProtonDelivery(io.vertx.proton.ProtonDelivery) Async(io.vertx.ext.unit.Async) JsonObject(io.vertx.core.json.JsonObject) Handler(io.vertx.core.Handler) Duration(java.time.Duration) TenantResult(org.eclipse.hono.util.TenantResult) Test(org.junit.Test)

Aggregations

ArgumentCaptor (org.mockito.ArgumentCaptor)43 Test (org.junit.Test)22 Map (java.util.Map)18 Collections (java.util.Collections)17 List (java.util.List)16 Matchers.anyString (org.mockito.Matchers.anyString)15 Test (org.testng.annotations.Test)15 HashMap (java.util.HashMap)13 HashSet (java.util.HashSet)13 Set (java.util.Set)13 Collectors (java.util.stream.Collectors)13 Mockito.verify (org.mockito.Mockito.verify)13 Matchers.any (org.mockito.Matchers.any)12 Matchers.eq (org.mockito.Matchers.eq)12 Before (org.junit.Before)9 Mockito (org.mockito.Mockito)9 Context (io.vertx.core.Context)8 Handler (io.vertx.core.Handler)8 Vertx (io.vertx.core.Vertx)8 JsonObject (io.vertx.core.json.JsonObject)8