Compare commits

...
Sign in to create a new pull request.

7 commits

Author SHA1 Message Date
c2d70c4f3e [#49] ci: Publish SDK packages to our Maven registry
Signed-off-by: Vitaliy Potyarkin <v.potyarkin@yadro.com>
2025-03-10 14:30:39 +03:00
Ori Bruk
8b44db3714 [#49] Provide auto deploy after merge
Signed-off-by: Ori Bruk <o.bruk@yadro.com>
2025-03-10 14:25:36 +03:00
Ori Bruk
39158348dd [#47] Add APE rule deserializer
Signed-off-by: Ori Bruk <o.bruk@yadro.com>
2025-03-05 14:08:51 +03:00
Ori Bruk
db74919401 [#45] Add the ability to create a client via wallet and password
Signed-off-by: Ori Bruk <o.bruk@yadro.com>
2025-03-05 11:14:42 +03:00
Ori Bruk
fe7d2968b8 [#43] Expanding the parameters for creating a container
Signed-off-by: Ori Bruk <o.bruk@yadro.com>
2025-02-20 14:35:32 +03:00
Ori Bruk
3861eb0dc2 [#41] Add APE rule serializer
Signed-off-by: Ori Bruk <o.bruk@yadro.com>
2025-02-13 20:57:31 +03:00
Ori Bruk
532db56f1b [#39] Fix loading large objects in chunks
Signed-off-by: Ori Bruk <o.bruk@yadro.com>
2025-02-11 17:14:30 +03:00
85 changed files with 2099 additions and 836 deletions

View file

@ -0,0 +1,22 @@
on:
push:
workflow_dispatch:
jobs:
image:
name: Publish Maven packages
runs-on: docker
container: git.frostfs.info/truecloudlab/env:openjdk-11-maven-3.8.6
steps:
- name: Clone git repo
uses: actions/checkout@v3
- name: Publish release packages
run: mvn clean --batch-mode --update-snapshots deploy
if: >-
startsWith(github.ref, 'refs/tags/v') &&
(github.event_name == 'workflow_dispatch' || github.event_name == 'push')
env:
MAVEN_REGISTRY: TrueCloudLab
MAVEN_REGISTRY_USER: ${{secrets.MAVEN_REGISTRY_USER}}
MAVEN_REGISTRY_PASSWORD: ${{secrets.MAVEN_REGISTRY_PASSWORD}}

1
.gitignore vendored
View file

@ -3,6 +3,7 @@ target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
**/.flattened-pom.xml
### IntelliJ IDEA ###
.idea/modules.xml

37
CHANGELOG.md Normal file
View file

@ -0,0 +1,37 @@
# Changelog
## [0.9.0] - 2025-03-05
### Added
- APE rule deserializer
## [0.8.0] - 2025-03-04
### Added
- Creating client via wallet and password
## [0.7.0] - 2025-02-20
### Added
- Expanding the parameters for creating a container
### Fixed
- Creating a session for working with objects
## [0.6.0] - 2025-02-13
### Added
- APE rules serializer
## [0.5.0] - 2025-02-11
### Fixed
- Loading large objects in chunks
- .gitignore
- pom revision

View file

@ -25,7 +25,6 @@ import info.frostfs.sdk.FrostFSClient;
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 info.frostfs.sdk.jdo.ClientSettings;
import info.frostfs.sdk.jdo.parameters.CallContext;
import info.frostfs.sdk.jdo.parameters.container.PrmContainerCreate;
@ -41,8 +40,8 @@ public class ContainerExample {
FrostFSClient frostFSClient = new FrostFSClient(clientSettings);
// Create container
var placementPolicy = new PlacementPolicy(new Replica[]{new Replica(1)}, true);
var prmContainerCreate = new PrmContainerCreate(new Container(BasicAcl.PUBLIC_RW, placementPolicy));
var placementPolicy = new PlacementPolicy(new Replica[]{new Replica(3)}, true, 1);
var prmContainerCreate = new PrmContainerCreate(new Container(placementPolicy));
var containerId = frostFSClient.createContainer(prmContainerCreate, callContext);
// Get container

View file

@ -1,7 +1,7 @@
package info.frostfs.sdk;
import frostfs.accounting.Types;
import info.frostfs.sdk.dto.chain.Chain;
import info.frostfs.sdk.dto.ape.Chain;
import info.frostfs.sdk.dto.container.Container;
import info.frostfs.sdk.dto.container.ContainerId;
import info.frostfs.sdk.dto.netmap.NetmapSnapshot;
@ -14,6 +14,7 @@ import info.frostfs.sdk.dto.session.SessionToken;
import info.frostfs.sdk.exceptions.ProcessFrostFSException;
import info.frostfs.sdk.jdo.ClientEnvironment;
import info.frostfs.sdk.jdo.ClientSettings;
import info.frostfs.sdk.jdo.ECDsa;
import info.frostfs.sdk.jdo.NetworkSettings;
import info.frostfs.sdk.jdo.parameters.CallContext;
import info.frostfs.sdk.jdo.parameters.ape.PrmApeChainAdd;
@ -41,6 +42,7 @@ import info.frostfs.sdk.utils.Validator;
import io.grpc.Channel;
import io.grpc.ClientInterceptors;
import io.grpc.ManagedChannel;
import org.apache.commons.lang3.StringUtils;
import java.util.List;
@ -67,10 +69,12 @@ public class FrostFSClient implements CommonClient {
? clientSettings.getChannel()
: initGrpcChannel(clientSettings);
var ecdsa = StringUtils.isBlank(clientSettings.getWif())
? new ECDsa(clientSettings.getWallet(), clientSettings.getPassword())
: new ECDsa(clientSettings.getWif());
Channel interceptChannel = ClientInterceptors.intercept(channel, MONITORING_CLIENT_INTERCEPTOR);
ClientEnvironment clientEnvironment = new ClientEnvironment(
clientSettings.getKey(), interceptChannel, new Version(), this,
new SessionCache(0)
ecdsa, interceptChannel, new Version(), this, new SessionCache(0)
);
Validator.validate(clientEnvironment);

View file

@ -0,0 +1,12 @@
package info.frostfs.sdk.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE})
public @interface ComplexAtLeastOneIsFilled {
AtLeastOneIsFilled[] value();
}

View file

@ -0,0 +1,30 @@
package info.frostfs.sdk.constants;
public class RuleConst {
public static final byte VERSION = 0;
public static final int BYTE_SIZE = 1;
public static final int U_INT_8_SIZE = BYTE_SIZE;
public static final int BOOL_SIZE = BYTE_SIZE;
public static final long NULL_SLICE = -1L;
public static final int NULL_SLICE_SIZE = 1;
public static final byte BYTE_TRUE = 1;
public static final byte BYTE_FALSE = 0;
// maxSliceLen taken from
// https://github.com/neo-project/neo/blob/38218bbee5bbe8b33cd8f9453465a19381c9a547/src/Neo/IO/Helper.cs#L77
public static final int MAX_SLICE_LENGTH = 0x1000000;
public static final int MAX_VAR_INT_LENGTH = 10;
public static final int CHAIN_MARSHAL_VERSION = 0;
public static final long OFFSET127 = 0x7f;
public static final long OFFSET128 = 0x80;
public static final int UNSIGNED_SERIALIZE_SIZE = 7;
private RuleConst() {
}
}

View file

@ -36,10 +36,10 @@ public class ClientEnvironment {
private SessionCache sessionCache;
public ClientEnvironment(String wif, Channel channel, Version version, FrostFSClient frostFSClient,
public ClientEnvironment(ECDsa key, Channel channel, Version version, FrostFSClient frostFSClient,
SessionCache sessionCache) {
this.key = new ECDsa(wif);
this.ownerId = new OwnerId(key.getPublicKeyByte());
this.key = key;
this.ownerId = new OwnerId(key.getAccount().getAddress());
this.version = version;
this.channel = channel;
this.frostFSClient = frostFSClient;
@ -47,16 +47,6 @@ public class ClientEnvironment {
this.address = channel.authority();
}
public ClientEnvironment(ECDsa key, Channel channel, Version version, FrostFSClient frostFSClient,
SessionCache sessionCache) {
this.key = key;
this.ownerId = new OwnerId(key.getPublicKeyByte());
this.version = version;
this.channel = channel;
this.frostFSClient = frostFSClient;
this.sessionCache = sessionCache;
}
public String getSessionKey() {
if (StringUtils.isBlank(sessionKey)) {
this.sessionKey = formCacheKey(address, getHexString(key.getPublicKeyByte()));

View file

@ -1,37 +1,61 @@
package info.frostfs.sdk.jdo;
import info.frostfs.sdk.annotations.AtLeastOneIsFilled;
import info.frostfs.sdk.annotations.NotNull;
import info.frostfs.sdk.annotations.ComplexAtLeastOneIsFilled;
import io.grpc.ChannelCredentials;
import io.grpc.ManagedChannel;
import lombok.Getter;
import lombok.experimental.FieldNameConstants;
import java.io.File;
@Getter
@FieldNameConstants
@AtLeastOneIsFilled(fields = {ClientSettings.Fields.host, ClientSettings.Fields.channel})
@ComplexAtLeastOneIsFilled(value = {
@AtLeastOneIsFilled(fields = {ClientSettings.Fields.host, ClientSettings.Fields.channel}),
@AtLeastOneIsFilled(fields = {ClientSettings.Fields.wif, ClientSettings.Fields.wallet}),
})
public class ClientSettings {
@NotNull
private final String key;
private String wif;
private File wallet;
private String password;
private String host;
private ChannelCredentials credentials;
private ManagedChannel channel;
public ClientSettings(String key, String host) {
this.key = key;
public ClientSettings(String wif, String host) {
this.wif = wif;
this.host = host;
}
public ClientSettings(String key, String host, ChannelCredentials credentials) {
this.key = key;
public ClientSettings(String wif, String host, ChannelCredentials credentials) {
this.wif = wif;
this.host = host;
this.credentials = credentials;
}
public ClientSettings(String key, ManagedChannel channel) {
this.key = key;
public ClientSettings(String wif, ManagedChannel channel) {
this.wif = wif;
this.channel = channel;
}
public ClientSettings(File wallet, String password, String host) {
this.wallet = wallet;
this.password = password;
this.host = host;
}
public ClientSettings(File wallet, String password, String host, ChannelCredentials credentials) {
this.wallet = wallet;
this.password = password;
this.host = host;
this.credentials = credentials;
}
public ClientSettings(File wallet, String password, ManagedChannel channel) {
this.wallet = wallet;
this.password = password;
this.channel = channel;
}
}

View file

@ -1,14 +1,25 @@
package info.frostfs.sdk.jdo;
import info.frostfs.sdk.annotations.NotNull;
import info.frostfs.sdk.exceptions.FrostFSException;
import info.frostfs.sdk.exceptions.ValidationFrostFSException;
import io.neow3j.wallet.Account;
import io.neow3j.wallet.nep6.NEP6Account;
import io.neow3j.wallet.nep6.NEP6Wallet;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;
import java.io.File;
import java.io.FileInputStream;
import java.security.PrivateKey;
import java.util.Optional;
import static info.frostfs.sdk.KeyExtension.*;
import static info.frostfs.sdk.KeyExtension.loadPrivateKey;
import static info.frostfs.sdk.constants.ErrorConst.WALLET_IS_INVALID;
import static info.frostfs.sdk.constants.ErrorConst.WIF_IS_INVALID;
import static info.frostfs.sdk.constants.FieldConst.EMPTY_STRING;
import static io.neow3j.wallet.Wallet.OBJECT_MAPPER;
import static java.util.Objects.isNull;
@Getter
public class ECDsa {
@ -22,13 +33,41 @@ public class ECDsa {
@NotNull
private final PrivateKey privateKey;
@NotNull
private final Account account;
public ECDsa(String wif) {
if (StringUtils.isEmpty(wif)) {
throw new ValidationFrostFSException(WIF_IS_INVALID);
}
this.privateKeyByte = getPrivateKeyFromWIF(wif);
this.publicKeyByte = loadPublicKey(privateKeyByte);
this.account = Account.fromWIF(wif);
this.privateKeyByte = account.getECKeyPair().getPrivateKey().getBytes();
this.publicKeyByte = account.getECKeyPair().getPublicKey().getEncoded(true);
this.privateKey = loadPrivateKey(privateKeyByte);
}
public ECDsa(File walletFile, String password) {
if (isNull(walletFile)) {
throw new ValidationFrostFSException(WALLET_IS_INVALID);
}
try (var walletStream = new FileInputStream(walletFile)) {
NEP6Wallet nep6Wallet = OBJECT_MAPPER.readValue(walletStream, NEP6Wallet.class);
Optional<NEP6Account> defaultAccount = nep6Wallet.getAccounts().stream()
.filter(NEP6Account::getDefault)
.findFirst();
var account = defaultAccount.map(Account::fromNEP6Account)
.orElseGet(() -> Account.fromNEP6Account(nep6Wallet.getAccounts().get(0)));
account.decryptPrivateKey(isNull(password) ? EMPTY_STRING : password);
this.account = account;
this.privateKeyByte = account.getECKeyPair().getPrivateKey().getBytes();
this.publicKeyByte = account.getECKeyPair().getPublicKey().getEncoded(true);
this.privateKey = loadPrivateKey(privateKeyByte);
} catch (Exception exp) {
throw new FrostFSException(exp.getMessage());
}
}
}

View file

@ -1,7 +1,7 @@
package info.frostfs.sdk.jdo.parameters.ape;
import info.frostfs.sdk.annotations.NotNull;
import info.frostfs.sdk.dto.chain.Chain;
import info.frostfs.sdk.dto.ape.Chain;
import info.frostfs.sdk.dto.chain.ChainTarget;
import lombok.AllArgsConstructor;
import lombok.Builder;

View file

@ -1,7 +1,6 @@
package info.frostfs.sdk.jdo.parameters.ape;
import info.frostfs.sdk.annotations.NotNull;
import info.frostfs.sdk.dto.chain.Chain;
import info.frostfs.sdk.dto.chain.ChainTarget;
import lombok.AllArgsConstructor;
import lombok.Builder;
@ -14,14 +13,15 @@ import java.util.Map;
@AllArgsConstructor
public class PrmApeChainRemove {
@NotNull
private Chain chain;
private byte[] chainId;
@NotNull
private ChainTarget chainTarget;
private Map<String, String> xHeaders;
public PrmApeChainRemove(Chain chain, ChainTarget chainTarget) {
this.chain = chain;
public PrmApeChainRemove(byte[] chainId, ChainTarget chainTarget) {
this.chainId = chainId;
this.chainTarget = chainTarget;
}
}

View file

@ -1,7 +1,7 @@
package info.frostfs.sdk.pool;
import frostfs.refs.Types;
import info.frostfs.sdk.dto.chain.Chain;
import info.frostfs.sdk.dto.ape.Chain;
import info.frostfs.sdk.dto.container.Container;
import info.frostfs.sdk.dto.container.ContainerId;
import info.frostfs.sdk.dto.netmap.NetmapSnapshot;

View file

@ -1,6 +1,6 @@
package info.frostfs.sdk.services;
import info.frostfs.sdk.dto.chain.Chain;
import info.frostfs.sdk.dto.ape.Chain;
import info.frostfs.sdk.jdo.parameters.CallContext;
import info.frostfs.sdk.jdo.parameters.ape.PrmApeChainAdd;
import info.frostfs.sdk.jdo.parameters.ape.PrmApeChainList;

View file

@ -4,21 +4,23 @@ import com.google.protobuf.ByteString;
import frostfs.ape.Types;
import frostfs.apemanager.APEManagerServiceGrpc;
import frostfs.apemanager.Service;
import info.frostfs.sdk.dto.chain.Chain;
import info.frostfs.sdk.dto.ape.Chain;
import info.frostfs.sdk.jdo.ClientEnvironment;
import info.frostfs.sdk.jdo.parameters.CallContext;
import info.frostfs.sdk.jdo.parameters.ape.PrmApeChainAdd;
import info.frostfs.sdk.jdo.parameters.ape.PrmApeChainList;
import info.frostfs.sdk.jdo.parameters.ape.PrmApeChainRemove;
import info.frostfs.sdk.mappers.chain.ChainMapper;
import info.frostfs.sdk.mappers.chain.ChainTargetMapper;
import info.frostfs.sdk.services.ApeManagerClient;
import info.frostfs.sdk.services.ContextAccessor;
import info.frostfs.sdk.tools.RequestConstructor;
import info.frostfs.sdk.tools.RequestSigner;
import info.frostfs.sdk.tools.Verifier;
import info.frostfs.sdk.tools.ape.RuleDeserializer;
import info.frostfs.sdk.tools.ape.RuleSerializer;
import java.util.List;
import java.util.stream.Collectors;
import static info.frostfs.sdk.utils.DeadLineUtil.deadLineAfter;
import static info.frostfs.sdk.utils.Validator.validate;
@ -68,12 +70,16 @@ public class ApeManagerClientImpl extends ContextAccessor implements ApeManagerC
Verifier.checkResponse(response);
return ChainMapper.toModels(response.getBody().getChainsList());
return response.getBody().getChainsList().stream()
.map(chain -> RuleDeserializer.deserialize(chain.getRaw().toByteArray()))
.collect(Collectors.toList());
}
private Service.AddChainRequest createAddChainRequest(PrmApeChainAdd args) {
var raw = RuleSerializer.serialize(args.getChain());
var chainGrpc = Types.Chain.newBuilder()
.setRaw(ByteString.copyFrom(args.getChain().getRaw()))
.setRaw(ByteString.copyFrom(raw))
.build();
var body = Service.AddChainRequest.Body.newBuilder()
.setChain(chainGrpc)
@ -90,7 +96,7 @@ public class ApeManagerClientImpl extends ContextAccessor implements ApeManagerC
private Service.RemoveChainRequest createRemoveChainRequest(PrmApeChainRemove args) {
var body = Service.RemoveChainRequest.Body.newBuilder()
.setChainId(ByteString.copyFrom(args.getChain().getRaw()))
.setChainId(ByteString.copyFrom(args.getChainId()))
.setTarget(ChainTargetMapper.toGrpcMessage(args.getChainTarget()))
.build();
var request = Service.RemoveChainRequest.newBuilder()

View file

@ -186,11 +186,15 @@ public class ContainerClientImpl extends ContextAccessor implements ContainerCli
private Service.PutRequest createPutRequest(PrmContainerCreate args, CallContext ctx) {
syncContainerWithNetwork(args.getContainer(), ctx);
var container = ContainerMapper.toGrpcMessage(args.getContainer()).toBuilder()
.setOwnerId(OwnerIdMapper.toGrpcMessage(getContext().getOwnerId()))
.setVersion(VersionMapper.toGrpcMessage(getContext().getVersion()))
.build();
var builder = ContainerMapper.toGrpcMessage(args.getContainer());
if (!builder.hasOwnerId()) {
builder.setOwnerId(OwnerIdMapper.toGrpcMessage(getContext().getOwnerId()));
}
if (!builder.hasVersion()) {
builder.setVersion(VersionMapper.toGrpcMessage(getContext().getVersion()));
}
var container = builder.build();
var body = Service.PutRequest.Body.newBuilder()
.setContainer(container)
.setSignature(RequestSigner.signRFC6979(getContext().getKey(), container))

View file

@ -35,7 +35,6 @@ import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import static info.frostfs.sdk.Helper.getSha256;
import static info.frostfs.sdk.constants.ErrorConst.PROTO_MESSAGE_IS_EMPTY_TEMPLATE;
import static info.frostfs.sdk.tools.RequestSigner.sign;
import static info.frostfs.sdk.utils.DeadLineUtil.deadLineAfter;
@ -166,8 +165,10 @@ public class ObjectClientImpl extends ContextAccessor implements ObjectClient {
// send the last part and create linkObject
if (CollectionUtils.isNotEmpty(sentObjectIds)) {
var largeObjectHeader =
new ObjectHeader(header.getContainerId(), ObjectType.REGULAR, attributes, fullLength, null);
var largeObjectHeader = new ObjectHeader(
header.getContainerId(), ObjectType.REGULAR, attributes, fullLength, header.getVersion()
);
largeObjectHeader.setOwnerId(header.getOwnerId());
split.setParentHeader(largeObjectHeader);
@ -333,6 +334,7 @@ public class ObjectClientImpl extends ContextAccessor implements ObjectClient {
.setBody(body)
.clearVerifyHeader();
RequestConstructor.addMetaHeader(chunkRequest, args.getXHeaders());
sign(chunkRequest, getContext().getKey());
stream.write(chunkRequest.build());
@ -357,9 +359,7 @@ public class ObjectClientImpl extends ContextAccessor implements ObjectClient {
grpcHeader = objectToolsImpl.updateSplitValues(grpcHeader, header.getSplit());
}
var oid = Types.ObjectID.newBuilder().setValue(getSha256(grpcHeader)).build();
var initRequest = createInitPutRequest(oid, grpcHeader, args, ctx);
var initRequest = createInitPutRequest(grpcHeader, args, ctx);
return putObjectInit(initRequest, ctx);
}
@ -525,13 +525,11 @@ public class ObjectClientImpl extends ContextAccessor implements ObjectClient {
return request.build();
}
private Service.PutRequest createInitPutRequest(Types.ObjectID oid,
frostfs.object.Types.Header header,
private Service.PutRequest createInitPutRequest(frostfs.object.Types.Header header,
PrmObjectPutBase args,
CallContext ctx) {
var address = Types.Address.newBuilder()
.setContainerId(header.getContainerId())
.setObjectId(oid)
.build();
var init = Service.PutRequest.Body.Init.newBuilder()
.setHeader(header)
@ -561,7 +559,6 @@ public class ObjectClientImpl extends ContextAccessor implements ObjectClient {
CallContext ctx) {
var address = Types.Address.newBuilder()
.setContainerId(grpcObject.getHeader().getContainerId())
.setObjectId(grpcObject.getObjectId())
.build();
var body = Service.PutSingleRequest.Body.newBuilder()
.setObject(grpcObject)

View file

@ -105,7 +105,10 @@ public class ObjectToolsImpl extends ContextAccessor implements ToolsClient {
split.setParent(ObjectIdMapper.toModel(parentObjectId));
}
grpcSplit.setPrevious(ObjectIdMapper.toGrpcMessage(split.getPrevious())).build();
if (nonNull(split.getPrevious())) {
grpcSplit.setPrevious(ObjectIdMapper.toGrpcMessage(split.getPrevious())).build();
}
return grpcHeader.toBuilder().setSplit(grpcSplit.build()).build();
}

View file

@ -5,6 +5,7 @@ import frostfs.object.Service;
import info.frostfs.sdk.dto.object.ObjectId;
import info.frostfs.sdk.jdo.ClientEnvironment;
import info.frostfs.sdk.jdo.parameters.object.PrmObjectPutBase;
import info.frostfs.sdk.tools.RequestConstructor;
import info.frostfs.sdk.tools.Verifier;
import lombok.AllArgsConstructor;
import lombok.Getter;
@ -27,6 +28,7 @@ public class ObjectWriter {
.setBody(body)
.clearVerifyHeader();
RequestConstructor.addMetaHeader(chunkRequest, args.getXHeaders());
sign(chunkRequest, environment.getKey());
streamer.write(chunkRequest.build());

View file

@ -86,7 +86,6 @@ public class RequestConstructor {
.build();
var body = protoToken.getBody().toBuilder()
.setObject(ctx)
.setSessionKey(ByteString.copyFrom(key.getPublicKeyByte()))
.build();
return protoToken.toBuilder()

View file

@ -78,6 +78,10 @@ public class Verifier {
}
var metaHeader = (Types.ResponseMetaHeader) MessageHelper.getField(response, META_HEADER_FIELD_NAME);
if (isNull(metaHeader) || metaHeader.getSerializedSize() == 0) {
return;
}
var status = ResponseStatusMapper.toModel(metaHeader.getStatus());
if (!status.isSuccess()) {
throw new ResponseFrostFSException(status);

View file

@ -0,0 +1,5 @@
package info.frostfs.sdk.tools.ape;
public interface MarshalFunction<T> {
int marshal(byte[] buf, int offset, T t);
}

View file

@ -0,0 +1,198 @@
package info.frostfs.sdk.tools.ape;
import info.frostfs.sdk.dto.ape.*;
import info.frostfs.sdk.enums.ConditionKindType;
import info.frostfs.sdk.enums.ConditionType;
import info.frostfs.sdk.enums.RuleMatchType;
import info.frostfs.sdk.enums.RuleStatus;
import info.frostfs.sdk.exceptions.FrostFSException;
import info.frostfs.sdk.exceptions.ValidationFrostFSException;
import org.apache.commons.lang3.ArrayUtils;
import java.lang.reflect.Array;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.atomic.AtomicInteger;
import static info.frostfs.sdk.constants.ErrorConst.*;
import static info.frostfs.sdk.constants.FieldConst.EMPTY_STRING;
import static info.frostfs.sdk.constants.RuleConst.*;
public class RuleDeserializer {
private RuleDeserializer() {
}
public static Chain deserialize(byte[] data) {
if (ArrayUtils.isEmpty(data)) {
throw new ValidationFrostFSException(INPUT_PARAM_IS_MISSING);
}
AtomicInteger offset = new AtomicInteger(0);
Chain chain = new Chain();
var version = uInt8Unmarshal(data, offset);
if (version != VERSION) {
throw new FrostFSException(String.format(UNSUPPORTED_MARSHALLER_VERSION_TEMPLATE, version));
}
var chainVersion = uInt8Unmarshal(data, offset);
if (chainVersion != CHAIN_MARSHAL_VERSION) {
throw new FrostFSException(String.format(UNSUPPORTED_CHAIN_VERSION_TEMPLATE, chainVersion));
}
chain.setId(sliceUnmarshal(data, offset, Byte.class, RuleDeserializer::uInt8Unmarshal));
chain.setRules(sliceUnmarshal(data, offset, Rule.class, RuleDeserializer::unmarshalRule));
chain.setMatchType(RuleMatchType.get(uInt8Unmarshal(data, offset)));
verifyUnmarshal(data, offset);
return chain;
}
private static Byte uInt8Unmarshal(byte[] buf, AtomicInteger offset) {
if (buf.length - offset.get() < 1) {
throw new FrostFSException(
String.format(BYTES_ARE_OVER_FOR_DESERIALIZE_TEMPLATE, Byte.class.getName(), offset.get()));
}
return buf[offset.getAndAdd(1)];
}
public static long varInt(byte[] buf, AtomicInteger offset) {
long ux = uVarInt(buf, offset); // ok to continue in presence of error
long x = ux >> 1;
if ((ux & 1) != 0) {
x = ~x;
}
return x;
}
public static long uVarInt(byte[] buf, AtomicInteger offset) {
long x = 0;
int s = 0;
for (int i = offset.get(); i < buf.length; i++) {
long b = buf[i];
if (i == MAX_VAR_INT_LENGTH) {
offset.set(-(i + 1));
return 0; // overflow
}
if (b >= 0) {
if (i == MAX_VAR_INT_LENGTH - 1 && b > 1) {
offset.set(-(i + 1));
return 0; // overflow
}
offset.set(i + 1);
return x | (b << s);
}
x |= (b & OFFSET127) << s;
s += UNSIGNED_SERIALIZE_SIZE;
}
offset.set(0);
return 0;
}
private static <T> T[] sliceUnmarshal(byte[] buf,
AtomicInteger offset,
Class<T> clazz,
UnmarshalFunction<T> unmarshalT) {
var size = (int) varInt(buf, offset);
if (size == NULL_SLICE) {
return null;
}
if (size > MAX_SLICE_LENGTH) {
throw new ValidationFrostFSException(String.format(SLICE_IS_TOO_BIG_TEMPLATE, size));
}
if (size < 0) {
throw new ValidationFrostFSException(String.format(SLICE_SIZE_IS_INVALID_TEMPLATE, size));
}
T[] result = (T[]) Array.newInstance(clazz, size);
for (int i = 0; i < result.length; i++) {
result[i] = unmarshalT.unmarshal(buf, offset);
}
return result;
}
private static boolean boolUnmarshal(byte[] buf, AtomicInteger offset) {
return uInt8Unmarshal(buf, offset) == BYTE_TRUE;
}
private static long int64Unmarshal(byte[] buf, AtomicInteger offset) {
if (buf.length - offset.get() < Long.BYTES) {
throw new ValidationFrostFSException(
String.format(BYTES_ARE_OVER_FOR_DESERIALIZE_TEMPLATE, Long.class.getName(), offset.get())
);
}
return varInt(buf, offset);
}
private static String stringUnmarshal(byte[] buf, AtomicInteger offset) {
int size = (int) int64Unmarshal(buf, offset);
if (size == 0) {
return EMPTY_STRING;
}
if (size > MAX_SLICE_LENGTH) {
throw new ValidationFrostFSException(String.format(STRING_IS_TOO_BIG_TEMPLATE, size));
}
if (size < 0) {
throw new ValidationFrostFSException(String.format(STRING_SIZE_IS_INVALID_TEMPLATE, size));
}
if (buf.length - offset.get() < size) {
throw new ValidationFrostFSException(
String.format(BYTES_ARE_OVER_FOR_DESERIALIZE_TEMPLATE, String.class.getName(), offset.get())
);
}
return new String(buf, offset.getAndAdd(size), size, StandardCharsets.UTF_8);
}
private static Actions unmarshalActions(byte[] buf, AtomicInteger offset) {
Actions actions = new Actions();
actions.setInverted(boolUnmarshal(buf, offset));
actions.setNames(sliceUnmarshal(buf, offset, String.class, RuleDeserializer::stringUnmarshal));
return actions;
}
private static Condition unmarshalCondition(byte[] buf, AtomicInteger offset) {
Condition condition = new Condition();
condition.setOp(ConditionType.get(uInt8Unmarshal(buf, offset)));
condition.setKind(ConditionKindType.get(uInt8Unmarshal(buf, offset)));
condition.setKey(stringUnmarshal(buf, offset));
condition.setValue(stringUnmarshal(buf, offset));
return condition;
}
private static Rule unmarshalRule(byte[] buf, AtomicInteger offset) {
Rule rule = new Rule();
rule.setStatus(RuleStatus.get(uInt8Unmarshal(buf, offset)));
rule.setActions(unmarshalActions(buf, offset));
rule.setResources(unmarshalResources(buf, offset));
rule.setAny(boolUnmarshal(buf, offset));
rule.setConditions(sliceUnmarshal(buf, offset, Condition.class, RuleDeserializer::unmarshalCondition));
return rule;
}
private static Resources unmarshalResources(byte[] buf, AtomicInteger offset) {
Resources resources = new Resources();
resources.setInverted(boolUnmarshal(buf, offset));
resources.setNames(sliceUnmarshal(buf, offset, String.class, RuleDeserializer::stringUnmarshal));
return resources;
}
private static void verifyUnmarshal(byte[] buf, AtomicInteger offset) {
if (buf.length != offset.get()) {
throw new ValidationFrostFSException(UNMARSHAL_SIZE_DIFFERS);
}
}
}

View file

@ -0,0 +1,260 @@
package info.frostfs.sdk.tools.ape;
import info.frostfs.sdk.dto.ape.*;
import info.frostfs.sdk.exceptions.ValidationFrostFSException;
import org.apache.commons.lang3.StringUtils;
import java.nio.charset.StandardCharsets;
import java.util.function.Function;
import static info.frostfs.sdk.constants.ErrorConst.*;
import static info.frostfs.sdk.constants.RuleConst.*;
import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;
public class RuleSerializer {
private RuleSerializer() {
}
public static byte[] serialize(Chain chain) {
if (isNull(chain)) {
throw new ValidationFrostFSException(String.format(INPUT_PARAM_IS_MISSING_TEMPLATE, Chain.class.getName()));
}
int s = U_INT_8_SIZE // Marshaller version
+ U_INT_8_SIZE // Chain version
+ sliceSize(chain.getId(), b -> BYTE_SIZE)
+ sliceSize(chain.getRules(), RuleSerializer::ruleSize)
+ U_INT_8_SIZE; // MatchType
byte[] buf = new byte[s];
int offset = uInt8Marshal(buf, 0, VERSION);
offset = uInt8Marshal(buf, offset, (byte) CHAIN_MARSHAL_VERSION);
offset = sliceMarshal(buf, offset, chain.getId(), RuleSerializer::byteMarshal);
offset = sliceMarshal(buf, offset, chain.getRules(), RuleSerializer::marshalRule);
offset = uInt8Marshal(buf, offset, (byte) chain.getMatchType().value);
verifyMarshal(buf, offset);
return buf;
}
private static <T> int sliceSize(T[] slice, Function<T, Integer> sizeOf) {
if (isNull(slice)) {
return NULL_SLICE_SIZE;
}
// Assuming int64Size is the size of the slice
int size = int64Size(slice.length);
for (T v : slice) {
size += sizeOf.apply(v);
}
return size;
}
/*
* https://cs.opensource.google/go/go/+/master:src/encoding/binary/varint.go;l=92;drc=dac9b9ddbd5160c5f4552410f5f828
* 1bd5eed38c
*
* and
*
* https://cs.opensource.google/go/go/+/master:src/encoding/binary/varint.go;l=41;drc=dac9b9ddbd5160c5f4552410f5f828
* 1bd5eed38c
* */
private static int int64Size(long value) {
long ux = value << 1;
if (value < 0) {
ux = ~ux;
}
int size = 0;
while (ux >= OFFSET128) {
size++;
ux >>>= UNSIGNED_SERIALIZE_SIZE;
}
return size + 1;
}
private static int stringSize(String s) {
int len = nonNull(s) ? s.length() : 0;
return int64Size(len) + len;
}
private static int actionsSize(Actions action) {
return BOOL_SIZE // Inverted
+ (nonNull(action) ? sliceSize(action.getNames(), RuleSerializer::stringSize) : 0);
}
private static int resourcesSize(Resources resource) {
return BOOL_SIZE // Inverted
+ (nonNull(resource) ? sliceSize(resource.getNames(), RuleSerializer::stringSize) : 0);
}
private static int conditionSize(Condition condition) {
if (isNull(condition)) {
throw new ValidationFrostFSException(String.format(REQUIRED_FIELD_TEMPLATE, Rule.class.getName()));
}
return BYTE_SIZE // Op
+ BYTE_SIZE // Object
+ stringSize(condition.getKey())
+ stringSize(condition.getValue());
}
private static int ruleSize(Rule rule) {
if (isNull(rule)) {
throw new ValidationFrostFSException(String.format(REQUIRED_FIELD_TEMPLATE, Rule.class.getName()));
}
return BYTE_SIZE // Status
+ actionsSize(rule.getActions())
+ resourcesSize(rule.getResources())
+ BOOL_SIZE // Any
+ sliceSize(rule.getConditions(), RuleSerializer::conditionSize);
}
private static int uInt8Marshal(byte[] buf, int offset, byte value) {
if (buf.length - offset < 1) {
throw new ValidationFrostFSException(
String.format(BYTES_ARE_OVER_FOR_SERIALIZE_TEMPLATE, Byte.class.getName(), 1)
);
}
buf[offset] = value;
return offset + 1;
}
private static int byteMarshal(byte[] buf, int offset, byte value) {
return uInt8Marshal(buf, offset, value);
}
// putVarInt encodes an int64 into buf and returns the number of bytes written.
private static int putVarInt(byte[] buf, int offset, long x) {
long ux = x << 1;
if (x < 0) {
ux = ~ux;
}
return putUVarInt(buf, offset, ux);
}
private static int putUVarInt(byte[] buf, int offset, long x) {
while (x >= OFFSET128) {
buf[offset] = (byte) (x | OFFSET128);
x >>>= UNSIGNED_SERIALIZE_SIZE;
offset++;
}
buf[offset] = (byte) x;
return offset + 1;
}
private static int int64Marshal(byte[] buf, int offset, long v) {
var size = int64Size(v);
if (buf.length - offset < size) {
throw new ValidationFrostFSException(
String.format(BYTES_ARE_OVER_FOR_SERIALIZE_TEMPLATE, Long.class.getName(), size)
);
}
return putVarInt(buf, offset, v);
}
private static <T> int sliceMarshal(byte[] buf, int offset, T[] slice, MarshalFunction<T> marshalT) {
if (isNull(slice)) {
return int64Marshal(buf, offset, NULL_SLICE);
}
if (slice.length > MAX_SLICE_LENGTH) {
throw new ValidationFrostFSException(String.format(SLICE_IS_TOO_BIG_TEMPLATE, slice.length));
}
offset = int64Marshal(buf, offset, slice.length);
for (T v : slice) {
offset = marshalT.marshal(buf, offset, v);
}
return offset;
}
private static int boolMarshal(byte[] buf, int offset, boolean value) {
return uInt8Marshal(buf, offset, value ? BYTE_TRUE : BYTE_FALSE);
}
private static int stringMarshal(byte[] buf, int offset, String value) {
if (StringUtils.isBlank(value)) {
throw new ValidationFrostFSException(STRING_IS_BLANK);
}
if (value.length() > MAX_SLICE_LENGTH) {
throw new ValidationFrostFSException(String.format(STRING_IS_TOO_BIG_TEMPLATE, value.length()));
}
if (buf.length - offset < int64Size(value.length()) + value.length()) {
throw new ValidationFrostFSException(
String.format(BYTES_ARE_OVER_FOR_SERIALIZE_TEMPLATE, String.class.getName(), value.length())
);
}
offset = int64Marshal(buf, offset, value.length());
if (value.isEmpty()) {
return offset;
}
byte[] stringBytes = value.getBytes(StandardCharsets.UTF_8);
// Copy exactly value.length() bytes as in the original code
System.arraycopy(stringBytes, 0, buf, offset, value.length());
return offset + value.length();
}
private static int marshalActions(byte[] buf, int offset, Actions action) {
if (isNull(action)) {
throw new ValidationFrostFSException(String.format(REQUIRED_FIELD_TEMPLATE, Actions.class.getName()));
}
offset = boolMarshal(buf, offset, action.isInverted());
return sliceMarshal(buf, offset, action.getNames(), RuleSerializer::stringMarshal);
}
private static int marshalCondition(byte[] buf, int offset, Condition condition) {
if (isNull(condition)) {
throw new ValidationFrostFSException(String.format(REQUIRED_FIELD_TEMPLATE, Condition.class.getName()));
}
offset = byteMarshal(buf, offset, (byte) condition.getOp().value);
offset = byteMarshal(buf, offset, (byte) condition.getKind().value);
offset = stringMarshal(buf, offset, condition.getKey());
return stringMarshal(buf, offset, condition.getValue());
}
private static int marshalRule(byte[] buf, int offset, Rule rule) {
if (isNull(rule)) {
throw new ValidationFrostFSException(String.format(REQUIRED_FIELD_TEMPLATE, Rule.class.getName()));
}
offset = byteMarshal(buf, offset, (byte) rule.getStatus().value);
offset = marshalActions(buf, offset, rule.getActions());
offset = marshalResources(buf, offset, rule.getResources());
offset = boolMarshal(buf, offset, rule.isAny());
return sliceMarshal(buf, offset, rule.getConditions(), RuleSerializer::marshalCondition);
}
private static int marshalResources(byte[] buf, int offset, Resources resources) {
if (isNull(resources)) {
throw new ValidationFrostFSException(String.format(REQUIRED_FIELD_TEMPLATE, Resources.class.getName()));
}
offset = boolMarshal(buf, offset, resources.isInverted());
return sliceMarshal(buf, offset, resources.getNames(), RuleSerializer::stringMarshal);
}
private static void verifyMarshal(byte[] buf, int lastOffset) {
if (buf.length != lastOffset) {
throw new ValidationFrostFSException(MARSHAL_SIZE_DIFFERS);
}
}
}

View file

@ -0,0 +1,7 @@
package info.frostfs.sdk.tools.ape;
import java.util.concurrent.atomic.AtomicInteger;
public interface UnmarshalFunction<T> {
T unmarshal(byte[] buf, AtomicInteger offset);
}

View file

@ -1,9 +1,6 @@
package info.frostfs.sdk.utils;
import info.frostfs.sdk.annotations.AtLeastOneIsFilled;
import info.frostfs.sdk.annotations.NotBlank;
import info.frostfs.sdk.annotations.NotNull;
import info.frostfs.sdk.annotations.Validate;
import info.frostfs.sdk.annotations.*;
import info.frostfs.sdk.constants.ErrorConst;
import info.frostfs.sdk.exceptions.ProcessFrostFSException;
import info.frostfs.sdk.exceptions.ValidationFrostFSException;
@ -37,6 +34,10 @@ public class Validator {
Class<?> clazz = object.getClass();
if (clazz.isAnnotationPresent(ComplexAtLeastOneIsFilled.class)) {
processComplexAtLeastOneIsFilled(object, clazz, errorMessage);
}
if (clazz.isAnnotationPresent(AtLeastOneIsFilled.class)) {
processAtLeastOneIsFilled(object, clazz, errorMessage);
}
@ -83,8 +84,22 @@ public class Validator {
process(getFieldValue(object, field), errorMessage);
}
private static <T> void processComplexAtLeastOneIsFilled(T object, Class<?> clazz, StringBuilder errorMessage) {
var annotation = clazz.getAnnotation(ComplexAtLeastOneIsFilled.class);
for (AtLeastOneIsFilled value : annotation.value()) {
processAtLeastOneIsFilled(object, clazz, errorMessage, value);
}
}
private static <T> void processAtLeastOneIsFilled(T object, Class<?> clazz, StringBuilder errorMessage) {
var annotation = clazz.getAnnotation(AtLeastOneIsFilled.class);
processAtLeastOneIsFilled(object, clazz, errorMessage, annotation);
}
private static <T> void processAtLeastOneIsFilled(T object,
Class<?> clazz,
StringBuilder errorMessage,
AtLeastOneIsFilled annotation) {
var emptyFieldsCount = 0;
for (String fieldName : annotation.fields()) {
var field = getField(clazz, fieldName);
@ -106,6 +121,7 @@ public class Validator {
}
}
private static <T> Object getFieldValue(T object, Field field) {
try {
return field.get(object);

View file

@ -1,31 +0,0 @@
package info.frostfs.sdk;
import com.google.common.io.ByteStreams;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import java.io.InputStream;
import static java.util.Objects.isNull;
public class FileUtils {
@SneakyThrows
public static byte[] resourceToBytes(String resourcePath) {
if (StringUtils.isBlank(resourcePath)) {
throw new IllegalArgumentException("Blank filename!");
}
ClassLoader loader = FileUtils.class.getClassLoader();
if (isNull(loader)) {
throw new RuntimeException("Class loader is null!");
}
InputStream certStream = loader.getResourceAsStream(resourcePath);
if (isNull(certStream)) {
throw new RuntimeException("Resource could not be found!");
}
return ByteStreams.toByteArray(certStream);
}
}

View file

@ -2,7 +2,6 @@ package info.frostfs.sdk.services;
import frostfs.accounting.AccountingServiceGrpc;
import frostfs.accounting.Service;
import info.frostfs.sdk.Base58;
import info.frostfs.sdk.dto.object.OwnerId;
import info.frostfs.sdk.jdo.ClientEnvironment;
import info.frostfs.sdk.jdo.parameters.CallContext;
@ -12,6 +11,7 @@ import info.frostfs.sdk.tools.RequestConstructor;
import info.frostfs.sdk.tools.RequestSigner;
import info.frostfs.sdk.tools.Verifier;
import io.grpc.Channel;
import io.neow3j.crypto.Base58;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

View file

@ -2,10 +2,9 @@ package info.frostfs.sdk.services;
import frostfs.apemanager.APEManagerServiceGrpc;
import frostfs.apemanager.Service;
import info.frostfs.sdk.FileUtils;
import info.frostfs.sdk.dto.chain.Chain;
import info.frostfs.sdk.dto.ape.*;
import info.frostfs.sdk.dto.chain.ChainTarget;
import info.frostfs.sdk.enums.TargetType;
import info.frostfs.sdk.enums.*;
import info.frostfs.sdk.exceptions.ValidationFrostFSException;
import info.frostfs.sdk.jdo.ClientEnvironment;
import info.frostfs.sdk.jdo.parameters.CallContext;
@ -18,6 +17,7 @@ import info.frostfs.sdk.tools.RequestConstructor;
import info.frostfs.sdk.tools.RequestSigner;
import info.frostfs.sdk.tools.Verifier;
import io.grpc.Channel;
import org.apache.commons.lang3.ArrayUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@ -30,7 +30,8 @@ import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import java.lang.reflect.Field;
import java.util.stream.Collectors;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
@ -39,6 +40,9 @@ import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
class ApeManagerClientTest {
private static final String CHAIN_BASE64 =
"AAAaY2hhaW4taWQtdGVzdAIAAAISR2V0T2JqZWN0AAIebmF0aXZlOm9iamVjdC8qAAIAABREZXBhcnRtZW50BEhSAA==";
private ApeManagerClientImpl apeManagerClient;
@Mock
@ -107,7 +111,9 @@ class ApeManagerClientTest {
assertThat(result).containsOnly(response.getBody().getChainId().toByteArray());
var request = captor.getValue();
assertThat(request.getBody().getChain().getRaw().toByteArray()).containsOnly(chain.getRaw());
assertEquals(
Base64.getEncoder().encodeToString(request.getBody().getChain().getRaw().toByteArray()), CHAIN_BASE64)
;
assertEquals(chainTarget.getName(), request.getBody().getTarget().getName());
assertEquals(chainTarget.getType().value, request.getBody().getTarget().getType().getNumber());
}
@ -133,7 +139,7 @@ class ApeManagerClientTest {
//Given
Chain chain = generateChain();
ChainTarget chainTarget = generateChainTarget();
PrmApeChainRemove params = new PrmApeChainRemove(chain, chainTarget);
PrmApeChainRemove params = new PrmApeChainRemove(Base64.getDecoder().decode(CHAIN_BASE64), chainTarget);
var response = ApeManagerGenerator.generateRemoveChainResponse();
@ -156,7 +162,7 @@ class ApeManagerClientTest {
verifierMock.verify(() -> Verifier.checkResponse(response), times(1));
var request = captor.getValue();
assertThat(request.getBody().getChainId().toByteArray()).containsOnly(chain.getRaw());
assertThat(request.getBody().getChainId().toByteArray()).containsOnly(Base64.getDecoder().decode(CHAIN_BASE64));
assertEquals(chainTarget.getName(), request.getBody().getTarget().getName());
assertEquals(chainTarget.getType().value, request.getBody().getTarget().getType().getNumber());
}
@ -167,7 +173,7 @@ class ApeManagerClientTest {
Chain chain = generateChain();
ChainTarget chainTarget = generateChainTarget();
PrmApeChainRemove params1 = new PrmApeChainRemove(null, chainTarget);
PrmApeChainRemove params2 = new PrmApeChainRemove(chain, null);
PrmApeChainRemove params2 = new PrmApeChainRemove(Base64.getDecoder().decode(CHAIN_BASE64), null);
PrmApeChainRemove params3 = new PrmApeChainRemove(null, null);
//When + Then
@ -202,11 +208,7 @@ class ApeManagerClientTest {
);
verifierMock.verify(() -> Verifier.checkResponse(response), times(1));
var actual = result.stream().map(Chain::getRaw).collect(Collectors.toList());
var expected = response.getBody().getChainsList().stream()
.map(chain -> chain.getRaw().toByteArray())
.collect(Collectors.toList());
assertThat(actual).hasSize(10).containsAll(expected);
assertThat(result).hasSize(10);
var request = captor.getValue();
assertEquals(chainTarget.getName(), request.getBody().getTarget().getName());
@ -221,8 +223,24 @@ class ApeManagerClientTest {
}
private Chain generateChain() {
byte[] chainRaw = FileUtils.resourceToBytes("test_chain_raw.json");
return new Chain(chainRaw);
var resources = new Resources(false, new String[]{"native:object/*"});
var actions = new Actions(false, new String[]{"GetObject"});
var condition = new Condition(
ConditionType.COND_STRING_EQUALS, ConditionKindType.RESOURCE, "Department", "HR"
);
var rule = new Rule();
rule.setStatus(RuleStatus.ALLOW);
rule.setResources(resources);
rule.setActions(actions);
rule.setAny(false);
rule.setConditions(new Condition[]{condition});
var chain = new Chain();
chain.setId(ArrayUtils.toObject("chain-id-test".getBytes(StandardCharsets.UTF_8)));
chain.setRules(new Rule[]{rule});
chain.setMatchType(RuleMatchType.DENY_PRIORITY);
return chain;
}
private ChainTarget generateChainTarget() {

View file

@ -3,22 +3,21 @@ package info.frostfs.sdk.testgenerator;
import com.google.protobuf.ByteString;
import frostfs.ape.Types;
import frostfs.apemanager.Service;
import info.frostfs.sdk.FileUtils;
import info.frostfs.sdk.Helper;
import org.bouncycastle.util.encoders.Base64;
import java.util.ArrayList;
public class ApeManagerGenerator {
private static final String BASE64_CHAIN = "AAAaY2hhaW4taWQtdGVzdAIAAAICKgACHm5hdGl2ZTpvYmplY3QvKgAAAA==";
private static ByteString generateChainID() {
return ByteString.copyFrom(Helper.getByteArrayFromHex("616c6c6f774f626a476574436e72"));
}
private static Types.Chain generateRawChain() {
byte[] chainRaw = FileUtils.resourceToBytes("test_chain_raw.json");
return Types.Chain.newBuilder()
.setRaw(ByteString.copyFrom(chainRaw))
.setRaw(ByteString.copyFrom(Base64.decode(BASE64_CHAIN)))
.build();
}

View file

@ -0,0 +1,179 @@
package info.frostfs.sdk.tools.ape;
import info.frostfs.sdk.dto.ape.*;
import info.frostfs.sdk.enums.ConditionKindType;
import info.frostfs.sdk.enums.ConditionType;
import info.frostfs.sdk.enums.RuleMatchType;
import info.frostfs.sdk.enums.RuleStatus;
import info.frostfs.sdk.exceptions.FrostFSException;
import info.frostfs.sdk.exceptions.ValidationFrostFSException;
import org.apache.commons.lang3.ArrayUtils;
import org.bouncycastle.util.encoders.Base64;
import org.junit.jupiter.api.Test;
import java.nio.charset.StandardCharsets;
import static java.util.Objects.isNull;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;
public class ApeRuleTest {
@Test
void apeRuleTest() {
//Given
var resources = new Resources(false, new String[]{"native:object/*"});
var actions = new Actions(false, new String[]{"*"});
var rule = new Rule();
rule.setStatus(RuleStatus.ALLOW);
rule.setResources(resources);
rule.setActions(actions);
rule.setAny(false);
rule.setConditions(new Condition[]{});
var chain = new Chain();
chain.setId(ArrayUtils.toObject("chain-id-test".getBytes(StandardCharsets.UTF_8)));
chain.setRules(new Rule[]{rule});
chain.setMatchType(RuleMatchType.DENY_PRIORITY);
//When
var serialized = RuleSerializer.serialize(chain);
var t = Base64.encode(serialized);
var restoredChain = RuleDeserializer.deserialize(serialized);
//Then
assertThat(restoredChain.getId()).isNotEmpty().containsOnly(chain.getId());
assertEquals(chain.getMatchType(), restoredChain.getMatchType());
compareRules(chain.getRules(), restoredChain.getRules());
}
@Test
void apeRuleTest2() {
//Given
var resources = new Resources(true, new String[]{"native:object/*", "U.S.S. ENTERPRISE"});
var actions = new Actions(true, new String[]{"put", "get"});
var cond1 = new Condition(
ConditionType.COND_STRING_EQUALS, ConditionKindType.RESOURCE, "key1", "value1"
);
var cond2 = new Condition(
ConditionType.COND_NUMERIC_GREATER_THAN, ConditionKindType.REQUEST, "key2", "value2"
);
var rule = new Rule();
rule.setStatus(RuleStatus.ACCESS_DENIED);
rule.setResources(resources);
rule.setActions(actions);
rule.setAny(true);
rule.setConditions(new Condition[]{cond1, cond2});
var chain = new Chain();
chain.setId(ArrayUtils.toObject("dumptext".getBytes(StandardCharsets.UTF_8)));
chain.setRules(new Rule[]{rule});
chain.setMatchType(RuleMatchType.FIRST_MATCH);
//When
var serialized = RuleSerializer.serialize(chain);
var restoredChain = RuleDeserializer.deserialize(serialized);
//Then
assertThat(restoredChain.getId()).isNotEmpty().containsOnly(chain.getId());
assertEquals(chain.getMatchType(), restoredChain.getMatchType());
compareRules(chain.getRules(), restoredChain.getRules());
}
@Test
void apeRuleTest3() {
//Given
var chain = new Chain();
chain.setMatchType(RuleMatchType.DENY_PRIORITY);
//When
var serialized = RuleSerializer.serialize(chain);
var restoredChain = RuleDeserializer.deserialize(serialized);
//Then
assertNull(restoredChain.getId());
assertEquals(chain.getMatchType(), restoredChain.getMatchType());
assertNull(restoredChain.getRules());
}
@Test
void apeRule_deserialize_wrong() {
//When + Then
assertThrows(ValidationFrostFSException.class, () -> RuleDeserializer.deserialize(null));
assertThrows(ValidationFrostFSException.class, () -> RuleDeserializer.deserialize(new byte[]{}));
assertThrows(FrostFSException.class, () -> RuleDeserializer.deserialize(new byte[]{1, 2, 3}));
assertThrows(ValidationFrostFSException.class, () -> RuleDeserializer.deserialize(new byte[]{
0x00, 0x00, 0x3A, 0x77, 0x73, 0x3A, 0x69, 0x61, 0x6D, 0x3A, 0x3A, 0x6E, 0x61, 0x6D, 0x65, 0x73,
0x70, 0x61, 0x63, 0x65, 0x3A, 0x67, 0x72, 0x6F, 0x75, 0x70, 0x2F, 0x73, 0x6F, (byte) 0x82, (byte) 0x82,
(byte) 0x82, (byte) 0x82, (byte) 0x82, (byte) 0x82, 0x75, (byte) 0x82
}));
}
@Test
void apeRule_serialize_wrong() {
//When + Then
assertThrows(ValidationFrostFSException.class, () -> RuleSerializer.serialize(null));
}
private void compareRules(Rule[] rules1, Rule[] rules2) {
assertThat(rules1).isNotEmpty();
assertThat(rules2).isNotEmpty();
assertEquals(rules1.length, rules2.length);
for (int ri = 0; ri < rules1.length; ri++) {
var rule1 = rules1[ri];
var rule2 = rules2[ri];
assertEquals(rule1.getStatus(), rule2.getStatus());
assertEquals(rule1.isAny(), rule2.isAny());
compareActions(rule1.getActions(), rule2.getActions());
compareResources(rule1.getResources(), rule2.getResources());
compareConditions(rule1.getConditions(), rule2.getConditions());
}
}
private void compareActions(Actions actions1, Actions actions2) {
if (isNull(actions1) && isNull(actions2)) {
return;
}
assertEquals(actions1.isInverted(), actions2.isInverted());
if (ArrayUtils.isEmpty(actions1.getNames()) && ArrayUtils.isEmpty(actions2.getNames())) {
return;
}
assertThat(actions2.getNames()).hasSize(actions1.getNames().length).containsOnly(actions1.getNames());
}
private void compareResources(Resources resources1, Resources resources2) {
if (isNull(resources1) && isNull(resources2)) {
return;
}
assertEquals(resources1.isInverted(), resources2.isInverted());
if (ArrayUtils.isEmpty(resources1.getNames()) && ArrayUtils.isEmpty(resources2.getNames())) {
return;
}
assertThat(resources2.getNames()).hasSize(resources1.getNames().length).containsOnly(resources1.getNames());
}
private void compareConditions(Condition[] conditions1, Condition[] conditions2) {
if (ArrayUtils.isEmpty(conditions1) && ArrayUtils.isEmpty(conditions2)) {
return;
}
assertEquals(conditions1.length, conditions2.length);
for (int i = 0; i < conditions1.length; i++) {
assertEquals(conditions1[i].getOp(), conditions2[i].getOp());
assertEquals(conditions1[i].getKind(), conditions2[i].getKind());
assertEquals(conditions1[i].getKey(), conditions2[i].getKey());
assertEquals(conditions1[i].getValue(), conditions2[i].getValue());
}
}
}

View file

@ -1,30 +0,0 @@
{
"ID": "",
"Rules": [
{
"Status": "Allow",
"Actions": {
"Inverted": false,
"Names": [
"GetObject"
]
},
"Resources": {
"Inverted": false,
"Names": [
"native:object/*"
]
},
"Any": false,
"Condition": [
{
"Op": "StringEquals",
"Object": "Resource",
"Key": "Department",
"Value": "HR"
}
]
}
],
"MatchType": "DenyPriority"
}

View file

@ -1,141 +0,0 @@
package info.frostfs.sdk;
import info.frostfs.sdk.exceptions.ProcessFrostFSException;
import info.frostfs.sdk.exceptions.ValidationFrostFSException;
import org.apache.commons.lang3.StringUtils;
import java.util.Arrays;
import static info.frostfs.sdk.ArrayHelper.concat;
import static info.frostfs.sdk.Helper.getSha256;
import static info.frostfs.sdk.constants.ErrorConst.*;
import static java.util.Objects.isNull;
public class Base58 {
public static final char[] ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz".toCharArray();
public static final int BASE58_SYMBOL_COUNT = 58;
public static final int BASE256_SYMBOL_COUNT = 256;
private static final int BYTE_DIVISION = 0xFF;
private static final char ENCODED_ZERO = ALPHABET[0];
private static final char BASE58_ASCII_MAX_VALUE = 128;
private static final int[] INDEXES = new int[BASE58_ASCII_MAX_VALUE];
static {
Arrays.fill(INDEXES, -1);
for (int i = 0; i < ALPHABET.length; i++) {
INDEXES[ALPHABET[i]] = i;
}
}
private Base58() {
}
public static byte[] base58CheckDecode(String input) {
if (StringUtils.isEmpty(input)) {
throw new ValidationFrostFSException(INPUT_PARAM_IS_MISSING);
}
byte[] buffer = decode(input);
if (buffer.length < 4) {
throw new ProcessFrostFSException(String.format(DECODE_LENGTH_VALUE_TEMPLATE, buffer.length));
}
byte[] decode = Arrays.copyOfRange(buffer, 0, buffer.length - 4);
byte[] checksum = getSha256(getSha256(decode));
var bufferEnd = Arrays.copyOfRange(buffer, buffer.length - 4, buffer.length);
var checksumStart = Arrays.copyOfRange(checksum, 0, 4);
if (!Arrays.equals(bufferEnd, checksumStart)) {
throw new ProcessFrostFSException(INVALID_CHECKSUM);
}
return decode;
}
public static String base58CheckEncode(byte[] data) {
if (isNull(data)) {
throw new ValidationFrostFSException(INPUT_PARAM_IS_MISSING);
}
byte[] checksum = getSha256(getSha256(data));
var buffer = concat(data, Arrays.copyOfRange(checksum, 0, 4));
return encode(buffer);
}
public static String encode(byte[] input) {
if (input.length == 0) {
return "";
}
// Count leading zeros.
int zeros = 0;
while (zeros < input.length && input[zeros] == 0) {
++zeros;
}
// Convert base-256 digits to base-58 digits (plus conversion to ASCII characters)
input = Arrays.copyOf(input, input.length); // since we modify it in-place
char[] encoded = new char[input.length * 2]; // upper bound
int outputStart = encoded.length;
for (int inputStart = zeros; inputStart < input.length; ) {
encoded[--outputStart] = ALPHABET[divmod(input, inputStart, BASE256_SYMBOL_COUNT, BASE58_SYMBOL_COUNT)];
if (input[inputStart] == 0) {
++inputStart; // optimization - skip leading zeros
}
}
// Preserve exactly as many leading encoded zeros in output as there were leading zeros in input.
while (outputStart < encoded.length && encoded[outputStart] == ENCODED_ZERO) {
++outputStart;
}
while (--zeros >= 0) {
encoded[--outputStart] = ENCODED_ZERO;
}
// Return encoded string (including encoded leading zeros).
return new String(encoded, outputStart, encoded.length - outputStart);
}
public static byte[] decode(String input) {
if (input.isEmpty()) {
return new byte[0];
}
// Convert the base58-encoded ASCII chars to a base58 byte sequence (base58 digits).
byte[] input58 = new byte[input.length()];
for (int i = 0; i < input.length(); ++i) {
char c = input.charAt(i);
int digit = c < BASE58_ASCII_MAX_VALUE ? INDEXES[c] : -1;
if (digit < 0) {
throw new ValidationFrostFSException(String.format(INVALID_BASE58_CHARACTER_TEMPLATE, (int) c));
}
input58[i] = (byte) digit;
}
// Count leading zeros.
int zeros = 0;
while (zeros < input58.length && input58[zeros] == 0) {
++zeros;
}
// Convert base-58 digits to base-256 digits.
byte[] decoded = new byte[input.length()];
int outputStart = decoded.length;
for (int inputStart = zeros; inputStart < input58.length; ) {
decoded[--outputStart] = divmod(input58, inputStart, BASE58_SYMBOL_COUNT, BASE256_SYMBOL_COUNT);
if (input58[inputStart] == 0) {
++inputStart; // optimization - skip leading zeros
}
}
// Ignore extra leading zeroes that were added during the calculation.
while (outputStart < decoded.length && decoded[outputStart] == 0) {
++outputStart;
}
// Return decoded data (including original number of leading zeros).
return Arrays.copyOfRange(decoded, outputStart - zeros, decoded.length);
}
private static byte divmod(byte[] number, int firstDigit, int base, int divisor) {
// this is just long division which accounts for the base of the input digits
int remainder = 0;
for (int i = firstDigit; i < number.length; i++) {
int digit = (int) number[i] & BYTE_DIVISION;
int temp = remainder * base + digit;
number[i] = (byte) (temp / divisor);
remainder = temp % divisor;
}
return (byte) remainder;
}
}

View file

@ -4,7 +4,6 @@ import com.google.protobuf.ByteString;
import com.google.protobuf.Message;
import info.frostfs.sdk.exceptions.ValidationFrostFSException;
import org.apache.commons.lang3.StringUtils;
import org.bouncycastle.crypto.digests.RIPEMD160Digest;
import java.math.BigInteger;
import java.security.MessageDigest;
@ -15,24 +14,11 @@ import static java.util.Objects.isNull;
public class Helper {
private static final String SHA256 = "SHA-256";
private static final int RIPEMD_160_HASH_BYTE_LENGTH = 20;
private static final int HEX_RADIX = 16;
private Helper() {
}
public static byte[] getRipemd160(byte[] value) {
if (isNull(value)) {
throw new ValidationFrostFSException(INPUT_PARAM_IS_MISSING);
}
var hash = new byte[RIPEMD_160_HASH_BYTE_LENGTH];
var digest = new RIPEMD160Digest();
digest.update(value, 0, value.length);
digest.doFinal(hash, 0);
return hash;
}
public static MessageDigest getSha256Instance() {
try {
return MessageDigest.getInstance(SHA256);

View file

@ -2,7 +2,6 @@ package info.frostfs.sdk;
import info.frostfs.sdk.exceptions.ProcessFrostFSException;
import info.frostfs.sdk.exceptions.ValidationFrostFSException;
import org.apache.commons.lang3.StringUtils;
import org.bouncycastle.asn1.sec.SECNamedCurves;
import org.bouncycastle.asn1.sec.SECObjectIdentifiers;
import org.bouncycastle.asn1.x9.X9ECParameters;
@ -10,12 +9,7 @@ import org.bouncycastle.crypto.params.ECDomainParameters;
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
import org.bouncycastle.jce.spec.ECNamedCurveSpec;
import org.bouncycastle.math.ec.ECPoint;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
@ -24,63 +18,20 @@ import java.security.spec.ECParameterSpec;
import java.security.spec.ECPrivateKeySpec;
import java.security.spec.ECPublicKeySpec;
import java.security.spec.InvalidKeySpecException;
import java.util.Arrays;
import static info.frostfs.sdk.Helper.getRipemd160;
import static info.frostfs.sdk.Helper.getSha256;
import static info.frostfs.sdk.constants.ErrorConst.*;
import static info.frostfs.sdk.constants.ErrorConst.COMPRESSED_PUBLIC_KEY_WRONG_LENGTH_TEMPLATE;
import static info.frostfs.sdk.constants.ErrorConst.INPUT_PARAM_IS_MISSING;
import static java.util.Objects.isNull;
import static org.bouncycastle.util.BigIntegers.fromUnsignedByteArray;
public class KeyExtension {
public static final byte NEO_ADDRESS_VERSION = 0x35;
private static final String CURVE_NAME = "secp256r1";
private static final String SECURITY_ALGORITHM = "EC";
private static final int PS_IN_HASH160 = 0x0C;
private static final int DECODE_ADDRESS_LENGTH = 21;
private static final int COMPRESSED_PUBLIC_KEY_LENGTH = 33;
private static final int UNCOMPRESSED_PUBLIC_KEY_LENGTH = 65;
private static final int CHECK_SIG_DESCRIPTOR = ByteBuffer
.wrap(getSha256("System.Crypto.CheckSig".getBytes(StandardCharsets.US_ASCII)))
.order(ByteOrder.LITTLE_ENDIAN).getInt();
private KeyExtension() {
}
public static byte[] compress(byte[] publicKey) {
checkInputValue(publicKey);
if (publicKey.length != UNCOMPRESSED_PUBLIC_KEY_LENGTH) {
throw new ValidationFrostFSException(String.format(
UNCOMPRESSED_PUBLIC_KEY_WRONG_LENGTH_TEMPLATE, UNCOMPRESSED_PUBLIC_KEY_LENGTH, publicKey.length
));
}
var secp256R1 = SECNamedCurves.getByOID(SECObjectIdentifiers.secp256r1);
var point = secp256R1.getCurve().decodePoint(publicKey);
return point.getEncoded(true);
}
public static byte[] getPrivateKeyFromWIF(String wif) {
if (StringUtils.isEmpty(wif)) {
throw new ValidationFrostFSException(INPUT_PARAM_IS_MISSING);
}
var data = Base58.base58CheckDecode(wif);
return Arrays.copyOfRange(data, 1, data.length - 1);
}
public static byte[] loadPublicKey(byte[] privateKey) {
checkInputValue(privateKey);
X9ECParameters params = SECNamedCurves.getByOID(SECObjectIdentifiers.secp256r1);
ECDomainParameters domain = new ECDomainParameters(
params.getCurve(), params.getG(), params.getN(), params.getH()
);
ECPoint q = domain.getG().multiply(new BigInteger(1, privateKey));
ECPublicKeyParameters publicParams = new ECPublicKeyParameters(q, domain);
return publicParams.getQ().getEncoded(true);
}
public static PrivateKey loadPrivateKey(byte[] privateKey) {
checkInputValue(privateKey);
@ -134,58 +85,6 @@ public class KeyExtension {
}
}
public static byte[] getScriptHash(byte[] publicKey) {
checkInputValue(publicKey);
var script = createSignatureRedeemScript(publicKey);
return getRipemd160(getSha256(script));
}
public static String publicKeyToAddress(byte[] publicKey) {
checkInputValue(publicKey);
if (publicKey.length != COMPRESSED_PUBLIC_KEY_LENGTH) {
throw new ValidationFrostFSException(String.format(
ENCODED_COMPRESSED_PUBLIC_KEY_WRONG_LENGTH_TEMPLATE, COMPRESSED_PUBLIC_KEY_LENGTH, publicKey.length
));
}
return toAddress(getScriptHash(publicKey));
}
private static String toAddress(byte[] scriptHash) {
checkInputValue(scriptHash);
byte[] data = new byte[DECODE_ADDRESS_LENGTH];
data[0] = NEO_ADDRESS_VERSION;
System.arraycopy(scriptHash, 0, data, 1, scriptHash.length);
return Base58.base58CheckEncode(data);
}
private static byte[] getBytes(int value) {
byte[] buffer = new byte[4];
for (int i = 0; i < buffer.length; i++) {
buffer[i] = (byte) (value >> i * Byte.SIZE);
}
return buffer;
}
private static byte[] createSignatureRedeemScript(byte[] publicKey) {
checkInputValue(publicKey);
if (publicKey.length != COMPRESSED_PUBLIC_KEY_LENGTH) {
throw new ValidationFrostFSException(String.format(
ENCODED_COMPRESSED_PUBLIC_KEY_WRONG_LENGTH_TEMPLATE, COMPRESSED_PUBLIC_KEY_LENGTH, publicKey.length
));
}
var script = new byte[]{PS_IN_HASH160, COMPRESSED_PUBLIC_KEY_LENGTH}; //PUSHDATA1 33
script = ArrayHelper.concat(script, publicKey);
script = ArrayHelper.concat(script, new byte[]{UNCOMPRESSED_PUBLIC_KEY_LENGTH}); //SYSCALL
script = ArrayHelper.concat(script, getBytes(CHECK_SIG_DESCRIPTOR)); //Neo_Crypto_CheckSig
return script;
}
private static void checkInputValue(byte[] data) {
if (isNull(data) || data.length == 0) {
throw new ValidationFrostFSException(INPUT_PARAM_IS_MISSING);

View file

@ -1,56 +0,0 @@
package info.frostfs.sdk;
import info.frostfs.sdk.exceptions.ProcessFrostFSException;
import info.frostfs.sdk.exceptions.ValidationFrostFSException;
import org.junit.jupiter.api.Test;
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 Base58Test {
private static final String WIF = "L1YS4myg3xHPvi3FHeLaEt7G8upwJaWL5YLV7huviuUtXFpzBMqZ";
private static final byte[] DECODE = new byte[]{
-128, -128, -5, 30, -36, -118, 85, -67, -6, 81, 43, 93, -38, 106, 21, -88, 127, 15, 125, -79, -17, -40, 77,
-15, 122, -88, 72, 109, -47, 125, -80, -40, -38, 1
};
@Test
void base58DecodeEncode() {
//When + Then
assertEquals(WIF, Base58.base58CheckEncode(Base58.base58CheckDecode(WIF)));
}
@Test
void base58Decode_success() {
//When
var decode = Base58.base58CheckDecode(WIF);
//Then
assertThat(decode).hasSize(34).containsExactly(DECODE);
}
@Test
void base58Decode_wrong() {
//When + Then
assertThrows(ValidationFrostFSException.class, () -> Base58.base58CheckDecode(null));
assertThrows(ValidationFrostFSException.class, () -> Base58.base58CheckDecode(""));
assertThrows(ValidationFrostFSException.class, () -> Base58.base58CheckDecode("WIF"));
assertThrows(ProcessFrostFSException.class, () -> Base58.base58CheckDecode("fh"));
}
@Test
void base58Encode_success() {
//When
var encode = Base58.base58CheckEncode(DECODE);
//Then
assertEquals(WIF, encode);
}
@Test
void base58Encode_wrong() {
//When + Then
assertThrows(ValidationFrostFSException.class, () -> Base58.base58CheckEncode(null));
}
}

View file

@ -10,38 +10,6 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
public class HelperTest {
@Test
void getRipemd160_success() {
//Given
var value = new byte[]{1, 2, 3, 4, 5};
var expected = new byte[]
{-21, -126, 92, 75, 36, -12, 37, 7, 122, 6, 124, -61, -66, -12, 87, 120, 63, 90, -41, 5};
//When
var result = Helper.getRipemd160(value);
//Then
assertThat(result).hasSize(20).containsExactly(expected);
}
@Test
void getRipemd160_givenParamIsNull() {
//When + Then
assertThrows(ValidationFrostFSException.class, () -> Helper.getRipemd160(null));
}
@Test
void getRipemd160_givenParamsIsEmpty() {
//Given
var value = new byte[]{};
var expected = new byte[]
{-100, 17, -123, -91, -59, -23, -4, 84, 97, 40, 8, -105, 126, -24, -11, 72, -78, 37, -115, 49};
//When
var result = Helper.getRipemd160(value);
//Then
assertThat(result).hasSize(20).containsExactly(expected);
}
@Test
void getSha256Instance() {
//When

View file

@ -8,8 +8,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class KeyExtensionTest {
private static final String WIF = "L1YS4myg3xHPvi3FHeLaEt7G8upwJaWL5YLV7huviuUtXFpzBMqZ";
private static final String OWNER_ID = "NVxUSpEEJzYXZZtUs18PrJTD9QZkLLNQ8S";
private static final byte[] PRIVATE_KEY = new byte[]{
-128, -5, 30, -36, -118, 85, -67, -6, 81, 43, 93, -38, 106, 21, -88, 127, 15, 125, -79, -17, -40, 77, -15,
122, -88, 72, 109, -47, 125, -80, -40, -38
@ -24,38 +22,6 @@ public class KeyExtensionTest {
-68, -73, 65, -57, -26, 75, 4, -51, -40, -20, 75, 89, -59, 111, 96, -80, 56, 13
};
@Test
void getPrivateKeyFromWIF_success() {
//When
var privateKey = KeyExtension.getPrivateKeyFromWIF(WIF);
//Then
assertThat(privateKey).hasSize(32).containsExactly(PRIVATE_KEY);
}
@Test
void getPrivateKeyFromWIF_wrong() {
//When + Then
assertThrows(ValidationFrostFSException.class, () -> KeyExtension.getPrivateKeyFromWIF(""));
assertThrows(ValidationFrostFSException.class, () -> KeyExtension.getPrivateKeyFromWIF(null));
}
@Test
void loadPublicKey_success() {
//When
var publicKey = KeyExtension.loadPublicKey(PRIVATE_KEY);
//Then
assertThat(publicKey).hasSize(33).containsExactly(PUBLIC_KEY);
}
@Test
void loadPublicKey_wrong() {
//When + Then
assertThrows(ValidationFrostFSException.class, () -> KeyExtension.loadPublicKey(null));
assertThrows(ValidationFrostFSException.class, () -> KeyExtension.loadPublicKey(new byte[]{}));
}
@Test
void loadPrivateKey_success() {
//When
@ -92,61 +58,4 @@ public class KeyExtensionTest {
assertThrows(ValidationFrostFSException.class, () -> KeyExtension.getPublicKeyFromBytes(new byte[]{}));
assertThrows(ValidationFrostFSException.class, () -> KeyExtension.getPublicKeyFromBytes(PRIVATE_KEY));
}
@Test
void getScriptHash_success() {
//Given
var expected = new byte[]{
110, 42, -125, -76, -25, -44, -94, 22, -98, 117, -100, -5, 103, 74, -128, -51, 37, -116, -102, 71
};
//When
var hash = KeyExtension.getScriptHash(PUBLIC_KEY);
//Then
assertThat(hash).hasSize(20).containsExactly(expected);
}
@Test
void getScriptHash_wrong() {
//When + Then
assertThrows(ValidationFrostFSException.class, () -> KeyExtension.getScriptHash(null));
assertThrows(ValidationFrostFSException.class, () -> KeyExtension.getScriptHash(new byte[]{}));
assertThrows(ValidationFrostFSException.class, () -> KeyExtension.getScriptHash(PRIVATE_KEY));
}
@Test
void publicKeyToAddress_success() {
//When
var address = KeyExtension.publicKeyToAddress(PUBLIC_KEY);
//Then
assertEquals(OWNER_ID, address);
}
@Test
void publicKeyToAddress_wrong() {
//When + Then
assertThrows(ValidationFrostFSException.class, () -> KeyExtension.publicKeyToAddress(null));
assertThrows(ValidationFrostFSException.class, () -> KeyExtension.publicKeyToAddress(new byte[]{}));
assertThrows(ValidationFrostFSException.class, () -> KeyExtension.publicKeyToAddress(PRIVATE_KEY));
}
@Test
void compress_success() {
//When
var publicKey = KeyExtension.compress(UNCOMPRESSED_PUBLIC_KEY);
//Then
assertThat(publicKey).hasSize(33).containsExactly(PUBLIC_KEY);
}
@Test
void compress_wrong() {
//When + Then
assertThrows(ValidationFrostFSException.class, () -> KeyExtension.compress(null));
assertThrows(ValidationFrostFSException.class, () -> KeyExtension.compress(new byte[]{}));
assertThrows(ValidationFrostFSException.class, () -> KeyExtension.compress(PUBLIC_KEY));
}
}

View file

@ -2,6 +2,7 @@ package info.frostfs.sdk.constants;
public class ErrorConst {
public static final String OBJECT_IS_NULL = "object must not be null";
public static final String STRING_IS_BLANK = "string must not be blank";
public static final String INPUT_PARAM_IS_MISSING = "input parameter is not present";
public static final String SOME_PARAM_IS_MISSING = "one of the input parameters is not present";
public static final String PARAM_IS_MISSING_TEMPLATE = "param %s is not present";
@ -19,6 +20,7 @@ public class ErrorConst {
public static final String UNEXPECTED_MESSAGE_TYPE_TEMPLATE = "unexpected message type, expected %s, actually %s";
public static final String WIF_IS_INVALID = "WIF is invalid";
public static final String WALLET_IS_INVALID = "wallet is not present";
public static final String UNEXPECTED_STREAM = "unexpected end of stream";
public static final String INVALID_HOST_TEMPLATE = "host %s has invalid format. Error: %s";
public static final String INVALID_RESPONSE = "invalid response";
@ -27,14 +29,7 @@ public class ErrorConst {
public static final String UNKNOWN_ENUM_VALUE_TEMPLATE = "unknown %s value: %s";
public static final String INPUT_PARAM_IS_NOT_SHA256 = "%s must be a sha256 hash";
public static final String DECODE_LENGTH_VALUE_TEMPLATE = "decode array length must be >= 4, but %s";
public static final String INVALID_BASE58_CHARACTER_TEMPLATE = "invalid character in Base58: 0x%04x";
public static final String INVALID_CHECKSUM = "checksum does not match";
public static final String WRONG_SIGNATURE_SIZE_TEMPLATE = "wrong signature size. Expected length=%s, actual=%s";
public static final String ENCODED_COMPRESSED_PUBLIC_KEY_WRONG_LENGTH_TEMPLATE =
"publicKey isn't encoded compressed public key. Expected length=%s, actual=%s";
public static final String UNCOMPRESSED_PUBLIC_KEY_WRONG_LENGTH_TEMPLATE =
"compress argument isn't uncompressed public key. Expected length=%s, actual=%s";
public static final String COMPRESSED_PUBLIC_KEY_WRONG_LENGTH_TEMPLATE =
"decompress argument isn't compressed public key. Expected length=%s, actual=%s";
@ -51,6 +46,19 @@ public class ErrorConst {
public static final String FIELDS_DELIMITER_COMMA = ", ";
public static final String FIELDS_DELIMITER_OR = " or ";
public static final String UNSUPPORTED_MARSHALLER_VERSION_TEMPLATE = "unsupported marshaller version %s";
public static final String UNSUPPORTED_CHAIN_VERSION_TEMPLATE = "unsupported chain version %s";
public static final String MARSHAL_SIZE_DIFFERS = "actual data size differs from expected";
public static final String UNMARSHAL_SIZE_DIFFERS = "unmarshalled bytes left";
public static final String BYTES_ARE_OVER_FOR_SERIALIZE_TEMPLATE =
"not enough bytes left to serialize value of type %s with length=%s";
public static final String BYTES_ARE_OVER_FOR_DESERIALIZE_TEMPLATE =
"not enough bytes left to deserialize a value of type %s from offset=%s";
public static final String SLICE_IS_TOO_BIG_TEMPLATE = "slice size is too big=%s";
public static final String SLICE_SIZE_IS_INVALID_TEMPLATE = "invalid slice size=%s";
public static final String STRING_IS_TOO_BIG_TEMPLATE = "string size is too big=%s";
public static final String STRING_SIZE_IS_INVALID_TEMPLATE = "invalid string size=%s";
private ErrorConst() {
}
}

View file

@ -0,0 +1,15 @@
package info.frostfs.sdk.dto.ape;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class Actions {
private boolean inverted;
private String[] names;
}

View file

@ -0,0 +1,17 @@
package info.frostfs.sdk.dto.ape;
import info.frostfs.sdk.enums.RuleMatchType;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class Chain {
private Byte[] id;
private Rule[] rules;
private RuleMatchType matchType;
}

View file

@ -0,0 +1,19 @@
package info.frostfs.sdk.dto.ape;
import info.frostfs.sdk.enums.ConditionKindType;
import info.frostfs.sdk.enums.ConditionType;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class Condition {
private ConditionType op;
private ConditionKindType kind;
private String key;
private String value;
}

View file

@ -0,0 +1,15 @@
package info.frostfs.sdk.dto.ape;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class Resources {
private boolean inverted;
private String[] names;
}

View file

@ -0,0 +1,27 @@
package info.frostfs.sdk.dto.ape;
import info.frostfs.sdk.enums.RuleStatus;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class Rule {
private RuleStatus status;
// Actions the operation is applied to.
private Actions actions;
// List of the resources the operation is applied to.
private Resources resources;
// True if individual conditions must be combined with the logical OR.
// By default, AND is used, so _each_ condition must pass.
private boolean any;
private Condition[] conditions;
}

View file

@ -1,10 +0,0 @@
package info.frostfs.sdk.dto.chain;
import lombok.AllArgsConstructor;
import lombok.Getter;
@Getter
@AllArgsConstructor
public class Chain {
private final byte[] raw;
}

View file

@ -2,7 +2,8 @@ package info.frostfs.sdk.dto.container;
import info.frostfs.sdk.dto.netmap.PlacementPolicy;
import info.frostfs.sdk.dto.netmap.Version;
import info.frostfs.sdk.enums.BasicAcl;
import info.frostfs.sdk.dto.object.OwnerId;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
@ -12,16 +13,16 @@ import java.util.UUID;
@Getter
@Setter
@AllArgsConstructor
public class Container {
private UUID nonce;
private BasicAcl basicAcl;
private PlacementPolicy placementPolicy;
private Version version;
private OwnerId ownerId;
private Map<String, String> attributes = new HashMap<>();
public Container(BasicAcl basicAcl, PlacementPolicy placementPolicy) {
public Container(PlacementPolicy placementPolicy) {
this.nonce = UUID.randomUUID();
this.basicAcl = basicAcl;
this.placementPolicy = placementPolicy;
}
}

View file

@ -1,8 +1,8 @@
package info.frostfs.sdk.dto.container;
import info.frostfs.sdk.Base58;
import info.frostfs.sdk.constants.AppConst;
import info.frostfs.sdk.exceptions.ValidationFrostFSException;
import io.neow3j.crypto.Base58;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

View file

@ -0,0 +1,15 @@
package info.frostfs.sdk.dto.netmap;
import info.frostfs.sdk.enums.FilterOperation;
import lombok.AllArgsConstructor;
import lombok.Getter;
@Getter
@AllArgsConstructor
public class Filter {
private final String name;
private final String key;
private final FilterOperation operation;
private final String value;
private final Filter[] filters;
}

View file

@ -8,4 +8,15 @@ import lombok.Getter;
public class PlacementPolicy {
private final Replica[] replicas;
private final boolean unique;
private final int backupFactory;
private final Filter[] filters;
private final Selector[] selectors;
public PlacementPolicy(Replica[] replicas, boolean unique, int backupFactory) {
this.replicas = replicas;
this.unique = unique;
this.backupFactory = backupFactory;
this.filters = null;
this.selectors = null;
}
}

View file

@ -0,0 +1,15 @@
package info.frostfs.sdk.dto.netmap;
import info.frostfs.sdk.enums.SelectorClause;
import lombok.AllArgsConstructor;
import lombok.Getter;
@Getter
@AllArgsConstructor
public class Selector {
private final String name;
private final int count;
private final SelectorClause clause;
private final String attribute;
private final String filter;
}

View file

@ -1,8 +1,8 @@
package info.frostfs.sdk.dto.object;
import info.frostfs.sdk.Base58;
import info.frostfs.sdk.constants.AppConst;
import info.frostfs.sdk.exceptions.ValidationFrostFSException;
import io.neow3j.crypto.Base58;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

View file

@ -1,14 +1,11 @@
package info.frostfs.sdk.dto.object;
import info.frostfs.sdk.Base58;
import info.frostfs.sdk.exceptions.ValidationFrostFSException;
import io.neow3j.crypto.Base58;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;
import static info.frostfs.sdk.KeyExtension.publicKeyToAddress;
import static info.frostfs.sdk.constants.ErrorConst.INPUT_PARAM_IS_MISSING;
import static info.frostfs.sdk.constants.ErrorConst.INPUT_PARAM_IS_MISSING_TEMPLATE;
import static java.util.Objects.isNull;
@Getter
public class OwnerId {
@ -24,14 +21,6 @@ public class OwnerId {
this.value = value;
}
public OwnerId(byte[] publicKey) {
if (isNull(publicKey) || publicKey.length == 0) {
throw new ValidationFrostFSException(INPUT_PARAM_IS_MISSING);
}
this.value = publicKeyToAddress(publicKey);
}
public byte[] toHash() {
return Base58.decode(value);
}

View file

@ -3,19 +3,21 @@ package info.frostfs.sdk.dto.response;
import info.frostfs.sdk.enums.StatusCode;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import static info.frostfs.sdk.constants.FieldConst.EMPTY_STRING;
import static java.util.Objects.isNull;
@Getter
@Setter
public class ResponseStatus {
private StatusCode code;
private String message;
private String details;
public ResponseStatus(StatusCode code, String message) {
public ResponseStatus(StatusCode code, String message, String details) {
this.code = code;
this.message = isNull(message) ? EMPTY_STRING : message;
this.message = StringUtils.isBlank(message) ? EMPTY_STRING : message;
this.details = StringUtils.isBlank(details) ? EMPTY_STRING : details;
}
public ResponseStatus(StatusCode code) {
@ -25,7 +27,7 @@ public class ResponseStatus {
@Override
public String toString() {
return String.format("Response status: %s. Message: %s.", code, message);
return String.format("Response status: %s. Message: %s. Details: %s", code, message, details);
}
public boolean isSuccess() {

View file

@ -1,33 +0,0 @@
package info.frostfs.sdk.enums;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public enum BasicAcl {
PRIVATE(0x1C8C8CCC),
PUBLIC_RO(0x1FBF8CFF),
PUBLIC_RW(0x1FBFBFFF),
PUBLIC_APPEND(0x1FBF9FFF),
;
private static final Map<Integer, BasicAcl> ENUM_MAP_BY_VALUE;
static {
Map<Integer, BasicAcl> map = new HashMap<>();
for (BasicAcl basicAcl : BasicAcl.values()) {
map.put(basicAcl.value, basicAcl);
}
ENUM_MAP_BY_VALUE = Collections.unmodifiableMap(map);
}
public final int value;
BasicAcl(int value) {
this.value = value;
}
public static BasicAcl get(int value) {
return ENUM_MAP_BY_VALUE.get(value);
}
}

View file

@ -0,0 +1,31 @@
package info.frostfs.sdk.enums;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public enum ConditionKindType {
RESOURCE(0),
REQUEST(1),
;
private static final Map<Integer, ConditionKindType> ENUM_MAP_BY_VALUE;
static {
Map<Integer, ConditionKindType> map = new HashMap<>();
for (ConditionKindType conditionKindType : ConditionKindType.values()) {
map.put(conditionKindType.value, conditionKindType);
}
ENUM_MAP_BY_VALUE = Collections.unmodifiableMap(map);
}
public final int value;
ConditionKindType(int value) {
this.value = value;
}
public static ConditionKindType get(int value) {
return ENUM_MAP_BY_VALUE.get(value);
}
}

View file

@ -0,0 +1,54 @@
package info.frostfs.sdk.enums;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public enum ConditionType {
COND_STRING_EQUALS(0),
COND_STRING_NOT_EQUALS(1),
COND_STRING_EQUALS_IGNORE_CASE(2),
COND_STRING_NOT_EQUALS_IGNORE_CASE(3),
COND_STRING_LIKE(4),
COND_STRING_NOT_LIKE(5),
COND_STRING_LESS_THAN(6),
COND_STRING_LESS_THAN_EQUALS(7),
COND_STRING_GREATER_THAN(8),
COND_STRING_GREATER_THAN_EQUALS(9),
COND_NUMERIC_EQUALS(10),
COND_NUMERIC_NOT_EQUALS(11),
COND_NUMERIC_LESS_THAN(12),
COND_NUMERIC_LESS_THAN_EQUALS(13),
COND_NUMERIC_GREATER_THAN(14),
COND_NUMERIC_GREATER_THAN_EQUALS(15),
COND_SLICE_CONTAINS(16),
COND_IP_ADDRESS(17),
COND_NOT_IP_ADDRESS(18),
;
private static final Map<Integer, ConditionType> ENUM_MAP_BY_VALUE;
static {
Map<Integer, ConditionType> map = new HashMap<>();
for (ConditionType conditionType : ConditionType.values()) {
map.put(conditionType.value, conditionType);
}
ENUM_MAP_BY_VALUE = Collections.unmodifiableMap(map);
}
public final int value;
ConditionType(int value) {
this.value = value;
}
public static ConditionType get(int value) {
return ENUM_MAP_BY_VALUE.get(value);
}
}

View file

@ -0,0 +1,40 @@
package info.frostfs.sdk.enums;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public enum FilterOperation {
OPERATION_UNSPECIFIED(0),
EQ(1),
NE(2),
GT(3),
GE(4),
LT(5),
LE(6),
OR(7),
AND(8),
NOT(9),
LIKE(10),
;
private static final Map<Integer, FilterOperation> ENUM_MAP_BY_VALUE;
static {
Map<Integer, FilterOperation> map = new HashMap<>();
for (FilterOperation nodeState : FilterOperation.values()) {
map.put(nodeState.value, nodeState);
}
ENUM_MAP_BY_VALUE = Collections.unmodifiableMap(map);
}
public final int value;
FilterOperation(int value) {
this.value = value;
}
public static FilterOperation get(int value) {
return ENUM_MAP_BY_VALUE.get(value);
}
}

View file

@ -0,0 +1,34 @@
package info.frostfs.sdk.enums;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public enum RuleMatchType {
// DENY_PRIORITY rejects the request if any `Deny` is specified.
DENY_PRIORITY(0),
// FIRST_MATCH returns the first rule action matched to the request.
FIRST_MATCH(1),
;
private static final Map<Integer, RuleMatchType> ENUM_MAP_BY_VALUE;
static {
Map<Integer, RuleMatchType> map = new HashMap<>();
for (RuleMatchType ruleMatchType : RuleMatchType.values()) {
map.put(ruleMatchType.value, ruleMatchType);
}
ENUM_MAP_BY_VALUE = Collections.unmodifiableMap(map);
}
public final int value;
RuleMatchType(int value) {
this.value = value;
}
public static RuleMatchType get(int value) {
return ENUM_MAP_BY_VALUE.get(value);
}
}

View file

@ -0,0 +1,33 @@
package info.frostfs.sdk.enums;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public enum RuleStatus {
ALLOW(0),
NO_RULE_FOUND(1),
ACCESS_DENIED(2),
QUOTA_LIMIT_REACHED(3),
;
private static final Map<Integer, RuleStatus> ENUM_MAP_BY_VALUE;
static {
Map<Integer, RuleStatus> map = new HashMap<>();
for (RuleStatus ruleStatus : RuleStatus.values()) {
map.put(ruleStatus.value, ruleStatus);
}
ENUM_MAP_BY_VALUE = Collections.unmodifiableMap(map);
}
public final int value;
RuleStatus(int value) {
this.value = value;
}
public static RuleStatus get(int value) {
return ENUM_MAP_BY_VALUE.get(value);
}
}

View file

@ -0,0 +1,32 @@
package info.frostfs.sdk.enums;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public enum SelectorClause {
CLAUSE_UNSPECIFIED(0),
SAME(1),
DISTINCT(2),
;
private static final Map<Integer, SelectorClause> ENUM_MAP_BY_VALUE;
static {
Map<Integer, SelectorClause> map = new HashMap<>();
for (SelectorClause nodeState : SelectorClause.values()) {
map.put(nodeState.value, nodeState);
}
ENUM_MAP_BY_VALUE = Collections.unmodifiableMap(map);
}
public final int value;
SelectorClause(int value) {
this.value = value;
}
public static SelectorClause get(int value) {
return ENUM_MAP_BY_VALUE.get(value);
}
}

View file

@ -1,31 +0,0 @@
package info.frostfs.sdk.mappers.chain;
import frostfs.ape.Types;
import info.frostfs.sdk.dto.chain.Chain;
import org.apache.commons.collections4.CollectionUtils;
import java.util.List;
import java.util.stream.Collectors;
import static java.util.Objects.isNull;
public class ChainMapper {
private ChainMapper() {
}
public static List<Chain> toModels(List<Types.Chain> chains) {
if (CollectionUtils.isEmpty(chains)) {
return null;
}
return chains.stream().map(ChainMapper::toModel).collect(Collectors.toList());
}
public static Chain toModel(Types.Chain chain) {
if (isNull(chain) || chain.getSerializedSize() == 0) {
return null;
}
return new Chain(chain.getRaw().toByteArray());
}
}

View file

@ -7,7 +7,6 @@ import info.frostfs.sdk.dto.container.ContainerId;
import static java.util.Objects.isNull;
public class ContainerIdMapper {
private ContainerIdMapper() {
}

View file

@ -3,33 +3,26 @@ 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.enums.BasicAcl;
import info.frostfs.sdk.exceptions.ProcessFrostFSException;
import info.frostfs.sdk.mappers.netmap.PlacementPolicyMapper;
import info.frostfs.sdk.mappers.netmap.VersionMapper;
import org.apache.commons.collections4.CollectionUtils;
import info.frostfs.sdk.mappers.object.OwnerIdMapper;
import java.util.Optional;
import java.util.stream.Collectors;
import static info.frostfs.sdk.UuidExtension.asBytes;
import static info.frostfs.sdk.UuidExtension.asUuid;
import static info.frostfs.sdk.constants.ErrorConst.UNKNOWN_ENUM_VALUE_TEMPLATE;
import static java.util.Objects.isNull;
public class ContainerMapper {
private ContainerMapper() {
}
public static Types.Container toGrpcMessage(Container container) {
public static Types.Container.Builder toGrpcMessage(Container container) {
if (isNull(container)) {
return null;
}
var containerGrpc = Types.Container.newBuilder()
.setBasicAcl(container.getBasicAcl().value)
.setPlacementPolicy(PlacementPolicyMapper.toGrpcMessage(container.getPlacementPolicy()))
.setNonce(ByteString.copyFrom(asBytes(container.getNonce())));
var attributes = container.getAttributes().entrySet().stream()
.map(entry ->
Types.Container.Attribute.newBuilder()
@ -38,9 +31,16 @@ public class ContainerMapper {
.build()
)
.collect(Collectors.toList());
containerGrpc.addAllAttributes(attributes);
return containerGrpc.build();
var containerGrpc = Types.Container.newBuilder()
.setPlacementPolicy(PlacementPolicyMapper.toGrpcMessage(container.getPlacementPolicy()))
.setNonce(ByteString.copyFrom(asBytes(container.getNonce())))
.addAllAttributes(attributes);
Optional.ofNullable(OwnerIdMapper.toGrpcMessage(container.getOwnerId())).ifPresent(containerGrpc::setOwnerId);
Optional.ofNullable(VersionMapper.toGrpcMessage(container.getVersion())).ifPresent(containerGrpc::setVersion);
return containerGrpc;
}
public static Container toModel(Types.Container containerGrpc) {
@ -48,24 +48,15 @@ public class ContainerMapper {
return null;
}
var basicAcl = BasicAcl.get(containerGrpc.getBasicAcl());
if (isNull(basicAcl)) {
throw new ProcessFrostFSException(
String.format(UNKNOWN_ENUM_VALUE_TEMPLATE, BasicAcl.class.getName(), containerGrpc.getBasicAcl())
);
}
var attributes = containerGrpc.getAttributesList().stream()
.collect(Collectors.toMap(Types.Container.Attribute::getKey, Types.Container.Attribute::getValue));
var container = new Container(basicAcl, PlacementPolicyMapper.toModel(containerGrpc.getPlacementPolicy()));
container.setNonce(asUuid(containerGrpc.getNonce().toByteArray()));
container.setVersion(VersionMapper.toModel(containerGrpc.getVersion()));
if (CollectionUtils.isNotEmpty(containerGrpc.getAttributesList())) {
var attributes = containerGrpc.getAttributesList().stream()
.collect(Collectors.toMap(Types.Container.Attribute::getKey, Types.Container.Attribute::getValue));
container.setAttributes(attributes);
}
return container;
return new Container(
asUuid(containerGrpc.getNonce().toByteArray()),
PlacementPolicyMapper.toModel(containerGrpc.getPlacementPolicy()),
VersionMapper.toModel(containerGrpc.getVersion()),
OwnerIdMapper.toModel(containerGrpc.getOwnerId()),
attributes
);
}
}

View file

@ -0,0 +1,85 @@
package info.frostfs.sdk.mappers.netmap;
import frostfs.netmap.Types;
import info.frostfs.sdk.dto.netmap.Filter;
import info.frostfs.sdk.enums.FilterOperation;
import info.frostfs.sdk.exceptions.ProcessFrostFSException;
import info.frostfs.sdk.exceptions.ValidationFrostFSException;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import static info.frostfs.sdk.constants.ErrorConst.INPUT_PARAM_IS_MISSING_TEMPLATE;
import static info.frostfs.sdk.constants.ErrorConst.UNKNOWN_ENUM_VALUE_TEMPLATE;
import static java.util.Objects.isNull;
public class FilterMapper {
private FilterMapper() {
}
public static List<Types.Filter> toGrpcMessages(Filter[] filters) {
if (ArrayUtils.isEmpty(filters)) {
return Collections.emptyList();
}
return Arrays.stream(filters).map(FilterMapper::toGrpcMessage).collect(Collectors.toList());
}
public static Types.Filter toGrpcMessage(Filter filter) {
if (isNull(filter)) {
throw new ValidationFrostFSException(
String.format(INPUT_PARAM_IS_MISSING_TEMPLATE, Filter.class.getName())
);
}
var operation = Types.Operation.forNumber(filter.getOperation().value);
if (isNull(operation)) {
throw new ProcessFrostFSException(String.format(
UNKNOWN_ENUM_VALUE_TEMPLATE,
Types.Operation.class.getName(),
filter.getOperation().name()
));
}
return Types.Filter.newBuilder()
.setName(filter.getName())
.setKey(filter.getKey())
.setOp(operation)
.setValue(filter.getValue())
.addAllFilters(toGrpcMessages(filter.getFilters()))
.build();
}
public static Filter[] toModels(List<Types.Filter> filters) {
if (CollectionUtils.isEmpty(filters)) {
return null;
}
return filters.stream().map(FilterMapper::toModel).toArray(Filter[]::new);
}
public static Filter toModel(Types.Filter filter) {
if (isNull(filter) || filter.getSerializedSize() == 0) {
return null;
}
var operation = FilterOperation.get(filter.getOpValue());
if (isNull(operation)) {
throw new ProcessFrostFSException(
String.format(UNKNOWN_ENUM_VALUE_TEMPLATE, FilterOperation.class.getName(), filter.getOp())
);
}
return new Filter(
filter.getName(),
filter.getKey(),
operation,
filter.getValue(),
toModels(filter.getFiltersList())
);
}
}

View file

@ -8,7 +8,6 @@ import java.util.stream.Collectors;
import static java.util.Objects.isNull;
public class NetmapSnapshotMapper {
private NetmapSnapshotMapper() {
}

View file

@ -2,7 +2,6 @@ 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 static java.util.Objects.isNull;
@ -15,14 +14,13 @@ public class PlacementPolicyMapper {
return null;
}
var pp = Types.PlacementPolicy.newBuilder()
.setUnique(placementPolicy.isUnique());
for (Replica replica : placementPolicy.getReplicas()) {
pp.addReplicas(ReplicaMapper.toGrpcMessage(replica));
}
return pp.build();
return Types.PlacementPolicy.newBuilder()
.setUnique(placementPolicy.isUnique())
.setContainerBackupFactor(placementPolicy.getBackupFactory())
.addAllFilters(FilterMapper.toGrpcMessages(placementPolicy.getFilters()))
.addAllSelectors(SelectorMapper.toGrpcMessages(placementPolicy.getSelectors()))
.addAllReplicas(ReplicaMapper.toGrpcMessages(placementPolicy.getReplicas()))
.build();
}
public static PlacementPolicy toModel(Types.PlacementPolicy placementPolicy) {
@ -31,8 +29,11 @@ public class PlacementPolicyMapper {
}
return new PlacementPolicy(
placementPolicy.getReplicasList().stream().map(ReplicaMapper::toModel).toArray(Replica[]::new),
placementPolicy.getUnique()
ReplicaMapper.toModels(placementPolicy.getReplicasList()),
placementPolicy.getUnique(),
placementPolicy.getContainerBackupFactor(),
FilterMapper.toModels(placementPolicy.getFiltersList()),
SelectorMapper.toModels(placementPolicy.getSelectorsList())
);
}
}

View file

@ -2,17 +2,35 @@ package info.frostfs.sdk.mappers.netmap;
import frostfs.netmap.Types;
import info.frostfs.sdk.dto.netmap.Replica;
import info.frostfs.sdk.exceptions.ValidationFrostFSException;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import static info.frostfs.sdk.constants.ErrorConst.INPUT_PARAM_IS_MISSING_TEMPLATE;
import static java.util.Objects.isNull;
public class ReplicaMapper {
private ReplicaMapper() {
}
public static List<Types.Replica> toGrpcMessages(Replica[] replicas) {
if (ArrayUtils.isEmpty(replicas)) {
return Collections.emptyList();
}
return Arrays.stream(replicas).map(ReplicaMapper::toGrpcMessage).collect(Collectors.toList());
}
public static Types.Replica toGrpcMessage(Replica replica) {
if (isNull(replica)) {
return null;
throw new ValidationFrostFSException(
String.format(INPUT_PARAM_IS_MISSING_TEMPLATE, Replica.class.getName())
);
}
return Types.Replica.newBuilder()
@ -21,6 +39,14 @@ public class ReplicaMapper {
.build();
}
public static Replica[] toModels(List<Types.Replica> filters) {
if (CollectionUtils.isEmpty(filters)) {
return null;
}
return filters.stream().map(ReplicaMapper::toModel).toArray(Replica[]::new);
}
public static Replica toModel(Types.Replica replica) {
if (isNull(replica) || replica.getSerializedSize() == 0) {
return null;

View file

@ -0,0 +1,85 @@
package info.frostfs.sdk.mappers.netmap;
import frostfs.netmap.Types;
import info.frostfs.sdk.dto.netmap.Selector;
import info.frostfs.sdk.enums.SelectorClause;
import info.frostfs.sdk.exceptions.ProcessFrostFSException;
import info.frostfs.sdk.exceptions.ValidationFrostFSException;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import static info.frostfs.sdk.constants.ErrorConst.INPUT_PARAM_IS_MISSING_TEMPLATE;
import static info.frostfs.sdk.constants.ErrorConst.UNKNOWN_ENUM_VALUE_TEMPLATE;
import static java.util.Objects.isNull;
public class SelectorMapper {
private SelectorMapper() {
}
public static List<Types.Selector> toGrpcMessages(Selector[] selectors) {
if (ArrayUtils.isEmpty(selectors)) {
return Collections.emptyList();
}
return Arrays.stream(selectors).map(SelectorMapper::toGrpcMessage).collect(Collectors.toList());
}
public static Types.Selector toGrpcMessage(Selector selector) {
if (isNull(selector)) {
throw new ValidationFrostFSException(
String.format(INPUT_PARAM_IS_MISSING_TEMPLATE, Selector.class.getName())
);
}
var clause = Types.Clause.forNumber(selector.getClause().value);
if (isNull(clause)) {
throw new ProcessFrostFSException(String.format(
UNKNOWN_ENUM_VALUE_TEMPLATE,
Types.Clause.class.getName(),
selector.getClause().name()
));
}
return Types.Selector.newBuilder()
.setName(selector.getName())
.setCount(selector.getCount())
.setClause(clause)
.setAttribute(selector.getAttribute())
.setFilter(selector.getFilter())
.build();
}
public static Selector[] toModels(List<Types.Selector> selectors) {
if (CollectionUtils.isEmpty(selectors)) {
return null;
}
return selectors.stream().map(SelectorMapper::toModel).toArray(Selector[]::new);
}
public static Selector toModel(Types.Selector selector) {
if (isNull(selector) || selector.getSerializedSize() == 0) {
return null;
}
var clause = SelectorClause.get(selector.getClauseValue());
if (isNull(clause)) {
throw new ProcessFrostFSException(
String.format(UNKNOWN_ENUM_VALUE_TEMPLATE, SelectorClause.class.getName(), selector.getClause())
);
}
return new Selector(
selector.getName(),
selector.getCount(),
clause,
selector.getAttribute(),
selector.getFilter()
);
}
}

View file

@ -6,7 +6,6 @@ import info.frostfs.sdk.dto.netmap.Version;
import static java.util.Objects.isNull;
public class VersionMapper {
private VersionMapper() {
}

View file

@ -4,19 +4,19 @@ import frostfs.object.Types;
import info.frostfs.sdk.dto.object.ObjectAttribute;
import org.apache.commons.collections4.CollectionUtils;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import static java.util.Objects.isNull;
public class ObjectAttributeMapper {
private ObjectAttributeMapper() {
}
public static List<Types.Header.Attribute> toGrpcMessages(List<ObjectAttribute> attributes) {
if (CollectionUtils.isEmpty(attributes)) {
return null;
return Collections.emptyList();
}
return attributes.stream().map(ObjectAttributeMapper::toGrpcMessage).collect(Collectors.toList());

View file

@ -7,7 +7,6 @@ import info.frostfs.sdk.dto.object.ObjectId;
import static java.util.Objects.isNull;
public class ObjectFrostFSMapper {
private ObjectFrostFSMapper() {
}

View file

@ -7,7 +7,6 @@ import info.frostfs.sdk.dto.object.ObjectId;
import static java.util.Objects.isNull;
public class ObjectIdMapper {
private ObjectIdMapper() {
}

View file

@ -3,11 +3,11 @@ package info.frostfs.sdk.mappers.object;
import com.google.protobuf.ByteString;
import frostfs.refs.Types;
import info.frostfs.sdk.dto.object.OwnerId;
import io.neow3j.crypto.Base58;
import static java.util.Objects.isNull;
public class OwnerIdMapper {
private OwnerIdMapper() {
}
@ -20,4 +20,12 @@ public class OwnerIdMapper {
.setValue(ByteString.copyFrom(ownerId.toHash()))
.build();
}
public static OwnerId toModel(Types.OwnerID ownerId) {
if (isNull(ownerId) || ownerId.getSerializedSize() == 0) {
return null;
}
return new OwnerId(Base58.encode(ownerId.getValue().toByteArray()));
}
}

View file

@ -8,7 +8,6 @@ import info.frostfs.sdk.mappers.object.ObjectIdMapper;
import static java.util.Objects.isNull;
public class AddressMapper {
private AddressMapper() {
}

View file

@ -4,19 +4,19 @@ import frostfs.object.Service;
import info.frostfs.sdk.dto.object.patch.Range;
import org.apache.commons.collections4.CollectionUtils;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import static java.util.Objects.isNull;
public class RangeMapper {
private RangeMapper() {
}
public static List<Service.Range> toGrpcMessages(List<Range> ranges) {
if (CollectionUtils.isEmpty(ranges)) {
return null;
return Collections.emptyList();
}
return ranges.stream().map(RangeMapper::toGrpcMessage).collect(Collectors.toList());

View file

@ -5,6 +5,9 @@ import info.frostfs.sdk.dto.response.ResponseStatus;
import info.frostfs.sdk.enums.StatusCode;
import info.frostfs.sdk.exceptions.ProcessFrostFSException;
import java.util.stream.Collectors;
import static info.frostfs.sdk.constants.ErrorConst.FIELDS_DELIMITER_COMMA;
import static info.frostfs.sdk.constants.ErrorConst.UNKNOWN_ENUM_VALUE_TEMPLATE;
import static java.util.Objects.isNull;
@ -24,6 +27,10 @@ public class ResponseStatusMapper {
);
}
return new ResponseStatus(statusCode, status.getMessage());
var stringDetails = status.getDetailsList().stream()
.map(t -> t.getValue().toStringUtf8())
.collect(Collectors.toList());
return new ResponseStatus(statusCode, status.getMessage(), String.join(FIELDS_DELIMITER_COMMA, stringDetails));
}
}

View file

@ -1,64 +0,0 @@
package info.frostfs.sdk.mappers.chain;
import com.google.protobuf.ByteString;
import frostfs.ape.Types;
import org.junit.jupiter.api.Test;
import java.util.Collections;
import java.util.List;
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 ChainMapperTest {
@Test
void toModels_success() {
//Given
var chain1 = Types.Chain.newBuilder()
.setRaw(ByteString.copyFrom(new byte[]{1, 2, 3, 4, 5}))
.build();
var chain2 = Types.Chain.newBuilder()
.setRaw(ByteString.copyFrom(new byte[]{6, 7, 8, 9, 10}))
.build();
//When
var result = ChainMapper.toModels(List.of(chain1, chain2));
//Then
assertNotNull(result);
assertThat(result).hasSize(2);
assertThat(result.get(0).getRaw()).containsOnly(chain1.getRaw().toByteArray());
assertThat(result.get(1).getRaw()).containsOnly(chain2.getRaw().toByteArray());
}
@Test
void toModels_null() {
//When + Then
assertNull(ChainMapper.toModels(null));
assertNull(ChainMapper.toModels(Collections.emptyList()));
}
@Test
void toModel_success() {
//Given
var chain = Types.Chain.newBuilder()
.setRaw(ByteString.copyFrom(new byte[]{1, 2, 3, 4, 5}))
.build();
//When
var result = ChainMapper.toModel(chain);
//Then
assertNotNull(result);
assertThat(result.getRaw()).containsOnly(chain.getRaw().toByteArray());
}
@Test
void toModel_null() {
//When + Then
assertNull(ChainMapper.toModel(null));
assertNull(ChainMapper.toModel(Types.Chain.getDefaultInstance()));
}
}

View file

@ -5,11 +5,10 @@ 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 info.frostfs.sdk.exceptions.ProcessFrostFSException;
import info.frostfs.sdk.dto.netmap.Version;
import info.frostfs.sdk.dto.object.OwnerId;
import info.frostfs.sdk.mappers.object.OwnerIdMapper;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import java.util.UUID;
@ -19,12 +18,54 @@ import static info.frostfs.sdk.constants.AttributeConst.DISABLE_HOMOMORPHIC_HASH
import static org.junit.jupiter.api.Assertions.*;
public class ContainerMapperTest {
private static final String OWNER_ID = "NVxUSpEEJzYXZZtUs18PrJTD9QZkLLNQ8S";
@Test
void toGrpcMessage_successFullMessage() {
//Given
var placementPolicy = new PlacementPolicy(new Replica[]{new Replica(3)}, true, 1);
var container = new Container(placementPolicy);
container.getAttributes().put("key1", "val1");
container.getAttributes().put(DISABLE_HOMOMORPHIC_HASHING_ATTRIBUTE, "false");
container.setVersion(new Version());
container.setOwnerId(new OwnerId(OWNER_ID));
//When
var result = ContainerMapper.toGrpcMessage(container);
//Then
assertNotNull(result);
assertEquals(container.getNonce(), asUuid(result.getNonce().toByteArray()));
assertEquals(container.getPlacementPolicy().isUnique(), result.getPlacementPolicy().getUnique());
assertEquals(
container.getPlacementPolicy().getBackupFactory(),
result.getPlacementPolicy().getContainerBackupFactor()
);
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()
);
assertEquals("key1", result.getAttributes(0).getKey());
assertEquals("val1", result.getAttributes(0).getValue());
assertEquals(DISABLE_HOMOMORPHIC_HASHING_ATTRIBUTE, result.getAttributes(1).getKey());
assertEquals("false", result.getAttributes(1).getValue());
assertEquals(container.getVersion().getMajor(), result.getVersion().getMajor());
assertEquals(container.getVersion().getMinor(), result.getVersion().getMinor());
assertEquals(container.getOwnerId().toString(), OwnerIdMapper.toModel(result.getOwnerId()).toString());
}
@Test
void toGrpcMessage_success() {
//Given
var placementPolicy = new PlacementPolicy(new Replica[]{new Replica(1)}, true);
var container = new Container(BasicAcl.PUBLIC_RW, placementPolicy);
var placementPolicy = new PlacementPolicy(new Replica[]{new Replica(3)}, true, 1);
var container = new Container(placementPolicy);
container.getAttributes().put("key1", "val1");
container.getAttributes().put(DISABLE_HOMOMORPHIC_HASHING_ATTRIBUTE, "false");
@ -33,9 +74,12 @@ public class ContainerMapperTest {
//Then
assertNotNull(result);
assertEquals(container.getBasicAcl().value, result.getBasicAcl());
assertEquals(container.getNonce(), asUuid(result.getNonce().toByteArray()));
assertEquals(container.getPlacementPolicy().isUnique(), result.getPlacementPolicy().getUnique());
assertEquals(
container.getPlacementPolicy().getBackupFactory(),
result.getPlacementPolicy().getContainerBackupFactor()
);
assertEquals(placementPolicy.getReplicas().length, result.getPlacementPolicy().getReplicasCount());
assertEquals(
container.getPlacementPolicy().getReplicas()[0].getCount(),
@ -58,9 +102,8 @@ public class ContainerMapperTest {
assertNull(ContainerMapper.toGrpcMessage(null));
}
@ParameterizedTest
@EnumSource(value = BasicAcl.class)
void toModel_success(BasicAcl basicAcl) {
@Test
void toModel_success() {
//Given
var version = frostfs.refs.Types.Version.newBuilder()
.setMajor(1)
@ -75,6 +118,7 @@ public class ContainerMapperTest {
var placementPolicy = frostfs.netmap.Types.PlacementPolicy.newBuilder()
.setUnique(true)
.addReplicas(replica)
.setContainerBackupFactor(2)
.build();
var attribute1 = Types.Container.Attribute.newBuilder()
@ -88,7 +132,6 @@ public class ContainerMapperTest {
.build();
var container = Types.Container.newBuilder()
.setBasicAcl(basicAcl.value)
.setNonce(ByteString.copyFrom(asBytes(UUID.randomUUID())))
.setVersion(version)
.setPlacementPolicy(placementPolicy)
@ -101,9 +144,12 @@ public class ContainerMapperTest {
//Then
assertNotNull(result);
assertEquals(container.getBasicAcl(), result.getBasicAcl().value);
assertEquals(asUuid(container.getNonce().toByteArray()), result.getNonce());
assertEquals(container.getPlacementPolicy().getUnique(), result.getPlacementPolicy().isUnique());
assertEquals(
container.getPlacementPolicy().getContainerBackupFactor(),
result.getPlacementPolicy().getBackupFactory()
);
assertEquals(placementPolicy.getReplicasCount(), result.getPlacementPolicy().getReplicas().length);
assertEquals(
container.getPlacementPolicy().getReplicasList().get(0).getCount(),
@ -126,15 +172,4 @@ public class ContainerMapperTest {
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(ProcessFrostFSException.class, () -> ContainerMapper.toModel(container));
}
}

View file

@ -0,0 +1,209 @@
package info.frostfs.sdk.mappers.netmap;
import frostfs.netmap.Types;
import info.frostfs.sdk.dto.netmap.Filter;
import info.frostfs.sdk.enums.FilterOperation;
import info.frostfs.sdk.exceptions.ProcessFrostFSException;
import info.frostfs.sdk.exceptions.ValidationFrostFSException;
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.Collections;
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 FilterMapperTest {
@ParameterizedTest
@EnumSource(value = FilterOperation.class)
void toGrpcMessages_success(FilterOperation operation) {
//Given
var filter1 = new Filter("name1", "key1", operation, "value1", null);
var filter2 = new Filter("name2", "key2", operation, "value2", null);
//When
var result = FilterMapper.toGrpcMessages(new Filter[]{filter1, filter2});
//Then
assertThat(result).isNotNull().hasSize(2);
assertEquals(filter1.getKey(), result.get(0).getKey());
assertEquals(filter1.getName(), result.get(0).getName());
assertEquals(filter1.getOperation().value, result.get(0).getOpValue());
assertEquals(filter1.getValue(), result.get(0).getValue());
assertEquals(0, result.get(0).getFiltersCount());
assertEquals(filter2.getKey(), result.get(1).getKey());
assertEquals(filter2.getName(), result.get(1).getName());
assertEquals(filter2.getOperation().value, result.get(1).getOpValue());
assertEquals(filter2.getValue(), result.get(1).getValue());
assertEquals(0, result.get(1).getFiltersCount());
}
@Test
void toGrpcMessages_null() {
//When + Then
assertEquals(Collections.emptyList(), FilterMapper.toGrpcMessages(null));
assertEquals(Collections.emptyList(), FilterMapper.toGrpcMessages(new Filter[]{}));
}
@ParameterizedTest
@EnumSource(value = FilterOperation.class)
void toGrpcMessage_success(FilterOperation operation) {
//Given
var filterChild = new Filter("name1", "key1", operation, "value1", null);
var filterParent = new Filter("name2", "key2", operation, "value2", new Filter[]{filterChild});
//When
var result = FilterMapper.toGrpcMessage(filterParent);
//Then
assertNotNull(result);
assertEquals(filterParent.getKey(), result.getKey());
assertEquals(filterParent.getName(), result.getName());
assertEquals(filterParent.getOperation().value, result.getOpValue());
assertEquals(filterParent.getValue(), result.getValue());
assertEquals(filterParent.getFilters().length, result.getFiltersCount());
var filterChildGrpc = result.getFilters(0);
assertEquals(filterChild.getKey(), filterChildGrpc.getKey());
assertEquals(filterChild.getName(), filterChildGrpc.getName());
assertEquals(filterChild.getOperation().value, filterChildGrpc.getOpValue());
assertEquals(filterChild.getValue(), filterChildGrpc.getValue());
assertEquals(0, filterChildGrpc.getFiltersCount());
}
@Test
void toGrpcMessage_null() {
//When + Then
assertThrows(ValidationFrostFSException.class, () -> FilterMapper.toGrpcMessage(null));
}
@Test
void toGrpcMessage_notValidOperation() {
//Given
var filter = new Filter("name1", "key1", FilterOperation.EQ, "value1", null);
//When + Then
try (MockedStatic<Types.Operation> mockStatic = mockStatic(Types.Operation.class)) {
mockStatic.when(() -> Types.Operation.forNumber(filter.getOperation().value))
.thenReturn(null);
assertThrows(ProcessFrostFSException.class, () -> FilterMapper.toGrpcMessage(filter));
}
}
@ParameterizedTest
@EnumSource(value = Types.Operation.class, names = "UNRECOGNIZED", mode = EnumSource.Mode.EXCLUDE)
void toModels_success(Types.Operation operation) {
//Given
var filter1 = Types.Filter.newBuilder()
.setName("name1")
.setKey("key1")
.setOp(operation)
.setValue("value1")
.build();
var filter2 = Types.Filter.newBuilder()
.setName("name2")
.setKey("key2")
.setOp(operation)
.setValue("value2")
.build();
//When
var result = FilterMapper.toModels(List.of(filter1, filter2));
//Then
assertThat(result).isNotNull().hasSize(2);
assertNotNull(result);
assertEquals(filter1.getKey(), result[0].getKey());
assertEquals(filter1.getName(), result[0].getName());
assertEquals(filter1.getOpValue(), result[0].getOperation().value);
assertEquals(filter1.getValue(), result[0].getValue());
assertNull(result[0].getFilters());
assertEquals(filter2.getKey(), result[1].getKey());
assertEquals(filter2.getName(), result[1].getName());
assertEquals(filter2.getOpValue(), result[1].getOperation().value);
assertEquals(filter2.getValue(), result[1].getValue());
assertNull(result[1].getFilters());
}
@Test
void toModels_null() {
//When + Then
assertNull(FilterMapper.toModels(null));
assertNull(FilterMapper.toModels(Collections.emptyList()));
}
@ParameterizedTest
@EnumSource(value = Types.Operation.class, names = "UNRECOGNIZED", mode = EnumSource.Mode.EXCLUDE)
void toModel_success(Types.Operation operation) {
//Given
var filterChild = Types.Filter.newBuilder()
.setName("name1")
.setKey("key1")
.setOp(operation)
.setValue("value1")
.build();
var filterParent = Types.Filter.newBuilder()
.setName("name2")
.setKey("key2")
.setOp(operation)
.setValue("value2")
.addFilters(filterChild)
.build();
//When
var result = FilterMapper.toModel(filterParent);
//Then
assertNotNull(result);
assertEquals(filterParent.getKey(), result.getKey());
assertEquals(filterParent.getName(), result.getName());
assertEquals(filterParent.getOpValue(), result.getOperation().value);
assertEquals(filterParent.getValue(), result.getValue());
assertEquals(filterParent.getFiltersCount(), result.getFilters().length);
var filterChildModel = result.getFilters()[0];
assertEquals(filterChild.getKey(), filterChildModel.getKey());
assertEquals(filterChild.getName(), filterChildModel.getName());
assertEquals(filterChild.getOpValue(), filterChildModel.getOperation().value);
assertEquals(filterChild.getValue(), filterChildModel.getValue());
assertNull(filterChildModel.getFilters());
}
@Test
void toModel_null() {
//When + Then
assertNull(FilterMapper.toModel(null));
assertNull(FilterMapper.toModel(Types.Filter.getDefaultInstance()));
}
@Test
void toModel_notValidScheme() {
//Given
var filter = Types.Filter.newBuilder()
.setName("name1")
.setKey("key1")
.setOp(Types.Operation.EQ)
.setValue("value1")
.build();
//When + Then
try (MockedStatic<FilterOperation> mockStatic = mockStatic(FilterOperation.class)) {
mockStatic.when(() -> FilterOperation.get(Types.Operation.EQ.getNumber()))
.thenReturn(null);
assertThrows(ProcessFrostFSException.class, () -> FilterMapper.toModel(filter));
}
}
}

View file

@ -15,7 +15,7 @@ public class PlacementPolicyMapperTest {
var replica1 = new Replica(1, "test1");
var replica2 = new Replica(2, "test2");
var placementPolicy = new PlacementPolicy(new Replica[]{replica1, replica2}, true);
var placementPolicy = new PlacementPolicy(new Replica[]{replica1, replica2}, true, 1);
//When
var result = PlacementPolicyMapper.toGrpcMessage(placementPolicy);
@ -23,6 +23,7 @@ public class PlacementPolicyMapperTest {
//Then
assertNotNull(result);
assertEquals(placementPolicy.isUnique(), result.getUnique());
assertEquals(placementPolicy.getBackupFactory(), result.getContainerBackupFactor());
assertEquals(placementPolicy.getReplicas().length, result.getReplicasCount());
assertEquals(replica1.getCount(), result.getReplicas(0).getCount());
assertEquals(replica1.getSelector(), result.getReplicas(0).getSelector());
@ -53,6 +54,7 @@ public class PlacementPolicyMapperTest {
.setUnique(true)
.addReplicas(replica1)
.addReplicas(replica2)
.setContainerBackupFactor(1)
.build();
//When
@ -61,6 +63,7 @@ public class PlacementPolicyMapperTest {
//Then
assertNotNull(result);
assertEquals(placementPolicy.getUnique(), result.isUnique());
assertEquals(placementPolicy.getContainerBackupFactor(), result.getBackupFactory());
assertEquals(placementPolicy.getReplicasCount(), result.getReplicas().length);
assertEquals(replica1.getCount(), result.getReplicas()[0].getCount());
assertEquals(replica1.getSelector(), result.getReplicas()[0].getSelector());

View file

@ -2,6 +2,7 @@ package info.frostfs.sdk.mappers.netmap;
import frostfs.netmap.Types;
import info.frostfs.sdk.dto.netmap.Replica;
import info.frostfs.sdk.exceptions.ValidationFrostFSException;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
@ -25,7 +26,7 @@ public class ReplicaMapperTest {
@Test
void toGrpcMessage_null() {
//When + Then
assertNull(ReplicaMapper.toGrpcMessage(null));
assertThrows(ValidationFrostFSException.class, () -> ReplicaMapper.toGrpcMessage(null));
}
@Test

View file

@ -0,0 +1,191 @@
package info.frostfs.sdk.mappers.netmap;
import frostfs.netmap.Types;
import info.frostfs.sdk.dto.netmap.Selector;
import info.frostfs.sdk.enums.SelectorClause;
import info.frostfs.sdk.exceptions.ProcessFrostFSException;
import info.frostfs.sdk.exceptions.ValidationFrostFSException;
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.Collections;
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 SelectorMapperTest {
@ParameterizedTest
@EnumSource(value = SelectorClause.class)
void toGrpcMessages_success(SelectorClause clause) {
//Given
var selector1 = new Selector("name1", 1, clause, "attribute1", "filter1");
var selector2 = new Selector("name2", 2, clause, "attribute2", "filter2");
//When
var result = SelectorMapper.toGrpcMessages(new Selector[]{selector1, selector2});
//Then
assertThat(result).isNotNull().hasSize(2);
assertEquals(selector1.getName(), result.get(0).getName());
assertEquals(selector1.getCount(), result.get(0).getCount());
assertEquals(selector1.getClause().value, result.get(0).getClauseValue());
assertEquals(selector1.getAttribute(), result.get(0).getAttribute());
assertEquals(selector1.getFilter(), result.get(0).getFilter());
assertEquals(selector2.getName(), result.get(1).getName());
assertEquals(selector2.getCount(), result.get(1).getCount());
assertEquals(selector2.getClause().value, result.get(1).getClauseValue());
assertEquals(selector2.getAttribute(), result.get(1).getAttribute());
assertEquals(selector2.getFilter(), result.get(1).getFilter());
}
@Test
void toGrpcMessages_null() {
//When + Then
assertEquals(Collections.emptyList(), SelectorMapper.toGrpcMessages(null));
assertEquals(Collections.emptyList(), SelectorMapper.toGrpcMessages(new Selector[]{}));
}
@ParameterizedTest
@EnumSource(value = SelectorClause.class)
void toGrpcMessage_success(SelectorClause clause) {
//Given
var selector = new Selector("name", 1, clause, "attribute", "filter");
//When
var result = SelectorMapper.toGrpcMessage(selector);
//Then
assertNotNull(result);
assertEquals(selector.getName(), result.getName());
assertEquals(selector.getCount(), result.getCount());
assertEquals(selector.getClause().value, result.getClauseValue());
assertEquals(selector.getAttribute(), result.getAttribute());
assertEquals(selector.getFilter(), result.getFilter());
}
@Test
void toGrpcMessage_null() {
//When + Then
assertThrows(ValidationFrostFSException.class, () -> SelectorMapper.toGrpcMessage(null));
}
@Test
void toGrpcMessage_notValidOperation() {
//Given
var selector = new Selector("name", 1, SelectorClause.SAME, "attribute", "filter");
//When + Then
try (MockedStatic<Types.Clause> mockStatic = mockStatic(Types.Clause.class)) {
mockStatic.when(() -> Types.Clause.forNumber(selector.getClause().value))
.thenReturn(null);
assertThrows(ProcessFrostFSException.class, () -> SelectorMapper.toGrpcMessage(selector));
}
}
@ParameterizedTest
@EnumSource(value = Types.Clause.class, names = "UNRECOGNIZED", mode = EnumSource.Mode.EXCLUDE)
void toModels_success(Types.Clause clause) {
//Given
var selector1 = Types.Selector.newBuilder()
.setName("name1")
.setCount(1)
.setClause(clause)
.setAttribute("attribute1")
.setFilter("filter1")
.build();
var selector2 = Types.Selector.newBuilder()
.setName("name2")
.setCount(2)
.setClause(clause)
.setAttribute("attribute2")
.setFilter("filter2")
.build();
//When
var result = SelectorMapper.toModels(List.of(selector1, selector2));
//Then
assertThat(result).isNotNull().hasSize(2);
assertNotNull(result);
assertEquals(selector1.getName(), result[0].getName());
assertEquals(selector1.getCount(), result[0].getCount());
assertEquals(selector1.getClauseValue(), result[0].getClause().value);
assertEquals(selector1.getAttribute(), result[0].getAttribute());
assertEquals(selector1.getFilter(), result[0].getFilter());
assertEquals(selector2.getName(), result[1].getName());
assertEquals(selector2.getCount(), result[1].getCount());
assertEquals(selector2.getClauseValue(), result[1].getClause().value);
assertEquals(selector2.getAttribute(), result[1].getAttribute());
assertEquals(selector2.getFilter(), result[1].getFilter());
}
@Test
void toModels_null() {
//When + Then
assertNull(SelectorMapper.toModels(null));
assertNull(SelectorMapper.toModels(Collections.emptyList()));
}
@ParameterizedTest
@EnumSource(value = Types.Clause.class, names = "UNRECOGNIZED", mode = EnumSource.Mode.EXCLUDE)
void toModel_success(Types.Clause clause) {
//Given
var selector = Types.Selector.newBuilder()
.setName("name")
.setCount(1)
.setClause(clause)
.setAttribute("attribute")
.setFilter("filter")
.build();
//When
var result = SelectorMapper.toModel(selector);
//Then
assertNotNull(result);
assertEquals(selector.getName(), result.getName());
assertEquals(selector.getCount(), result.getCount());
assertEquals(selector.getClauseValue(), result.getClause().value);
assertEquals(selector.getAttribute(), result.getAttribute());
assertEquals(selector.getFilter(), result.getFilter());
}
@Test
void toModel_null() {
//When + Then
assertNull(SelectorMapper.toModel(null));
assertNull(SelectorMapper.toModel(Types.Selector.getDefaultInstance()));
}
@Test
void toModel_notValidScheme() {
//Given
var selector = Types.Selector.newBuilder()
.setName("name")
.setCount(1)
.setClause(Types.Clause.SAME)
.setAttribute("attribute")
.setFilter("filter")
.build();
//When + Then
try (MockedStatic<SelectorClause> mockStatic = mockStatic(SelectorClause.class)) {
mockStatic.when(() -> SelectorClause.get(Types.Clause.SAME.getNumber()))
.thenReturn(null);
assertThrows(ProcessFrostFSException.class, () -> SelectorMapper.toModel(selector));
}
}
}

View file

@ -2,6 +2,7 @@ package info.frostfs.sdk.mappers.object;
import frostfs.object.Types;
import info.frostfs.sdk.dto.object.ObjectAttribute;
import org.apache.commons.collections4.CollectionUtils;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
@ -33,8 +34,8 @@ public class ObjectAttributeMapperTest {
@Test
void toGrpcMessages_null() {
//When + Then
assertNull(ObjectAttributeMapper.toGrpcMessages(null));
assertNull(ObjectAttributeMapper.toGrpcMessages(Collections.emptyList()));
assertTrue(CollectionUtils.isEmpty(ObjectAttributeMapper.toGrpcMessages(null)));
assertTrue(CollectionUtils.isEmpty(ObjectAttributeMapper.toGrpcMessages(Collections.emptyList())));
}
@Test

View file

@ -1,6 +1,7 @@
package info.frostfs.sdk.mappers.object.patch;
import info.frostfs.sdk.dto.object.patch.Range;
import org.apache.commons.collections4.CollectionUtils;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
@ -32,8 +33,8 @@ public class RangeMapperTest {
@Test
void toGrpcMessages_null() {
//When + Then
assertNull(RangeMapper.toGrpcMessages(null));
assertNull(RangeMapper.toGrpcMessages(Collections.emptyList()));
assertTrue(CollectionUtils.isEmpty(RangeMapper.toGrpcMessages(null)));
assertTrue(CollectionUtils.isEmpty(RangeMapper.toGrpcMessages(Collections.emptyList())));
}
@Test

48
pom.xml
View file

@ -17,7 +17,7 @@
</modules>
<properties>
<revision>0.4.0</revision>
<revision>0.10.0</revision>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
@ -28,6 +28,7 @@
<assertj.version>3.26.3</assertj.version>
<lombok.version>1.18.34</lombok.version>
<protobuf.version>3.23.0</protobuf.version>
<neow3j.version>3.23.0</neow3j.version>
</properties>
<dependencies>
@ -48,6 +49,11 @@
<scope>provided</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.neow3j</groupId>
<artifactId>contract</artifactId>
<version>${neow3j.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
@ -128,6 +134,46 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>flatten-maven-plugin</artifactId>
<version>1.0.0</version>
<configuration>
<updatePomFile>true</updatePomFile>
</configuration>
<executions>
<execution>
<id>flatten</id>
<phase>process-resources</phase>
<goals>
<goal>flatten</goal>
</goals>
</execution>
<execution>
<id>flatten.clean</id>
<phase>clean</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>TrueCloudLab</id>
<url>https://git.frostfs.info/api/packages/TrueCloudLab/maven</url>
</repository>
</repositories>
<distributionManagement>
<repository>
<id>TrueCloudLab</id>
<url>https://git.frostfs.info/api/packages/TrueCloudLab/maven</url>
</repository>
<snapshotRepository>
<id>TrueCloudLab</id>
<url>https://git.frostfs.info/api/packages/TrueCloudLab/maven</url>
</snapshotRepository>
</distributionManagement>
</project>