Skip to content

Commit

Permalink
fix: removed toByteArray function as it is now no longer needed
Browse files Browse the repository at this point in the history
  • Loading branch information
CRAlwin committed Sep 18, 2024
1 parent b5d99c3 commit 36badbd
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,6 @@ public static <T extends Tuple<T, F>, F extends Field> TupleList<T, F> fromStrea
}
}

public byte[] toByteArray(){
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
this.forEach(
tuple -> {
try {
tuple.writeTo(outputStream);
} catch (IOException exception) {
rethrow(exception);
}
});
return outputStream.toByteArray();
}

/**
* Converts a {@link TupleList} to {@link TupleChunk}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
import io.carbynestack.httpclient.CsHttpClient;
import io.carbynestack.httpclient.CsHttpClientException;
import io.carbynestack.httpclient.CsResponseEntity;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.util.ArrayList;
Expand Down Expand Up @@ -100,28 +100,38 @@ void givenSslConfiguration_whenBuildClient_thenInitializeCsHttpClientAccordingly

@SneakyThrows
@Test
void givenSuccessfulRequest_whenDownloadTripleSharesAsBytes_thenReturnExpectedContent(){
void givenSuccessfulRequest_whenDownloadTripleSharesAsBytes_thenReturnExpectedContent() {
UUID requestId = UUID.fromString("3dc08ff2-5eed-49a9-979e-3a3ac0e4a2cf");
int expectedCount = 2;
TupleList<MultiplicationTriple<Field.Gfp>, Field.Gfp> expectedTripleList =
new TupleList(MultiplicationTriple.class, GFP);
new TupleList(MultiplicationTriple.class, GFP);

expectedTripleList.add(new MultiplicationTriple(GFP, testShare, testShare, testShare));
expectedTripleList.add(new MultiplicationTriple(GFP, testShare, testShare, testShare));
ByteArrayOutputStream tripeListAsBytes = new ByteArrayOutputStream();
expectedTripleList.forEach(
tuple -> {
try {
tuple.writeTo(tripeListAsBytes);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
CsResponseEntity<String, byte[]> givenResponseEntity =
CsResponseEntity.success(HttpStatus.SC_OK, expectedTripleList.toByteArray());
CsResponseEntity.success(HttpStatus.SC_OK, tripeListAsBytes.toByteArray());

CastorServiceUri serviceUri = new CastorServiceUri(serviceAddress);

doReturn(givenResponseEntity)
.when(csHttpClientMock)
.getForEntity(
.when(csHttpClientMock)
.getForEntity(
serviceUri.getIntraVcpRequestTuplesUri(
requestId, TupleType.MULTIPLICATION_TRIPLE_GFP, expectedCount),
requestId, TupleType.MULTIPLICATION_TRIPLE_GFP, expectedCount),
Collections.emptyList(),
byte[].class);
TupleList actualTripleList =
castorIntraVcpClient.downloadTupleShares(
requestId, TupleType.MULTIPLICATION_TRIPLE_GFP, expectedCount);
castorIntraVcpClient.downloadTupleShares(
requestId, TupleType.MULTIPLICATION_TRIPLE_GFP, expectedCount);

assertEquals(expectedTripleList, actualTripleList);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Objects;
Expand Down Expand Up @@ -292,17 +294,25 @@ void givenRetrievingTuplesFails_whenGetTuples_thenKeepReservationAndReservationM
tuplesDownloadService.getTupleList(
tupleType.getTupleCls(), tupleType.getField(), count, requestId);

assertArrayEquals(
TupleList actualTupleList =
TupleList.fromStream(
tupleType.getTupleCls(),
tupleType.getField(),
new ByteArrayInputStream(
tupleData,
(int) (fragmentStartIndex * tupleType.getTupleSize()),
(int) ((fragmentStartIndex + count) * tupleType.getTupleSize())),
count * tupleType.getTupleSize())
.toByteArray(),
tupleList);
tupleType.getTupleCls(),
tupleType.getField(),
new ByteArrayInputStream(
tupleData,
(int) (fragmentStartIndex * tupleType.getTupleSize()),
(int) ((fragmentStartIndex + count) * tupleType.getTupleSize())),
count * tupleType.getTupleSize());
ByteArrayOutputStream actualTupleData = new ByteArrayOutputStream();
actualTupleList.forEach(
tuple -> {
try {
((Tuple<?, ?>) tuple).writeTo(actualTupleData);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
assertArrayEquals(actualTupleData.toByteArray(), tupleList);

// no fragments stored -> existing fragment was reserved, consumed and then deleted
assertFalse(tupleChunkFragmentRepository.findAll().iterator().hasNext());
Expand Down

0 comments on commit 36badbd

Please sign in to comment.