Skip to content

Commit

Permalink
tests(bindings): AMQP1 0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Pakisan committed Apr 25, 2024
1 parent ee6d885 commit 64ebecb
Show file tree
Hide file tree
Showing 27 changed files with 623 additions and 58 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.asyncapi.bindings.amqp1;

import com.asyncapi.bindings.ChannelBinding;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import lombok.EqualsAndHashCode;

/**
* This class MUST NOT contain any properties. Its name is reserved for future use.
* <p>
* Describes AMQP 1.0 channel binding.
*
* @version 0.1.0
* @see <a href="https://github.com/asyncapi/bindings/tree/master/amqp1#channel-binding-object">AMQP 1.0 channel binding</a>
* @author Pavel Bodiachevskii
*/
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.EXISTING_PROPERTY,
defaultImpl = com.asyncapi.bindings.amqp1.v0._1_0.channel.AMQP1ChannelBinding.class,
property = "bindingVersion",
visible = true
)
@JsonSubTypes({
@JsonSubTypes.Type(value = com.asyncapi.bindings.amqp1.v0._1_0.channel.AMQP1ChannelBinding.class, names = {
"0.1.0",
"latest"
}),
})
@EqualsAndHashCode(callSuper = true)
public abstract class AMQP1ChannelBinding extends ChannelBinding {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.asyncapi.bindings.amqp1;

import com.asyncapi.bindings.MessageBinding;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import lombok.EqualsAndHashCode;

/**
* This class MUST NOT contain any properties. Its name is reserved for future use.
* <p>
* Describes AMQP 1.0 message binding.
*
* @version 0.1.0
* @see <a href="https://github.com/asyncapi/bindings/tree/master/amqp1#message-binding-object">AMQP message binding</a>
* @author Pavel Bodiachevskii
*/
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.EXISTING_PROPERTY,
defaultImpl = com.asyncapi.bindings.amqp1.v0._1_0.message.AMQP1MessageBinding.class,
property = "bindingVersion",
visible = true
)
@JsonSubTypes({
@JsonSubTypes.Type(value = com.asyncapi.bindings.amqp1.v0._1_0.message.AMQP1MessageBinding.class, names = {
"0.1.0",
"latest"
}),
})
@EqualsAndHashCode(callSuper = true)
public abstract class AMQP1MessageBinding extends MessageBinding {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.asyncapi.bindings.amqp1;

import com.asyncapi.bindings.OperationBinding;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import lombok.EqualsAndHashCode;

/**
* This class MUST NOT contain any properties. Its name is reserved for future use.
* <p>
* Describes AMQP 1.0 operation binding.
*
* @version 0.1.0
* @see <a href="https://github.com/asyncapi/bindings/tree/master/amqp1#operation-binding-object">AMQP operation binding</a>
* @author Pavel Bodiachevskii
*/
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.EXISTING_PROPERTY,
defaultImpl = com.asyncapi.bindings.amqp1.v0._1_0.operation.AMQP1OperationBinding.class,
property = "bindingVersion",
visible = true
)
@JsonSubTypes({
@JsonSubTypes.Type(value = com.asyncapi.bindings.amqp1.v0._1_0.operation.AMQP1OperationBinding.class, names = {
"0.1.0",
"latest"
}),
})
@EqualsAndHashCode(callSuper = true)
public abstract class AMQP1OperationBinding extends OperationBinding {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.asyncapi.bindings.amqp1;

import com.asyncapi.bindings.ServerBinding;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import lombok.EqualsAndHashCode;

/**
* This class MUST NOT contain any properties. Its name is reserved for future use.
* <p>
* Describes AMQP 1.0 server binding.
*
* @version 0.1.0
* @see <a href="https://github.com/asyncapi/bindings/tree/master/amqp1#server-binding-object">AMQP server binding</a>
* @author Pavel Bodiachevskii
*/
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.EXISTING_PROPERTY,
defaultImpl = com.asyncapi.bindings.amqp1.v0._1_0.server.AMQP1ServerBinding.class,
property = "bindingVersion",
visible = true
)
@JsonSubTypes({
@JsonSubTypes.Type(value = com.asyncapi.bindings.amqp1.v0._1_0.server.AMQP1ServerBinding.class, names = {
"0.1.0",
"latest"
}),
})
@EqualsAndHashCode(callSuper = true)
public abstract class AMQP1ServerBinding extends ServerBinding {}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.asyncapi.bindings.amqp1.v0._1_0.channel;

import com.asyncapi.bindings.ChannelBinding;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.jetbrains.annotations.Nullable;

