Enable specifying target type when created using DMF API (#1472)
Extension of DMF API with possibility of setting target type name when creating target. If a target type with the provided name is found (was created beforehand) then it is associated with the new target. Signed-off-by: Ondrej Charvat <ondrej.charvat@proton.me>
This commit is contained in:
@@ -231,14 +231,18 @@ public class AmqpMessageHandlerService extends BaseAmqpService {
|
||||
final URI amqpUri = IpUtil.createAmqpUri(virtualHost, replyTo);
|
||||
final Target target;
|
||||
if (isOptionalMessageBodyEmpty(message)) {
|
||||
LOG.debug("Received \"THING_CREATED\" AMQP message for thing \"{}\" without body.", thingId);
|
||||
target = controllerManagement.findOrRegisterTargetIfItDoesNotExist(thingId, amqpUri);
|
||||
} else {
|
||||
checkContentTypeJson(message);
|
||||
final DmfCreateThing thingCreateBody = convertMessage(message, DmfCreateThing.class);
|
||||
final DmfAttributeUpdate thingAttributeUpdateBody = thingCreateBody.getAttributeUpdate();
|
||||
|
||||
LOG.debug("Received \"THING_CREATED\" AMQP message for thing \"{}\" with target name \"{}\" and type " +
|
||||
"\"{}\".", thingId, thingCreateBody.getName(), thingCreateBody.getType());
|
||||
|
||||
target = controllerManagement.findOrRegisterTargetIfItDoesNotExist(thingId, amqpUri,
|
||||
thingCreateBody.getName());
|
||||
thingCreateBody.getName(), thingCreateBody.getType());
|
||||
|
||||
if (thingAttributeUpdateBody != null) {
|
||||
controllerManagement.updateControllerAttributes(thingId, thingAttributeUpdateBody.getAttributes(),
|
||||
|
||||
@@ -50,7 +50,6 @@ import org.eclipse.hawkbit.repository.UpdateMode;
|
||||
import org.eclipse.hawkbit.repository.builder.ActionStatusBuilder;
|
||||
import org.eclipse.hawkbit.repository.builder.ActionStatusCreate;
|
||||
import org.eclipse.hawkbit.repository.exception.AssignmentQuotaExceededException;
|
||||
import org.eclipse.hawkbit.repository.jpa.model.JpaAction;
|
||||
import org.eclipse.hawkbit.repository.jpa.builder.JpaActionStatusBuilder;
|
||||
import org.eclipse.hawkbit.repository.jpa.model.JpaActionStatus;
|
||||
import org.eclipse.hawkbit.repository.jpa.model.helper.SecurityTokenGeneratorHolder;
|
||||
@@ -167,6 +166,9 @@ public class AmqpMessageHandlerServiceTest {
|
||||
@Captor
|
||||
private ArgumentCaptor<String> targetNameCaptor;
|
||||
|
||||
@Captor
|
||||
private ArgumentCaptor<String> targetTypeNameCaptor;
|
||||
|
||||
@Captor
|
||||
private ArgumentCaptor<URI> uriCaptor;
|
||||
|
||||
@@ -228,7 +230,8 @@ public class AmqpMessageHandlerServiceTest {
|
||||
uriCaptor.capture())).thenReturn(targetMock);
|
||||
} else {
|
||||
when(controllerManagementMock.findOrRegisterTargetIfItDoesNotExist(targetIdCaptor.capture(),
|
||||
uriCaptor.capture(), targetNameCaptor.capture())).thenReturn(targetMock);
|
||||
uriCaptor.capture(), targetNameCaptor.capture(), targetTypeNameCaptor.capture()))
|
||||
.thenReturn(targetMock);
|
||||
if (payload.getAttributeUpdate() != null) {
|
||||
when(controllerManagementMock.updateControllerAttributes(targetIdCaptor.capture(),
|
||||
attributesCaptor.capture(), modeCaptor.capture())).thenReturn(null);
|
||||
@@ -280,6 +283,27 @@ public class AmqpMessageHandlerServiceTest {
|
||||
assertThat(targetNameCaptor.getValue()).as("Thing name is wrong").isEqualTo(thingName);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Description("Tests the creation of a target/thing with specified type name by calling the same method that incoming RabbitMQ messages would access.")
|
||||
public void createThingWithType() {
|
||||
final String knownThingId = "2";
|
||||
final String knownThingTypeName = "TargetTypeName";
|
||||
|
||||
final DmfCreateThing payload = new DmfCreateThing();
|
||||
payload.setType(knownThingTypeName);
|
||||
|
||||
processThingCreatedMessage(knownThingId, payload);
|
||||
|
||||
assertThingIdCapturedField(knownThingId);
|
||||
assertReplyToCapturedField("MyTest");
|
||||
assertThingTypeCapturedField(knownThingTypeName);
|
||||
}
|
||||
|
||||
@Step
|
||||
private void assertThingTypeCapturedField(final String thingType) {
|
||||
assertThat(targetTypeNameCaptor.getValue()).as("Thing type is wrong").isEqualTo(thingType);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Description("Tests not allowed body in message")
|
||||
public void createThingWithWrongBody() {
|
||||
|
||||
@@ -26,7 +26,7 @@ public final class SoftwareModuleJsonMatcher {
|
||||
|
||||
/**
|
||||
* Creates a matcher that matches when the list of repository software
|
||||
* modules arelogically equal to the specified JSON software modules.
|
||||
* modules are logically equal to the specified JSON software modules.
|
||||
* <p>
|
||||
* If the specified repository software modules are <code>null</code> then
|
||||
* the created matcher will only match if the JSON software modules are
|
||||
|
||||
@@ -24,6 +24,9 @@ public class DmfCreateThing {
|
||||
@JsonProperty
|
||||
private String name;
|
||||
|
||||
@JsonProperty
|
||||
private String type;
|
||||
|
||||
@JsonProperty
|
||||
private DmfAttributeUpdate attributeUpdate;
|
||||
|
||||
@@ -35,6 +38,14 @@ public class DmfCreateThing {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(final String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public DmfAttributeUpdate getAttributeUpdate() {
|
||||
return attributeUpdate;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user