Skip to content

Commit

Permalink
[#1078] use DittoDuration format for AzSaslRequestSigning's ttl param…
Browse files Browse the repository at this point in the history
…eter.

Signed-off-by: Yufei Cai <yufei.cai@bosch.io>
  • Loading branch information
yufei-cai committed Jun 3, 2021
1 parent f89eea0 commit e56d304
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
Expand Up @@ -14,6 +14,7 @@

import java.time.Duration;

import org.eclipse.ditto.base.model.common.DittoDuration;
import org.eclipse.ditto.connectivity.model.HmacCredentials;
import org.eclipse.ditto.connectivity.service.messaging.AzSaslRequestSigning;
import org.eclipse.ditto.json.JsonFieldDefinition;
Expand All @@ -33,11 +34,15 @@ public RequestSigning create(final ActorSystem actorSystem, final HmacCredential
final JsonObject parameters = credentials.getParameters();
final String sharedKeyName = parameters.getValueOrThrow(JsonFields.SHARED_KEY_NAME);
final String sharedKey = parameters.getValueOrThrow(JsonFields.SHARED_KEY);
final Duration ttl = parameters.getValue(JsonFields.TTL).map(Duration::parse).orElse(DEFAULT_TTL);
final Duration ttl = parameters.getValue(JsonFields.TTL).map(this::parseDuration).orElse(DEFAULT_TTL);
final String endpoint = parameters.getValueOrThrow(JsonFields.ENDPOINT);
return AzSaslRequestSigning.of(sharedKeyName, sharedKey, ttl, endpoint);
}

private Duration parseDuration(final String string) {
return DittoDuration.parseDuration(string).getDuration();
}

/**
* JSON fields of algorithm parameters.
*/
Expand Down
@@ -0,0 +1,57 @@
/*
* Copyright (c) 2021 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.connectivity.service.messaging.httppush;

import java.util.Base64;

import org.eclipse.ditto.connectivity.model.HmacCredentials;
import org.eclipse.ditto.json.JsonObject;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import akka.actor.ActorSystem;
import akka.testkit.javadsl.TestKit;

/**
* Tests {@link org.eclipse.ditto.connectivity.service.messaging.httppush.AzSaslRequestSigningFactory}.
*/
public final class AzSaslRequestSigningFactoryTest {

private ActorSystem actorSystem;

@Before
public void start() {
actorSystem = ActorSystem.create();
}

@After
public void stop() {
if (actorSystem != null) {
TestKit.shutdownActorSystem(actorSystem);
}
}

@Test
public void create() {
final AzSaslRequestSigningFactory underTest = new AzSaslRequestSigningFactory();
final HmacCredentials credentials = HmacCredentials.of("az-sasl", JsonObject.newBuilder()
.set("sharedKeyName", "name")
.set("sharedKey", Base64.getEncoder().encodeToString("shared key".getBytes()))
.set("endpoint", "example.com")
.set("ttl", "15m")
.build());

underTest.create(actorSystem, credentials);
}
}

0 comments on commit e56d304

Please sign in to comment.