diff --git a/models/src/main/java/info/frostfs/sdk/UuidExtension.java b/models/src/main/java/info/frostfs/sdk/UuidExtension.java index dc4c6ff..9cec1c2 100644 --- a/models/src/main/java/info/frostfs/sdk/UuidExtension.java +++ b/models/src/main/java/info/frostfs/sdk/UuidExtension.java @@ -7,13 +7,15 @@ import static java.util.Objects.isNull; public class UuidExtension { private static final int UUID_BYTE_ARRAY_LENGTH = 16; + private static final String ERROR_WRONG_UUID_SIZE = "Uuid byte array length must be " + UUID_BYTE_ARRAY_LENGTH; + private static final String ERROR_UUID_MISSING = "Uuid is not present"; private UuidExtension() { } public static UUID asUuid(byte[] bytes) { if (isNull(bytes) || bytes.length != UUID_BYTE_ARRAY_LENGTH) { - throw new IllegalArgumentException("Uuid byte array length must be " + UUID_BYTE_ARRAY_LENGTH); + throw new IllegalArgumentException(ERROR_WRONG_UUID_SIZE); } ByteBuffer bb = ByteBuffer.wrap(bytes); @@ -24,7 +26,7 @@ public class UuidExtension { public static byte[] asBytes(UUID uuid) { if (isNull(uuid)) { - throw new IllegalArgumentException("Uuid is not present"); + throw new IllegalArgumentException(ERROR_UUID_MISSING); } ByteBuffer bb = ByteBuffer.allocate(UUID_BYTE_ARRAY_LENGTH); diff --git a/models/src/main/java/info/frostfs/sdk/dto/object/ObjectHeader.java b/models/src/main/java/info/frostfs/sdk/dto/object/ObjectHeader.java index 208b936..9cc1f47 100644 --- a/models/src/main/java/info/frostfs/sdk/dto/object/ObjectHeader.java +++ b/models/src/main/java/info/frostfs/sdk/dto/object/ObjectHeader.java @@ -14,7 +14,6 @@ public class ObjectHeader { private final ContainerId containerId; private final ObjectType objectType; private List attributes; - private long size; private Version version; private OwnerId ownerId; private long payloadLength; @@ -22,14 +21,14 @@ public class ObjectHeader { private Split split; public ObjectHeader(ContainerId containerId, ObjectType objectType, - List attributes, long size, Version version) { + List attributes, long payloadLength, Version version) { if (isNull(containerId) || isNull(objectType)) { throw new IllegalArgumentException("ContainerId or objectType is not present"); } this.attributes = attributes; this.containerId = containerId; - this.size = size; + this.payloadLength = payloadLength; this.objectType = objectType; this.version = version; } @@ -106,14 +105,6 @@ public class ObjectHeader { return containerId; } - public long getSize() { - return size; - } - - public void setSize(long size) { - this.size = size; - } - public ObjectType getObjectType() { return objectType; } diff --git a/models/src/main/java/info/frostfs/sdk/mappers/SessionMapper.java b/models/src/main/java/info/frostfs/sdk/mappers/SessionMapper.java index 45331cf..ca14d05 100644 --- a/models/src/main/java/info/frostfs/sdk/mappers/SessionMapper.java +++ b/models/src/main/java/info/frostfs/sdk/mappers/SessionMapper.java @@ -9,13 +9,14 @@ import java.io.IOException; import static java.util.Objects.isNull; public class SessionMapper { + private static final String ERROR_TOKEN_MISSING_MESSAGE = "Token is not present"; private SessionMapper() { } public static byte[] serialize(Types.SessionToken token) { if (isNull(token)) { - throw new IllegalArgumentException("Token is not present"); + throw new IllegalArgumentException(ERROR_TOKEN_MISSING_MESSAGE); } try { @@ -30,7 +31,7 @@ public class SessionMapper { public static Types.SessionToken deserializeSessionToken(byte[] bytes) { if (isNull(bytes) || bytes.length == 0) { - throw new IllegalArgumentException("Token is not present"); + throw new IllegalArgumentException(ERROR_TOKEN_MISSING_MESSAGE); } try { diff --git a/models/src/main/java/info/frostfs/sdk/mappers/SignatureMapper.java b/models/src/main/java/info/frostfs/sdk/mappers/SignatureMapper.java index 5f14f4a..5e9e103 100644 --- a/models/src/main/java/info/frostfs/sdk/mappers/SignatureMapper.java +++ b/models/src/main/java/info/frostfs/sdk/mappers/SignatureMapper.java @@ -7,6 +7,7 @@ import info.frostfs.sdk.dto.Signature; import static java.util.Objects.isNull; public class SignatureMapper { + private static final String ERROR_UNKNOWN_VALUE_TEMPLATE = "Unknown SignatureScheme. Value: %s."; private SignatureMapper() { } @@ -19,7 +20,7 @@ public class SignatureMapper { var scheme = Types.SignatureScheme.forNumber(signature.getScheme().value); if (isNull(scheme)) { throw new IllegalArgumentException( - String.format("Unknown SignatureScheme. Value: %s.", signature.getScheme().name()) + String.format(ERROR_UNKNOWN_VALUE_TEMPLATE, signature.getScheme().name()) ); } diff --git a/models/src/main/java/info/frostfs/sdk/mappers/StatusMapper.java b/models/src/main/java/info/frostfs/sdk/mappers/StatusMapper.java index 2bfa717..04ddd6e 100644 --- a/models/src/main/java/info/frostfs/sdk/mappers/StatusMapper.java +++ b/models/src/main/java/info/frostfs/sdk/mappers/StatusMapper.java @@ -7,6 +7,7 @@ import info.frostfs.sdk.enums.StatusCode; import static java.util.Objects.isNull; public class StatusMapper { + private static final String ERROR_UNKNOWN_VALUE_TEMPLATE = "Unknown StatusCode. Value: %s."; private StatusMapper() { } @@ -19,7 +20,7 @@ public class StatusMapper { var statusCode = StatusCode.get(status.getCode()); if (isNull(statusCode)) { throw new IllegalArgumentException( - String.format("Unknown StatusCode. Value: %s.", status.getCode()) + String.format(ERROR_UNKNOWN_VALUE_TEMPLATE, status.getCode()) ); } diff --git a/models/src/main/java/info/frostfs/sdk/mappers/VersionMapper.java b/models/src/main/java/info/frostfs/sdk/mappers/VersionMapper.java index 13e7a0d..f12cb73 100644 --- a/models/src/main/java/info/frostfs/sdk/mappers/VersionMapper.java +++ b/models/src/main/java/info/frostfs/sdk/mappers/VersionMapper.java @@ -22,7 +22,7 @@ public class VersionMapper { } public static Version toModel(Types.Version version) { - if (isNull(version)) { + if (isNull(version) || version.getSerializedSize() == 0) { return null; } diff --git a/models/src/main/java/info/frostfs/sdk/mappers/container/ContainerMapper.java b/models/src/main/java/info/frostfs/sdk/mappers/container/ContainerMapper.java index becfb22..c02bfcb 100644 --- a/models/src/main/java/info/frostfs/sdk/mappers/container/ContainerMapper.java +++ b/models/src/main/java/info/frostfs/sdk/mappers/container/ContainerMapper.java @@ -12,6 +12,7 @@ import static info.frostfs.sdk.UuidExtension.asUuid; import static java.util.Objects.isNull; public class ContainerMapper { + private static final String ERROR_UNKNOWN_VALUE_TEMPLATE = "Unknown BasicACL rule. Value: %s."; private ContainerMapper() { } @@ -29,14 +30,14 @@ public class ContainerMapper { } public static Container toModel(Types.Container containerGrpc) { - if (isNull(containerGrpc)) { + if (isNull(containerGrpc) || containerGrpc.getSerializedSize() == 0) { return null; } var basicAcl = BasicAcl.get(containerGrpc.getBasicAcl()); if (isNull(basicAcl)) { throw new IllegalArgumentException( - String.format("Unknown BasicACL rule. Value: %s.", containerGrpc.getBasicAcl()) + String.format(ERROR_UNKNOWN_VALUE_TEMPLATE, containerGrpc.getBasicAcl()) ); } diff --git a/models/src/main/java/info/frostfs/sdk/mappers/netmap/NetmapSnapshotMapper.java b/models/src/main/java/info/frostfs/sdk/mappers/netmap/NetmapSnapshotMapper.java index f911e74..b7abf4e 100644 --- a/models/src/main/java/info/frostfs/sdk/mappers/netmap/NetmapSnapshotMapper.java +++ b/models/src/main/java/info/frostfs/sdk/mappers/netmap/NetmapSnapshotMapper.java @@ -13,7 +13,7 @@ public class NetmapSnapshotMapper { } public static NetmapSnapshot toModel(Service.NetmapSnapshotResponse netmap) { - if (isNull(netmap)) { + if (isNull(netmap) || netmap.getSerializedSize() == 0) { return null; } diff --git a/models/src/main/java/info/frostfs/sdk/mappers/netmap/NodeInfoMapper.java b/models/src/main/java/info/frostfs/sdk/mappers/netmap/NodeInfoMapper.java index d91dcd1..a5791b3 100644 --- a/models/src/main/java/info/frostfs/sdk/mappers/netmap/NodeInfoMapper.java +++ b/models/src/main/java/info/frostfs/sdk/mappers/netmap/NodeInfoMapper.java @@ -1,8 +1,8 @@ package info.frostfs.sdk.mappers.netmap; import frostfs.netmap.Service; +import frostfs.netmap.Types; import frostfs.netmap.Types.NodeInfo.Attribute; -import frostfs.refs.Types; import info.frostfs.sdk.dto.netmap.NodeInfo; import info.frostfs.sdk.enums.NodeState; import info.frostfs.sdk.mappers.VersionMapper; @@ -12,28 +12,27 @@ import java.util.stream.Collectors; import static java.util.Objects.isNull; public class NodeInfoMapper { + private static final String ERROR_UNKNOWN_VALUE_TEMPLATE = "Unknown NodeState. Value: %s."; private NodeInfoMapper() { } public static NodeInfo toModel(Service.LocalNodeInfoResponse.Body nodeInfo) { - if (isNull(nodeInfo)) { + if (isNull(nodeInfo) || nodeInfo.getSerializedSize() == 0) { return null; } return toModel(nodeInfo.getNodeInfo(), nodeInfo.getVersion()); } - public static NodeInfo toModel(frostfs.netmap.Types.NodeInfo nodeInfo, Types.Version version) { - if (isNull(nodeInfo)) { + public static NodeInfo toModel(Types.NodeInfo nodeInfo, frostfs.refs.Types.Version version) { + if (isNull(nodeInfo) || nodeInfo.getSerializedSize() == 0) { return null; } NodeState nodeState = NodeState.get(nodeInfo.getState().getNumber()); if (isNull(nodeState)) { - throw new IllegalArgumentException( - String.format("Unknown NodeState. Value: %s.", nodeInfo.getState()) - ); + throw new IllegalArgumentException(String.format(ERROR_UNKNOWN_VALUE_TEMPLATE, nodeInfo.getState())); } return new NodeInfo( diff --git a/models/src/main/java/info/frostfs/sdk/mappers/netmap/PlacementPolicyMapper.java b/models/src/main/java/info/frostfs/sdk/mappers/netmap/PlacementPolicyMapper.java index f5bdaef..191a080 100644 --- a/models/src/main/java/info/frostfs/sdk/mappers/netmap/PlacementPolicyMapper.java +++ b/models/src/main/java/info/frostfs/sdk/mappers/netmap/PlacementPolicyMapper.java @@ -27,7 +27,7 @@ public class PlacementPolicyMapper { } public static PlacementPolicy toModel(Types.PlacementPolicy placementPolicy) { - if (isNull(placementPolicy)) { + if (isNull(placementPolicy) || placementPolicy.getSerializedSize() == 0) { return null; } diff --git a/models/src/main/java/info/frostfs/sdk/mappers/netmap/ReplicaMapper.java b/models/src/main/java/info/frostfs/sdk/mappers/netmap/ReplicaMapper.java index e25ec21..6bb3939 100644 --- a/models/src/main/java/info/frostfs/sdk/mappers/netmap/ReplicaMapper.java +++ b/models/src/main/java/info/frostfs/sdk/mappers/netmap/ReplicaMapper.java @@ -22,7 +22,7 @@ public class ReplicaMapper { } public static Replica toModel(Types.Replica replica) { - if (isNull(replica)) { + if (isNull(replica) || replica.getSerializedSize() == 0) { return null; } diff --git a/models/src/main/java/info/frostfs/sdk/mappers/object/ObjectAttributeMapper.java b/models/src/main/java/info/frostfs/sdk/mappers/object/ObjectAttributeMapper.java index 17c3e94..687aa86 100644 --- a/models/src/main/java/info/frostfs/sdk/mappers/object/ObjectAttributeMapper.java +++ b/models/src/main/java/info/frostfs/sdk/mappers/object/ObjectAttributeMapper.java @@ -22,7 +22,7 @@ public class ObjectAttributeMapper { } public static ObjectAttribute toModel(Types.Header.Attribute attribute) { - if (isNull(attribute)) { + if (isNull(attribute) || attribute.getSerializedSize() == 0) { return null; } diff --git a/models/src/main/java/info/frostfs/sdk/mappers/object/ObjectFilterMapper.java b/models/src/main/java/info/frostfs/sdk/mappers/object/ObjectFilterMapper.java index b1c121d..874e9b1 100644 --- a/models/src/main/java/info/frostfs/sdk/mappers/object/ObjectFilterMapper.java +++ b/models/src/main/java/info/frostfs/sdk/mappers/object/ObjectFilterMapper.java @@ -7,6 +7,7 @@ import info.frostfs.sdk.dto.object.ObjectFilter; import static java.util.Objects.isNull; public class ObjectFilterMapper { + private static final String ERROR_UNKNOWN_VALUE_TEMPLATE = "Unknown MatchType. Value: %s."; private ObjectFilterMapper() { } @@ -19,7 +20,7 @@ public class ObjectFilterMapper { var objectMatchType = Types.MatchType.forNumber(filter.getMatchType().value); if (isNull(objectMatchType)) { throw new IllegalArgumentException( - String.format("Unknown MatchType. Value: %s.", filter.getMatchType().name()) + String.format(ERROR_UNKNOWN_VALUE_TEMPLATE, filter.getMatchType().name()) ); } diff --git a/models/src/main/java/info/frostfs/sdk/mappers/object/ObjectFrostFSMapper.java b/models/src/main/java/info/frostfs/sdk/mappers/object/ObjectFrostFSMapper.java index aa5f698..228661b 100644 --- a/models/src/main/java/info/frostfs/sdk/mappers/object/ObjectFrostFSMapper.java +++ b/models/src/main/java/info/frostfs/sdk/mappers/object/ObjectFrostFSMapper.java @@ -12,7 +12,7 @@ public class ObjectFrostFSMapper { } public static ObjectFrostFS toModel(Types.Object object) { - if (isNull(object)) { + if (isNull(object) || object.getSerializedSize() == 0) { return null; } diff --git a/models/src/main/java/info/frostfs/sdk/mappers/object/ObjectHeaderMapper.java b/models/src/main/java/info/frostfs/sdk/mappers/object/ObjectHeaderMapper.java index fc5b4cc..af8b898 100644 --- a/models/src/main/java/info/frostfs/sdk/mappers/object/ObjectHeaderMapper.java +++ b/models/src/main/java/info/frostfs/sdk/mappers/object/ObjectHeaderMapper.java @@ -7,12 +7,14 @@ import info.frostfs.sdk.dto.object.ObjectHeader; import info.frostfs.sdk.enums.ObjectType; import info.frostfs.sdk.mappers.VersionMapper; import info.frostfs.sdk.mappers.container.ContainerIdMapper; +import org.apache.commons.collections4.ListUtils; import java.util.stream.Collectors; import static java.util.Objects.isNull; public class ObjectHeaderMapper { + private static final String ERROR_UNKNOWN_VALUE_TEMPLATE = "Unknown ObjectType. Value: %s."; private ObjectHeaderMapper() { } @@ -25,7 +27,7 @@ public class ObjectHeaderMapper { var objectType = Types.ObjectType.forNumber(header.getObjectType().value); if (isNull(objectType)) { throw new IllegalArgumentException( - String.format("Unknown ObjectType. Value: %s.", header.getObjectType().name()) + String.format(ERROR_UNKNOWN_VALUE_TEMPLATE, header.getObjectType().name()) ); } @@ -41,21 +43,23 @@ public class ObjectHeaderMapper { } public static ObjectHeader toModel(Types.Header header) { - if (isNull(header)) { + if (isNull(header) || header.getSerializedSize() == 0) { return null; } var objectType = ObjectType.get(header.getObjectTypeValue()); if (isNull(objectType)) { throw new IllegalArgumentException( - String.format("Unknown ObjectType. Value: %s.", header.getObjectType()) + String.format(ERROR_UNKNOWN_VALUE_TEMPLATE, header.getObjectType()) ); } return new ObjectHeader( new ContainerId(header.getContainerId().getValue().toByteArray()), objectType, - header.getAttributesList().stream().map(ObjectAttributeMapper::toModel).collect(Collectors.toList()), + ListUtils.emptyIfNull(header.getAttributesList()).stream() + .map(ObjectAttributeMapper::toModel) + .collect(Collectors.toList()), header.getPayloadLength(), VersionMapper.toModel(header.getVersion()) ); diff --git a/models/src/main/java/info/frostfs/sdk/mappers/object/ObjectIdMapper.java b/models/src/main/java/info/frostfs/sdk/mappers/object/ObjectIdMapper.java index af51095..5c05d17 100644 --- a/models/src/main/java/info/frostfs/sdk/mappers/object/ObjectIdMapper.java +++ b/models/src/main/java/info/frostfs/sdk/mappers/object/ObjectIdMapper.java @@ -22,6 +22,10 @@ public class ObjectIdMapper { } public static ObjectId toModel(Types.ObjectID objectId) { + if (isNull(objectId) || objectId.getSerializedSize() == 0) { + return null; + } + return new ObjectId(objectId.getValue().toByteArray()); } } diff --git a/models/src/test/java/info/frostfs/sdk/UuidExtensionTest.java b/models/src/test/java/info/frostfs/sdk/UuidExtensionTest.java new file mode 100644 index 0000000..6cc40e0 --- /dev/null +++ b/models/src/test/java/info/frostfs/sdk/UuidExtensionTest.java @@ -0,0 +1,57 @@ +package info.frostfs.sdk; + +import org.junit.jupiter.api.Test; + +import java.util.UUID; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class UuidExtensionTest { + private static final UUID TEST_UUID = UUID.fromString("ca70c669-0294-432d-8b83-05fc1475a579"); + private static final byte[] TEST_UUID_BYTES = + new byte[]{-54, 112, -58, 105, 2, -108, 67, 45, -117, -125, 5, -4, 20, 117, -91, 121}; + + @Test + void uuidAsBytes_success() { + //When + var result = UuidExtension.asBytes(TEST_UUID); + + //Then + assertThat(result).hasSize(16).containsExactly(TEST_UUID_BYTES); + } + + @Test + void uuidAsBytes_givenParamsIsNull() { + //When + Then + assertThrows(IllegalArgumentException.class, () -> UuidExtension.asBytes(null)); + } + + @Test + void bytesAsUuid_success() { + //When + var result = UuidExtension.asUuid(TEST_UUID_BYTES); + + //Then + assertEquals(TEST_UUID, result); + } + + @Test + void bytesAsUuid_givenParamsIsNull() { + //When + Then + assertThrows(IllegalArgumentException.class, () -> UuidExtension.asUuid(null)); + } + + @Test + void bytesAsUuid_wrongArraySize() { + //Given + var valueLength15 = new byte[]{-54, 112, -58, 105, 2, -108, 67, 45, -117, -125, 5, -4, 20, 117, -91}; + var valueLength17 = new byte[]{-54, 112, -58, 105, 2, -108, 67, 45, -117, -125, 5, -4, 20, 117, -91, 121, 12}; + + + //When + Then + assertThrows(IllegalArgumentException.class, () -> UuidExtension.asUuid(valueLength15)); + assertThrows(IllegalArgumentException.class, () -> UuidExtension.asUuid(valueLength17)); + } +} diff --git a/models/src/test/java/info/frostfs/sdk/mappers/MetaHeaderMapperTest.java b/models/src/test/java/info/frostfs/sdk/mappers/MetaHeaderMapperTest.java new file mode 100644 index 0000000..7d874cc --- /dev/null +++ b/models/src/test/java/info/frostfs/sdk/mappers/MetaHeaderMapperTest.java @@ -0,0 +1,33 @@ +package info.frostfs.sdk.mappers; + +import info.frostfs.sdk.dto.MetaHeader; +import info.frostfs.sdk.dto.Version; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +public class MetaHeaderMapperTest { + + @Test + void toGrpcMessage_success() { + //Given + var version = new Version(1, 2); + var metaHeader = new MetaHeader(version, 1, 2); + + //When + var result = MetaHeaderMapper.toGrpcMessage(metaHeader); + + //Then + assertNotNull(result); + assertEquals(metaHeader.getEpoch(), result.getEpoch()); + assertEquals(metaHeader.getTtl(), result.getTtl()); + assertEquals(metaHeader.getVersion().getMajor(), result.getVersion().getMajor()); + assertEquals(metaHeader.getVersion().getMinor(), result.getVersion().getMinor()); + } + + @Test + void toGrpcMessage_null() { + //When + Then + assertNull(MetaHeaderMapper.toGrpcMessage(null)); + } +} diff --git a/models/src/test/java/info/frostfs/sdk/mappers/OwnerIdMapperTest.java b/models/src/test/java/info/frostfs/sdk/mappers/OwnerIdMapperTest.java new file mode 100644 index 0000000..bc51895 --- /dev/null +++ b/models/src/test/java/info/frostfs/sdk/mappers/OwnerIdMapperTest.java @@ -0,0 +1,35 @@ +package info.frostfs.sdk.mappers; + +import info.frostfs.sdk.dto.OwnerId; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; + +public class OwnerIdMapperTest { + private static final String OWNER_ID = "NVxUSpEEJzYXZZtUs18PrJTD9QZkLLNQ8S"; + + @Test + void toGrpcMessage_success() { + //Given + var ownerId = new OwnerId(OWNER_ID); + var expected = new byte[]{ + 53, 110, 42, -125, -76, -25, -44, -94, 22, -98, 117, -100, -5, 103, 74, -128, -51, 37, -116, -102, 71, + -1, 95, -4, 3 + }; + + //When + var result = OwnerIdMapper.toGrpcMessage(ownerId); + + //Then + assertNotNull(result); + assertThat(result.getValue().toByteArray()).hasSize(25).containsExactly(expected); + } + + @Test + void toGrpcMessage_null() { + //When + Then + assertNull(OwnerIdMapper.toGrpcMessage(null)); + } +} diff --git a/models/src/test/java/info/frostfs/sdk/mappers/SessionMapperTest.java b/models/src/test/java/info/frostfs/sdk/mappers/SessionMapperTest.java new file mode 100644 index 0000000..f8c125e --- /dev/null +++ b/models/src/test/java/info/frostfs/sdk/mappers/SessionMapperTest.java @@ -0,0 +1,67 @@ +package info.frostfs.sdk.mappers; + +import com.google.protobuf.ByteString; +import frostfs.session.Types; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.*; + +public class SessionMapperTest { + private static final byte[] SERIALIZED_TOKEN = new byte[] + {10, 21, 10, 5, 1, 2, 3, 4, 5, 26, 6, 8, 1, 16, 3, 24, 2, 34, 4, 6, 7, 8, 9}; + + @Test + void serialize_success() { + //Given + var sessionToken = createSessionToken(); + + //When + var result = SessionMapper.serialize(sessionToken); + + //Then + assertNotNull(result); + assertThat(result).hasSize(23).containsExactly(SERIALIZED_TOKEN); + } + + @Test + void serialize_wrong() { + //When + Then + assertThrows(IllegalArgumentException.class, () -> SessionMapper.serialize(null)); + } + + @Test + void deserialize_success() { + //When + var result = SessionMapper.deserializeSessionToken(SERIALIZED_TOKEN); + + //Then + assertNotNull(result); + assertEquals(createSessionToken(), result); + } + + @Test + void deserialize_wrong() { + //When + Then + assertThrows(IllegalArgumentException.class, () -> SessionMapper.deserializeSessionToken(null)); + assertThrows(IllegalArgumentException.class, () -> SessionMapper.deserializeSessionToken(new byte[]{})); + } + + private Types.SessionToken createSessionToken() { + var lifetime = Types.SessionToken.Body.TokenLifetime.newBuilder() + .setExp(1) + .setIat(2) + .setNbf(3) + .build(); + + var body = Types.SessionToken.Body.newBuilder() + .setId(ByteString.copyFrom(new byte[]{1, 2, 3, 4, 5})) + .setSessionKey(ByteString.copyFrom(new byte[]{6, 7, 8, 9})) + .setLifetime(lifetime) + .build(); + + return Types.SessionToken.newBuilder() + .setBody(body) + .build(); + } +} diff --git a/models/src/test/java/info/frostfs/sdk/mappers/SignatureMapperTest.java b/models/src/test/java/info/frostfs/sdk/mappers/SignatureMapperTest.java new file mode 100644 index 0000000..3435ab7 --- /dev/null +++ b/models/src/test/java/info/frostfs/sdk/mappers/SignatureMapperTest.java @@ -0,0 +1,56 @@ +package info.frostfs.sdk.mappers; + +import frostfs.refs.Types; +import info.frostfs.sdk.dto.Signature; +import info.frostfs.sdk.enums.SignatureScheme; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; +import org.mockito.MockedStatic; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.mockStatic; + +public class SignatureMapperTest { + + @ParameterizedTest + @EnumSource(value = SignatureScheme.class) + void toGrpcMessage_success(SignatureScheme scheme) { + //Given + var signature = new Signature(); + signature.setKey(new byte[]{1, 2, 3}); + signature.setSign(new byte[]{4, 5, 6}); + signature.setScheme(scheme); + + //When + var result = SignatureMapper.toGrpcMessage(signature); + + //Then + assertNotNull(result); + assertThat(result.getKey().toByteArray()).containsExactly(signature.getKey()); + assertThat(result.getSign().toByteArray()).containsExactly(signature.getSign()); + assertEquals(signature.getScheme().value, result.getScheme().getNumber()); + } + + @Test + void toGrpcMessage_null() { + //When + Then + assertNull(SignatureMapper.toGrpcMessage(null)); + } + + @Test + void toGrpcMessage_notValidScheme() { + //Given + var signature = new Signature(); + signature.setScheme(SignatureScheme.ECDSA_SHA512); + + //When + Then + try (MockedStatic mockStatic = mockStatic(Types.SignatureScheme.class)) { + mockStatic.when(() -> Types.SignatureScheme.forNumber(signature.getScheme().value)) + .thenReturn(null); + + assertThrows(IllegalArgumentException.class, () -> SignatureMapper.toGrpcMessage(signature)); + } + } +} diff --git a/models/src/test/java/info/frostfs/sdk/mappers/StatusMapperTest.java b/models/src/test/java/info/frostfs/sdk/mappers/StatusMapperTest.java new file mode 100644 index 0000000..d0f5a59 --- /dev/null +++ b/models/src/test/java/info/frostfs/sdk/mappers/StatusMapperTest.java @@ -0,0 +1,53 @@ +package info.frostfs.sdk.mappers; + +import frostfs.status.Types; +import info.frostfs.sdk.enums.StatusCode; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; + +import static org.junit.jupiter.api.Assertions.*; + +public class StatusMapperTest { + + @ParameterizedTest + @EnumSource(value = StatusCode.class) + void toModel_success(StatusCode statusCode) { + //Given + var status = Types.Status.newBuilder() + .setCode(statusCode.value) + .setMessage("test") + .build(); + + //When + var result = StatusMapper.toModel(status); + + //Then + assertNotNull(result); + assertEquals(status.getCode(), result.getCode().value); + assertEquals(status.getMessage(), result.getMessage()); + } + + @Test + void toModel_null() { + //When + var result = StatusMapper.toModel(null); + + //Then + assertNotNull(result); + assertEquals(StatusCode.SUCCESS.value, result.getCode().value); + assertEquals("", result.getMessage()); + } + + @Test + void toModel_notValid() { + //Given + var status = Types.Status.newBuilder() + .setCode(-1) + .setMessage("test") + .build(); + + //When + Then + assertThrows(IllegalArgumentException.class, () -> StatusMapper.toModel(status)); + } +} diff --git a/models/src/test/java/info/frostfs/sdk/mappers/VersionMapperTest.java b/models/src/test/java/info/frostfs/sdk/mappers/VersionMapperTest.java new file mode 100644 index 0000000..3fea7b7 --- /dev/null +++ b/models/src/test/java/info/frostfs/sdk/mappers/VersionMapperTest.java @@ -0,0 +1,54 @@ +package info.frostfs.sdk.mappers; + +import frostfs.refs.Types; +import info.frostfs.sdk.dto.Version; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +public class VersionMapperTest { + + @Test + void toGrpcMessage_success() { + //Given + var version = new Version(1, 2); + + //When + var result = VersionMapper.toGrpcMessage(version); + + //Then + assertNotNull(result); + assertEquals(version.getMajor(), result.getMajor()); + assertEquals(version.getMinor(), result.getMinor()); + } + + @Test + void toGrpcMessage_null() { + //When + Then + assertNull(VersionMapper.toGrpcMessage(null)); + } + + @Test + void toModel_success() { + //Given + var version = Types.Version.newBuilder() + .setMajor(1) + .setMinor(2) + .build(); + + //When + var result = VersionMapper.toModel(version); + + //Then + assertNotNull(result); + assertEquals(version.getMajor(), result.getMajor()); + assertEquals(version.getMinor(), result.getMinor()); + } + + @Test + void toModel_null() { + //When + Then + assertNull(VersionMapper.toModel(null)); + assertNull(VersionMapper.toModel(Types.Version.getDefaultInstance())); + } +} diff --git a/models/src/test/java/info/frostfs/sdk/mappers/container/ContainerIdMapperTest.java b/models/src/test/java/info/frostfs/sdk/mappers/container/ContainerIdMapperTest.java new file mode 100644 index 0000000..a421415 --- /dev/null +++ b/models/src/test/java/info/frostfs/sdk/mappers/container/ContainerIdMapperTest.java @@ -0,0 +1,28 @@ +package info.frostfs.sdk.mappers.container; + +import info.frostfs.sdk.dto.container.ContainerId; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +public class ContainerIdMapperTest { + + @Test + void toGrpcMessage_success() { + //Given + var containerId = new ContainerId("EQGx2QeYHJb53uRwYGzcQaW191sZpdNrjutk6veUSV2R"); + + //When + var result = ContainerIdMapper.toGrpcMessage(containerId); + + //Then + assertNotNull(result); + assertEquals(containerId.getValue(), new ContainerId(result.getValue().toByteArray()).getValue()); + } + + @Test + void toGrpcMessage_null() { + //When + Then + assertNull(ContainerIdMapper.toGrpcMessage(null)); + } +} diff --git a/models/src/test/java/info/frostfs/sdk/mappers/container/ContainerMapperTest.java b/models/src/test/java/info/frostfs/sdk/mappers/container/ContainerMapperTest.java new file mode 100644 index 0000000..7db4345 --- /dev/null +++ b/models/src/test/java/info/frostfs/sdk/mappers/container/ContainerMapperTest.java @@ -0,0 +1,116 @@ +package info.frostfs.sdk.mappers.container; + +import com.google.protobuf.ByteString; +import frostfs.container.Types; +import info.frostfs.sdk.dto.container.Container; +import info.frostfs.sdk.dto.netmap.PlacementPolicy; +import info.frostfs.sdk.dto.netmap.Replica; +import info.frostfs.sdk.enums.BasicAcl; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; + +import java.util.UUID; + +import static info.frostfs.sdk.UuidExtension.asBytes; +import static info.frostfs.sdk.UuidExtension.asUuid; +import static org.junit.jupiter.api.Assertions.*; + +public class ContainerMapperTest { + + @Test + void toGrpcMessage_success() { + //Given + var placementPolicy = new PlacementPolicy(true, new Replica[]{new Replica(1)}); + var container = new Container(BasicAcl.PUBLIC_RW, placementPolicy); + + //When + var result = ContainerMapper.toGrpcMessage(container); + + //Then + assertNotNull(result); + assertEquals(container.getBasicAcl().value, result.getBasicAcl()); + assertEquals(container.getNonce(), asUuid(result.getNonce().toByteArray())); + assertEquals(container.getPlacementPolicy().isUnique(), result.getPlacementPolicy().getUnique()); + assertEquals(placementPolicy.getReplicas().length, result.getPlacementPolicy().getReplicasCount()); + assertEquals( + container.getPlacementPolicy().getReplicas()[0].getCount(), + result.getPlacementPolicy().getReplicasList().get(0).getCount() + ); + assertEquals( + container.getPlacementPolicy().getReplicas()[0].getSelector(), + result.getPlacementPolicy().getReplicasList().get(0).getSelector() + ); + } + + @Test + void toGrpcMessage_null() { + //When + Then + assertNull(ContainerMapper.toGrpcMessage(null)); + } + + @ParameterizedTest + @EnumSource(value = BasicAcl.class) + void toModel_success(BasicAcl basicAcl) { + //Given + var version = frostfs.refs.Types.Version.newBuilder() + .setMajor(1) + .setMinor(2) + .build(); + + var replica = frostfs.netmap.Types.Replica.newBuilder() + .setCount(1) + .setSelector("") + .build(); + + var placementPolicy = frostfs.netmap.Types.PlacementPolicy.newBuilder() + .setUnique(true) + .addReplicas(replica) + .build(); + + var container = Types.Container.newBuilder() + .setBasicAcl(basicAcl.value) + .setNonce(ByteString.copyFrom(asBytes(UUID.randomUUID()))) + .setVersion(version) + .setPlacementPolicy(placementPolicy) + .build(); + + //When + var result = ContainerMapper.toModel(container); + + //Then + assertNotNull(result); + assertEquals(container.getBasicAcl(), result.getBasicAcl().value); + assertEquals(asUuid(container.getNonce().toByteArray()), result.getNonce()); + assertEquals(container.getPlacementPolicy().getUnique(), result.getPlacementPolicy().isUnique()); + assertEquals(placementPolicy.getReplicasCount(), result.getPlacementPolicy().getReplicas().length); + assertEquals( + container.getPlacementPolicy().getReplicasList().get(0).getCount(), + result.getPlacementPolicy().getReplicas()[0].getCount() + ); + assertEquals( + container.getPlacementPolicy().getReplicasList().get(0).getSelector(), + result.getPlacementPolicy().getReplicas()[0].getSelector() + ); + assertEquals(version.getMajor(), result.getVersion().getMajor()); + assertEquals(version.getMinor(), result.getVersion().getMinor()); + } + + @Test + void toModel_null() { + //When + Then + assertNull(ContainerMapper.toModel(null)); + assertNull(ContainerMapper.toModel(Types.Container.getDefaultInstance())); + } + + @Test + void toModel_notValid() { + //Given + var container = Types.Container.newBuilder() + .setBasicAcl(-1) + .build(); + + //When + Then + assertThrows(IllegalArgumentException.class, () -> ContainerMapper.toModel(container)); + } +} diff --git a/models/src/test/java/info/frostfs/sdk/mappers/netmap/NetmapSnapshotMapperTest.java b/models/src/test/java/info/frostfs/sdk/mappers/netmap/NetmapSnapshotMapperTest.java new file mode 100644 index 0000000..731eb51 --- /dev/null +++ b/models/src/test/java/info/frostfs/sdk/mappers/netmap/NetmapSnapshotMapperTest.java @@ -0,0 +1,98 @@ +package info.frostfs.sdk.mappers.netmap; + +import com.google.protobuf.ByteString; +import frostfs.netmap.Service; +import frostfs.netmap.Types; +import org.junit.jupiter.api.Test; + +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.*; + +public class NetmapSnapshotMapperTest { + + @Test + void toModel_success() { + //Given + var version = frostfs.refs.Types.Version.newBuilder() + .setMajor(1) + .setMinor(2) + .build(); + + var nodeInfo1 = Types.NodeInfo.newBuilder() + .setPublicKey(ByteString.copyFrom(new byte[]{1, 2, 3, 4, 5, 6})) + .addAttributes(Types.NodeInfo.Attribute.newBuilder().setKey("key1").setValue("value1").build()) + .addAttributes(Types.NodeInfo.Attribute.newBuilder().setKey("key2").setValue("value2").build()) + .addAddresses("address1") + .addAddresses("address2") + .setState(Types.NodeInfo.State.ONLINE) + .build(); + + var nodeInfo2 = Types.NodeInfo.newBuilder() + .setPublicKey(ByteString.copyFrom(new byte[]{7, 8, 9, 10})) + .addAttributes(Types.NodeInfo.Attribute.newBuilder().setKey("key3").setValue("value3").build()) + .addAttributes(Types.NodeInfo.Attribute.newBuilder().setKey("key4").setValue("value4").build()) + .addAddresses("address3") + .addAddresses("address4") + .setState(Types.NodeInfo.State.MAINTENANCE) + .build(); + + var netmap = Types.Netmap.newBuilder() + .setEpoch(10) + .addNodes(nodeInfo1) + .addNodes(nodeInfo2) + .build(); + + var body = Service.NetmapSnapshotResponse.Body.newBuilder() + .setNetmap(netmap) + .build(); + + var response = Service.NetmapSnapshotResponse.newBuilder() + .setBody(body) + .setMetaHeader(frostfs.session.Types.ResponseMetaHeader.newBuilder().setVersion(version).build()) + .build(); + + //When + var result = NetmapSnapshotMapper.toModel(response); + + //Then + assertNotNull(result); + + var resultNode1 = result.getNodeInfoCollection().get(0); + var resultNode2 = result.getNodeInfoCollection().get(1); + + + assertEquals(netmap.getEpoch(), result.getEpoch()); + assertEquals(netmap.getNodesCount(), result.getNodeInfoCollection().size()); + + assertThat(resultNode1.getPublicKey()).containsExactly(nodeInfo1.getPublicKey().toByteArray()); + assertEquals(nodeInfo1.getState().getNumber(), resultNode1.getState().value); + assertEquals(version.getMajor(), resultNode1.getVersion().getMajor()); + assertEquals(version.getMinor(), resultNode1.getVersion().getMinor()); + assertThat(resultNode1.getAddresses()) + .hasSize(2) + .containsAll(nodeInfo1.getAddressesList()); + assertThat(resultNode1.getAttributes()) + .hasSize(2) + .contains(Map.entry("key1", "value1"), Map.entry("key2", "value2")); + + assertThat(resultNode2.getPublicKey()).containsExactly(nodeInfo2.getPublicKey().toByteArray()); + assertEquals(nodeInfo2.getState().getNumber(), resultNode2.getState().value); + assertEquals(version.getMajor(), resultNode2.getVersion().getMajor()); + assertEquals(version.getMinor(), resultNode2.getVersion().getMinor()); + assertThat(resultNode2.getAddresses()) + .hasSize(2) + .containsAll(nodeInfo2.getAddressesList()); + assertThat(resultNode2.getAttributes()) + .hasSize(2) + .contains(Map.entry("key3", "value3"), Map.entry("key4", "value4")); + } + + @Test + void toModel_null() { + //When + Then + assertNull(NetmapSnapshotMapper.toModel(null)); + assertNull(NetmapSnapshotMapper.toModel(Service.NetmapSnapshotResponse.getDefaultInstance())); + } +} diff --git a/models/src/test/java/info/frostfs/sdk/mappers/netmap/NodeInfoMapperTest.java b/models/src/test/java/info/frostfs/sdk/mappers/netmap/NodeInfoMapperTest.java new file mode 100644 index 0000000..c60fceb --- /dev/null +++ b/models/src/test/java/info/frostfs/sdk/mappers/netmap/NodeInfoMapperTest.java @@ -0,0 +1,123 @@ +package info.frostfs.sdk.mappers.netmap; + +import com.google.protobuf.ByteString; +import frostfs.netmap.Service; +import frostfs.netmap.Types; +import info.frostfs.sdk.enums.NodeState; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; +import org.mockito.MockedStatic; + +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.mockStatic; + +public class NodeInfoMapperTest { + + @ParameterizedTest + @EnumSource(value = Types.NodeInfo.State.class, names = "UNRECOGNIZED", mode = EnumSource.Mode.EXCLUDE) + void toModel_success(Types.NodeInfo.State state) { + //Given + var version = frostfs.refs.Types.Version.newBuilder() + .setMajor(1) + .setMinor(2) + .build(); + + var nodeInfo = Types.NodeInfo.newBuilder() + .setPublicKey(ByteString.copyFrom(new byte[]{1, 2, 3, 4, 5, 6})) + .addAttributes(Types.NodeInfo.Attribute.newBuilder().setKey("key1").setValue("value1").build()) + .addAttributes(Types.NodeInfo.Attribute.newBuilder().setKey("key2").setValue("value2").build()) + .addAddresses("address1") + .addAddresses("address2") + .setState(state) + .build(); + + var body = Service.LocalNodeInfoResponse.Body.newBuilder() + .setNodeInfo(nodeInfo) + .setVersion(version) + .build(); + + //When + var result = NodeInfoMapper.toModel(body); + + //Then + assertNotNull(result); + assertThat(result.getPublicKey()).containsExactly(nodeInfo.getPublicKey().toByteArray()); + assertEquals(nodeInfo.getState().getNumber(), result.getState().value); + assertEquals(version.getMajor(), result.getVersion().getMajor()); + assertEquals(version.getMinor(), result.getVersion().getMinor()); + assertThat(result.getAddresses()) + .hasSize(2) + .containsAll(nodeInfo.getAddressesList()); + assertThat(result.getAttributes()) + .hasSize(2) + .contains(Map.entry("key1", "value1"), Map.entry("key2", "value2")); + } + + @ParameterizedTest + @EnumSource(value = Types.NodeInfo.State.class, names = "UNRECOGNIZED", mode = EnumSource.Mode.EXCLUDE) + void toModel2_success(Types.NodeInfo.State state) { + //Given + var version = frostfs.refs.Types.Version.newBuilder() + .setMajor(1) + .setMinor(2) + .build(); + + var nodeInfo = Types.NodeInfo.newBuilder() + .setPublicKey(ByteString.copyFrom(new byte[]{1, 2, 3, 4, 5, 6})) + .addAttributes(Types.NodeInfo.Attribute.newBuilder().setKey("key1").setValue("value1").build()) + .addAttributes(Types.NodeInfo.Attribute.newBuilder().setKey("key2").setValue("value2").build()) + .addAddresses("address1") + .addAddresses("address2") + .setState(state) + .build(); + + //When + var result = NodeInfoMapper.toModel(nodeInfo, version); + + //Then + assertNotNull(result); + assertThat(result.getPublicKey()).containsExactly(nodeInfo.getPublicKey().toByteArray()); + assertEquals(nodeInfo.getState().getNumber(), result.getState().value); + assertEquals(version.getMajor(), result.getVersion().getMajor()); + assertEquals(version.getMinor(), result.getVersion().getMinor()); + assertThat(result.getAddresses()) + .hasSize(2) + .containsAll(nodeInfo.getAddressesList()); + assertThat(result.getAttributes()) + .hasSize(2) + .contains(Map.entry("key1", "value1"), Map.entry("key2", "value2")); + } + + @Test + void toModel_null() { + //When + Then + assertNull(NodeInfoMapper.toModel(null)); + assertNull(NodeInfoMapper.toModel(null, null)); + assertNull(NodeInfoMapper.toModel(Service.LocalNodeInfoResponse.Body.getDefaultInstance())); + assertNull(NodeInfoMapper.toModel(Types.NodeInfo.getDefaultInstance(), null)); + } + + @Test + void toModel_notValidScheme() { + //Given + var nodeInfo = Types.NodeInfo.newBuilder() + .setState(Types.NodeInfo.State.ONLINE) + .build(); + + var body = Service.LocalNodeInfoResponse.Body.newBuilder() + .setNodeInfo(nodeInfo) + .build(); + + //When + Then + try (MockedStatic mockStatic = mockStatic(NodeState.class)) { + mockStatic.when(() -> NodeState.get(Types.NodeInfo.State.ONLINE.getNumber())) + .thenReturn(null); + + assertThrows(IllegalArgumentException.class, () -> NodeInfoMapper.toModel(body)); + } + } +} diff --git a/models/src/test/java/info/frostfs/sdk/mappers/netmap/PlacementPolicyMapperTest.java b/models/src/test/java/info/frostfs/sdk/mappers/netmap/PlacementPolicyMapperTest.java new file mode 100644 index 0000000..3cc57ae --- /dev/null +++ b/models/src/test/java/info/frostfs/sdk/mappers/netmap/PlacementPolicyMapperTest.java @@ -0,0 +1,77 @@ +package info.frostfs.sdk.mappers.netmap; + +import frostfs.netmap.Types; +import info.frostfs.sdk.dto.netmap.PlacementPolicy; +import info.frostfs.sdk.dto.netmap.Replica; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +public class PlacementPolicyMapperTest { + + @Test + void toGrpcMessage_success() { + //Given + var replica1 = new Replica(1, "test1"); + var replica2 = new Replica(2, "test2"); + + var placementPolicy = new PlacementPolicy(true, new Replica[]{replica1, replica2}); + + //When + var result = PlacementPolicyMapper.toGrpcMessage(placementPolicy); + + //Then + assertNotNull(result); + assertEquals(placementPolicy.isUnique(), result.getUnique()); + assertEquals(placementPolicy.getReplicas().length, result.getReplicasCount()); + assertEquals(replica1.getCount(), result.getReplicas(0).getCount()); + assertEquals(replica1.getSelector(), result.getReplicas(0).getSelector()); + assertEquals(replica2.getCount(), result.getReplicas(1).getCount()); + assertEquals(replica2.getSelector(), result.getReplicas(1).getSelector()); + } + + @Test + void toGrpcMessage_null() { + //When + Then + assertNull(PlacementPolicyMapper.toGrpcMessage(null)); + } + + @Test + void toModel_success() { + //Given + var replica1 = Types.Replica.newBuilder() + .setCount(1) + .setSelector("test1") + .build(); + + var replica2 = Types.Replica.newBuilder() + .setCount(2) + .setSelector("test2") + .build(); + + var placementPolicy = Types.PlacementPolicy.newBuilder() + .setUnique(true) + .addReplicas(replica1) + .addReplicas(replica2) + .build(); + + //When + var result = PlacementPolicyMapper.toModel(placementPolicy); + + //Then + assertNotNull(result); + assertEquals(placementPolicy.getUnique(), result.isUnique()); + assertEquals(placementPolicy.getReplicasCount(), result.getReplicas().length); + assertEquals(replica1.getCount(), result.getReplicas()[0].getCount()); + assertEquals(replica1.getSelector(), result.getReplicas()[0].getSelector()); + assertEquals(replica2.getCount(), result.getReplicas()[1].getCount()); + assertEquals(replica2.getSelector(), result.getReplicas()[1].getSelector()); + } + + @Test + void toModel_null() { + //When + Then + assertNull(PlacementPolicyMapper.toModel(null)); + assertNull(PlacementPolicyMapper.toModel(Types.PlacementPolicy.getDefaultInstance())); + } +} diff --git a/models/src/test/java/info/frostfs/sdk/mappers/netmap/ReplicaMapperTest.java b/models/src/test/java/info/frostfs/sdk/mappers/netmap/ReplicaMapperTest.java new file mode 100644 index 0000000..973e74c --- /dev/null +++ b/models/src/test/java/info/frostfs/sdk/mappers/netmap/ReplicaMapperTest.java @@ -0,0 +1,54 @@ +package info.frostfs.sdk.mappers.netmap; + +import frostfs.netmap.Types; +import info.frostfs.sdk.dto.netmap.Replica; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +public class ReplicaMapperTest { + + @Test + void toGrpcMessage_success() { + //Given + var replica = new Replica(1); + + //When + var result = ReplicaMapper.toGrpcMessage(replica); + + //Then + assertNotNull(result); + assertEquals(replica.getCount(), result.getCount()); + assertEquals(replica.getSelector(), result.getSelector()); + } + + @Test + void toGrpcMessage_null() { + //When + Then + assertNull(ReplicaMapper.toGrpcMessage(null)); + } + + @Test + void toModel_success() { + //Given + var replica = Types.Replica.newBuilder() + .setCount(1) + .setSelector("") + .build(); + + //When + var result = ReplicaMapper.toModel(replica); + + //Then + assertNotNull(result); + assertEquals(replica.getCount(), result.getCount()); + assertEquals(replica.getSelector(), result.getSelector()); + } + + @Test + void toModel_null() { + //When + Then + assertNull(ReplicaMapper.toModel(null)); + assertNull(ReplicaMapper.toModel(Types.Replica.getDefaultInstance())); + } +} diff --git a/models/src/test/java/info/frostfs/sdk/mappers/object/ObjectAttributeMapperTest.java b/models/src/test/java/info/frostfs/sdk/mappers/object/ObjectAttributeMapperTest.java new file mode 100644 index 0000000..4ae2902 --- /dev/null +++ b/models/src/test/java/info/frostfs/sdk/mappers/object/ObjectAttributeMapperTest.java @@ -0,0 +1,54 @@ +package info.frostfs.sdk.mappers.object; + +import frostfs.object.Types; +import info.frostfs.sdk.dto.object.ObjectAttribute; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +public class ObjectAttributeMapperTest { + + @Test + void toGrpcMessage_success() { + //Given + var objectAttribute = new ObjectAttribute("key", "value"); + + //When + var result = ObjectAttributeMapper.toGrpcMessage(objectAttribute); + + //Then + assertNotNull(result); + assertEquals(objectAttribute.getKey(), result.getKey()); + assertEquals(objectAttribute.getValue(), result.getValue()); + } + + @Test + void toGrpcMessage_null() { + //When + Then + assertNull(ObjectAttributeMapper.toGrpcMessage(null)); + } + + @Test + void toModel_success() { + //Given + var objectAttribute = Types.Header.Attribute.newBuilder() + .setKey("key") + .setValue("value") + .build(); + + //When + var result = ObjectAttributeMapper.toModel(objectAttribute); + + //Then + assertNotNull(result); + assertEquals(objectAttribute.getKey(), result.getKey()); + assertEquals(objectAttribute.getValue(), result.getValue()); + } + + @Test + void toModel_null() { + //When + Then + assertNull(ObjectAttributeMapper.toModel(null)); + assertNull(ObjectAttributeMapper.toModel(Types.Header.Attribute.getDefaultInstance())); + } +} diff --git a/models/src/test/java/info/frostfs/sdk/mappers/object/ObjectFilterMapperTest.java b/models/src/test/java/info/frostfs/sdk/mappers/object/ObjectFilterMapperTest.java new file mode 100644 index 0000000..3451ccc --- /dev/null +++ b/models/src/test/java/info/frostfs/sdk/mappers/object/ObjectFilterMapperTest.java @@ -0,0 +1,53 @@ +package info.frostfs.sdk.mappers.object; + +import frostfs.object.Types; +import info.frostfs.sdk.dto.object.ObjectFilter; +import info.frostfs.sdk.enums.ObjectMatchType; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; +import org.mockito.MockedStatic; + +import static info.frostfs.sdk.enums.ObjectMatchType.UNSPECIFIED; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.mockStatic; + +public class ObjectFilterMapperTest { + + @ParameterizedTest + @EnumSource(value = ObjectMatchType.class) + void toGrpcMessage_success(ObjectMatchType type) { + //Given + var objectFilter = new ObjectFilter(type, "key", "value"); + + //When + var result = ObjectFilterMapper.toGrpcMessage(objectFilter); + + //Then + assertNotNull(result); + assertEquals(objectFilter.getKey(), result.getKey()); + assertEquals(objectFilter.getValue(), result.getValue()); + assertEquals(objectFilter.getMatchType().value, result.getMatchType().getNumber()); + + } + + @Test + void toGrpcMessage_null() { + //When + Then + assertNull(ObjectFilterMapper.toGrpcMessage(null)); + } + + @Test + void toGrpcMessage_notValidScheme() { + //Given + var objectFilter = new ObjectFilter(UNSPECIFIED, "key", "value"); + + //When + Then + try (MockedStatic mockStatic = mockStatic(Types.MatchType.class)) { + mockStatic.when(() -> Types.MatchType.forNumber(objectFilter.getMatchType().value)) + .thenReturn(null); + + assertThrows(IllegalArgumentException.class, () -> ObjectFilterMapper.toGrpcMessage(objectFilter)); + } + } +} diff --git a/models/src/test/java/info/frostfs/sdk/mappers/object/ObjectFrostFSMapperTest.java b/models/src/test/java/info/frostfs/sdk/mappers/object/ObjectFrostFSMapperTest.java new file mode 100644 index 0000000..dbcf9b7 --- /dev/null +++ b/models/src/test/java/info/frostfs/sdk/mappers/object/ObjectFrostFSMapperTest.java @@ -0,0 +1,60 @@ +package info.frostfs.sdk.mappers.object; + +import com.google.protobuf.ByteString; +import frostfs.object.Types; +import info.frostfs.sdk.dto.container.ContainerId; +import info.frostfs.sdk.dto.object.ObjectId; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.*; + +public class ObjectFrostFSMapperTest { + @Test + void toModel_success() { + //Given + var containerId = new ContainerId("EQGx2QeYHJb53uRwYGzcQaW191sZpdNrjutk6veUSV2R"); + var cid = frostfs.refs.Types.ContainerID.newBuilder() + .setValue(ByteString.copyFrom(containerId.toHash())) + .build(); + var version = frostfs.refs.Types.Version.newBuilder() + .setMajor(1) + .setMinor(2) + .build(); + var header = Types.Header.newBuilder() + .setObjectType(Types.ObjectType.REGULAR) + .setVersion(version) + .setContainerId(cid) + .setPayloadLength(10) + .build(); + + var objectId = new ObjectId("85orCLKSu3X1jGiTFmwmTUsBU88RBARNwuRwrEy5pyww"); + var object = Types.Object.newBuilder() + .setObjectId(ObjectIdMapper.toGrpcMessage(objectId)) + .setPayload(ByteString.copyFrom(new byte[]{1, 2, 3, 4, 5})) + .setHeader(header) + .build(); + + //When + var result = ObjectFrostFSMapper.toModel(object); + + //Then + assertNotNull(result); + assertEquals(objectId.getValue(), result.getObjectId().getValue()); + assertThat(result.getPayload()).containsExactly(object.getPayload().toByteArray()); + assertThat(result.getHeader().getAttributes()).hasSize(0); + assertEquals(header.getObjectTypeValue(), result.getHeader().getObjectType().value); + assertEquals(version.getMajor(), result.getHeader().getVersion().getMajor()); + assertEquals(version.getMinor(), result.getHeader().getVersion().getMinor()); + assertEquals(header.getPayloadLength(), result.getHeader().getPayloadLength()); + assertEquals(containerId.getValue(), result.getHeader().getContainerId().getValue()); + } + + @Test + void toModel_null() { + //When + Then + assertNull(ObjectFrostFSMapper.toModel(null)); + assertNull(ObjectFrostFSMapper.toModel(Types.Object.getDefaultInstance())); + } + +} diff --git a/models/src/test/java/info/frostfs/sdk/mappers/object/ObjectHeaderMapperTest.java b/models/src/test/java/info/frostfs/sdk/mappers/object/ObjectHeaderMapperTest.java new file mode 100644 index 0000000..dd410b2 --- /dev/null +++ b/models/src/test/java/info/frostfs/sdk/mappers/object/ObjectHeaderMapperTest.java @@ -0,0 +1,127 @@ +package info.frostfs.sdk.mappers.object; + +import com.google.protobuf.ByteString; +import frostfs.object.Types; +import info.frostfs.sdk.dto.container.ContainerId; +import info.frostfs.sdk.dto.object.ObjectAttribute; +import info.frostfs.sdk.dto.object.ObjectHeader; +import info.frostfs.sdk.enums.ObjectType; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; +import org.mockito.MockedStatic; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.mockStatic; + +public class ObjectHeaderMapperTest { + + @ParameterizedTest + @EnumSource(value = ObjectType.class) + void toGrpcMessage_success(ObjectType type) { + //Given + var containerId = new ContainerId("EQGx2QeYHJb53uRwYGzcQaW191sZpdNrjutk6veUSV2R"); + var objectHeader = new ObjectHeader( + containerId, + type, + List.of(new ObjectAttribute("Filename", "cat.jpg"), new ObjectAttribute("Filename2", "cat2.jpg")) + ); + + //When + var result = ObjectHeaderMapper.toGrpcMessage(objectHeader); + + //Then + assertNotNull(result); + assertEquals( + objectHeader.getContainerId().getValue(), + new ContainerId(result.getContainerId().getValue().toByteArray()).getValue() + ); + assertEquals(objectHeader.getObjectType().value, result.getObjectType().getNumber()); + assertEquals(objectHeader.getAttributes().size(), result.getAttributesCount()); + + assertEquals(objectHeader.getAttributes().get(0).getKey(), result.getAttributes(0).getKey()); + assertEquals(objectHeader.getAttributes().get(0).getValue(), result.getAttributes(0).getValue()); + + assertEquals(objectHeader.getAttributes().get(1).getKey(), result.getAttributes(1).getKey()); + assertEquals(objectHeader.getAttributes().get(1).getValue(), result.getAttributes(1).getValue()); + } + + @Test + void toGrpcMessage_null() { + //When + Then + assertNull(ObjectHeaderMapper.toGrpcMessage(null)); + } + + @Test + void toGrpcMessage_notValidScheme() { + //Given + var containerId = new ContainerId("EQGx2QeYHJb53uRwYGzcQaW191sZpdNrjutk6veUSV2R"); + var objectHeader = new ObjectHeader(containerId, ObjectType.REGULAR, null); + + //When + Then + try (MockedStatic mockStatic = mockStatic(Types.ObjectType.class)) { + mockStatic.when(() -> Types.ObjectType.forNumber(objectHeader.getObjectType().value)) + .thenReturn(null); + + assertThrows(IllegalArgumentException.class, () -> ObjectHeaderMapper.toGrpcMessage(objectHeader)); + } + } + + @ParameterizedTest + @EnumSource(value = Types.ObjectType.class, names = "UNRECOGNIZED", mode = EnumSource.Mode.EXCLUDE) + void toModel_success(Types.ObjectType type) { + //Given + var containerId = new ContainerId("EQGx2QeYHJb53uRwYGzcQaW191sZpdNrjutk6veUSV2R"); + var cid = frostfs.refs.Types.ContainerID.newBuilder() + .setValue(ByteString.copyFrom(containerId.toHash())) + .build(); + var version = frostfs.refs.Types.Version.newBuilder() + .setMajor(1) + .setMinor(2) + .build(); + var header = Types.Header.newBuilder() + .setObjectType(type) + .setVersion(version) + .setContainerId(cid) + .setPayloadLength(10) + .build(); + + //When + var result = ObjectHeaderMapper.toModel(header); + + //Then + assertNotNull(result); + assertThat(result.getAttributes()).hasSize(0); + assertEquals(header.getObjectTypeValue(), result.getObjectType().value); + assertEquals(version.getMajor(), result.getVersion().getMajor()); + assertEquals(version.getMinor(), result.getVersion().getMinor()); + assertEquals(header.getPayloadLength(), result.getPayloadLength()); + assertEquals(containerId.getValue(), result.getContainerId().getValue()); + } + + @Test + void toModel_null() { + //When + Then + assertNull(ObjectHeaderMapper.toModel(null)); + assertNull(ObjectHeaderMapper.toModel(Types.Header.getDefaultInstance())); + } + + @Test + void toModel_notValidScheme() { + //Given + var header = Types.Header.newBuilder() + .setObjectType(Types.ObjectType.TOMBSTONE) + .build(); + + //When + Then + try (MockedStatic mockStatic = mockStatic(ObjectType.class)) { + mockStatic.when(() -> ObjectType.get(Types.ObjectType.TOMBSTONE.getNumber())) + .thenReturn(null); + + assertThrows(IllegalArgumentException.class, () -> ObjectHeaderMapper.toModel(header)); + } + } +} diff --git a/models/src/test/java/info/frostfs/sdk/mappers/object/ObjectIdMapperTest.java b/models/src/test/java/info/frostfs/sdk/mappers/object/ObjectIdMapperTest.java new file mode 100644 index 0000000..ab5a722 --- /dev/null +++ b/models/src/test/java/info/frostfs/sdk/mappers/object/ObjectIdMapperTest.java @@ -0,0 +1,53 @@ +package info.frostfs.sdk.mappers.object; + +import com.google.protobuf.ByteString; +import frostfs.refs.Types; +import info.frostfs.sdk.dto.object.ObjectId; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +public class ObjectIdMapperTest { + + @Test + void toGrpcMessage_success() { + //Given + var objectId = new ObjectId("85orCLKSu3X1jGiTFmwmTUsBU88RBARNwuRwrEy5pyww"); + + //When + var result = ObjectIdMapper.toGrpcMessage(objectId); + + //Then + assertNotNull(result); + assertEquals(objectId.getValue(), new ObjectId(result.getValue().toByteArray()).getValue()); + } + + @Test + void toGrpcMessage_null() { + //When + Then + assertNull(ObjectIdMapper.toGrpcMessage(null)); + } + + @Test + void toModel_success() { + //Given + var objectId = new ObjectId("85orCLKSu3X1jGiTFmwmTUsBU88RBARNwuRwrEy5pyww"); + var objectIdGrpc = Types.ObjectID.newBuilder() + .setValue(ByteString.copyFrom(objectId.toHash())) + .build(); + + //When + var result = ObjectIdMapper.toModel(objectIdGrpc); + + //Then + assertNotNull(result); + assertEquals(objectId.getValue(), result.getValue()); + } + + @Test + void toModel_null() { + //When + Then + assertNull(ObjectIdMapper.toModel(null)); + assertNull(ObjectIdMapper.toModel(Types.ObjectID.getDefaultInstance())); + } +} diff --git a/pom.xml b/pom.xml index 16dc005..9b5a976 100644 --- a/pom.xml +++ b/pom.xml @@ -20,8 +20,32 @@ 11 UTF-8 checkstyle.xml + 5.12.0 + 5.10.3 + 3.26.3 + + + org.junit.jupiter + junit-jupiter + ${junit.version} + test + + + org.assertj + assertj-core + ${assertj.version} + test + + + org.mockito + mockito-core + ${mockito.version} + test + + +