/**
* This class MUST NOT contain any properties. Its name is reserved for future use.
Expand All @@ -17,5 +17,16 @@
@Data
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class AMQP1ChannelBinding extends ChannelBinding {
public class AMQP1ChannelBinding extends com.asyncapi.bindings.amqp1.AMQP1ChannelBinding {

@Override
public String getBindingVersion() {
return "0.1.0";
}

@Override
public void setBindingVersion(@Nullable String bindingVersion) {
super.setBindingVersion("0.1.0");
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.asyncapi.bindings.amqp1.v0._1_0.message;

import com.asyncapi.bindings.MessageBinding;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.jetbrains.annotations.Nullable;

/**
* This class MUST NOT contain any properties. Its name is reserved for future use.
Expand All @@ -17,5 +17,16 @@
@Data
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class AMQP1MessageBinding extends MessageBinding {
public class AMQP1MessageBinding extends com.asyncapi.bindings.amqp1.AMQP1MessageBinding {

@Override
public String getBindingVersion() {
return "0.1.0";
}

@Override
public void setBindingVersion(@Nullable String bindingVersion) {
super.setBindingVersion("0.1.0");
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.asyncapi.bindings.amqp1.v0._1_0.operation;

import com.asyncapi.bindings.OperationBinding;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.jetbrains.annotations.Nullable;

/**
* This class MUST NOT contain any properties. Its name is reserved for future use.
Expand All @@ -17,5 +17,16 @@
@Data
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class AMQP1OperationBinding extends OperationBinding {
public class AMQP1OperationBinding extends com.asyncapi.bindings.amqp1.AMQP1OperationBinding {

@Override
public String getBindingVersion() {
return "0.1.0";
}

@Override
public void setBindingVersion(@Nullable String bindingVersion) {
super.setBindingVersion("0.1.0");
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.asyncapi.bindings.amqp1.v0._1_0.server;

import com.asyncapi.bindings.ServerBinding;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.jetbrains.annotations.Nullable;

/**
* This class MUST NOT contain any properties. Its name is reserved for future use.
Expand All @@ -17,5 +17,16 @@
@Data
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class AMQP1ServerBinding extends ServerBinding {
public class AMQP1ServerBinding extends com.asyncapi.bindings.amqp1.AMQP1ServerBinding {

@Override
public String getBindingVersion() {
return "0.1.0";
}

@Override
public void setBindingVersion(@Nullable String bindingVersion) {
super.setBindingVersion("0.1.0");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.asyncapi.bindings;

import com.asyncapi.ExtendableObject;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;

public abstract class BindingTest<BindingType extends ExtendableObject> {

private static final ObjectMapper objectMapper = new ObjectMapper()
.setSerializationInclusion(JsonInclude.Include.NON_NULL);

protected Class<BindingType> bindingTypeClass;
protected String pathToBindingJson;
protected String pathToExtendedBindingJson;
protected String pathToWronglyExtendedBindingJson;
protected BindingType binding;

@Test
@Order(1)
@DisplayName("compare binding from Json and from code")
public void compareBindingFromJsonAndFromCode() throws IOException, URISyntaxException {
Assertions.assertEquals(read(pathToBindingJson, bindingTypeClass), binding);
}

@Test
@Order(2)
@DisplayName("compare extended binding from Json and from code")
public void compareExtendedBindingFromJsonAndFromCode() throws IOException, URISyntaxException {
var binding = read(pathToExtendedBindingJson, bindingTypeClass);
Assertions.assertEquals(
Map.ofEntries(
Map.entry("x-number", 0),
Map.entry("x-string", ""),
Map.entry("x-object", Map.ofEntries(Map.entry("property", Collections.emptyMap())))
),
binding.getExtensionFields()
);

Assertions.assertEquals(read(pathToExtendedBindingJson, bindingTypeClass), binding);
}

@Test
@Order(3)
@DisplayName("deserialize and compare extended binding json with extended binding from code")
public void deserializeAndCompareExtendedBindingJsonWithExtendedBindingFromCode() throws IOException, URISyntaxException {
var extensions = new LinkedHashMap<String, Object>();
extensions.put("x-number", 0);
extensions.put("x-string", "");
extensions.put("x-object", Map.ofEntries(Map.entry("property", Collections.emptyMap())));

binding.setExtensionFields(extensions);
Assertions.assertEquals(
read(pathToExtendedBindingJson),
objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(binding)
);
}

@Test
@Order(4)
@DisplayName("throw exception when binding extended wrongly")
public void throwExceptionWhenBindingExtendedWrongly() {
var exception = Assertions.assertThrows(JsonMappingException.class, () -> {
read(pathToWronglyExtendedBindingJson, bindingTypeClass);
});

Assertions.assertTrue(exception.getMessage().startsWith("\"ext-number\" is not valid extension property (through reference chain:"));
}

private String read(@NotNull String pathToJson) throws IOException, URISyntaxException {
Path path = Path.of(BindingTest.class.getResource(pathToJson).toURI());

return Files.readString(path);
}

private BindingType read(@NotNull String pathToJson, @NotNull Class<BindingType> bindingTypeClass) throws IOException, URISyntaxException {
Path path = Path.of(BindingTest.class.getResource(pathToJson).toURI());

return objectMapper.readValue(Files.readString(path), bindingTypeClass);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.asyncapi.bindings;

import com.asyncapi.bindings.amqp1.AMQP1Test;
import com.asyncapi.bindings.redis.RedisTest;
import com.asyncapi.bindings.stomp.STOMPTest;
import com.asyncapi.bindings.websockets.WebSocketsTest;
Expand All @@ -9,6 +10,10 @@
@DisplayName("Bindings")
public class BindingsTest {

@Nested
@DisplayName("AMQP1")
class AMQP1 extends AMQP1Test {}

@Nested
@DisplayName("Redis")
class Redis extends RedisTest {}
Expand Down
Loading

0 comments on commit 64ebecb

Please sign in to comment.