package com.kryptnostic.storage.v1.models.utils;

import java.util.List;

import org.apache.commons.codec.binary.StringUtils;
import org.junit.Assert;
import org.junit.Test;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.kryptnostic.kodex.v1.exceptions.types.SecurityConfigurationException;
import com.kryptnostic.kodex.v1.models.AesEncryptable;
import com.kryptnostic.kodex.v1.models.utils.AesEncryptableUtils;
import com.kryptnostic.storage.v1.models.request.AesEncryptableBase;

public class AesEncryptableUtilsTests extends AesEncryptableBase {

    @Test
    public void shortStringChunkTest() throws JsonProcessingException, SecurityConfigurationException {
        initImplicitEncryption();
        List<AesEncryptable<String>> results = AesEncryptableUtils.chunkString("cool", config);

        Assert.assertEquals(1, results.size());
    }

    @Test
    public void longStringChunkTest() throws JsonProcessingException, SecurityConfigurationException {
        initImplicitEncryption();
        String str = StringUtils.newStringUtf8(fillByteArray(4096));
        List<AesEncryptable<String>> results = AesEncryptableUtils.chunkString(str, config);

        Assert.assertEquals(1, results.size());

        str = StringUtils.newStringUtf8(fillByteArray(4096 + 1));

        results = AesEncryptableUtils.chunkString(str, config);

        Assert.assertEquals(2, results.size());
    }

    private byte[] fillByteArray(int sz) {
        byte[] str = new byte[sz];

        for (int i = 0; i < sz; i++) {
            str[i] = 42;
        }

        return str;
    }

}

@@ -43,16 +43,44 @@
 utils;%0A%0A
+import java.io.IOException;%0A
 import j
@@ -247,32 +247,115 @@
 ssingException;%0A
+import com.google.common.base.Charsets;%0Aimport com.google.common.primitives.Chars;%0A
 import com.krypt
@@ -708,272 +708,1401 @@
 oid 
-shortStringChunkTest() throws JsonProcessingException, SecurityConfigurationException %7B%0A        initImplicitEncryption();%0A        List%3CAesEncryptable%3CString%3E%3E results = AesEncryptableUtils.chunkString(%22cool%22, config);%0A%0A        Assert.assertEquals(1, results.size()
+platformAssumptionsTest() throws JsonProcessingException, SecurityConfigurationException %7B%0A        initImplicitEncryption();%0A%0A        final int PADDING = 2;%0A        %0A        AesEncryptable%3CString%3E enc = new AesEncryptable%3CString%3E(StringUtils.newStringUtf16(%22%22.getBytes(Charsets.UTF_16)));%0A        Assert.assertEquals(0 + PADDING, enc.encrypt(config).getEncryptedData().getContents().length);%0A        %0A        enc = new AesEncryptable%3CString%3E(StringUtils.newStringUtf16(%22a%22.getBytes(Charsets.UTF_16)));%0A        Assert.assertEquals(2 + PADDING, enc.encrypt(config).getEncryptedData().getContents().length);%0A        %0A        enc = new AesEncryptable%3CString%3E(StringUtils.newStringUtf16(%22ab%22.getBytes(Charsets.UTF_16)));%0A        Assert.assertEquals(4 + PADDING, enc.encrypt(config).getEncryptedData().getContents().length);%0A    %7D%0A%0A    @Test%0A    public void shortStringChunkTest() throws SecurityConfigurationException, IOException, ClassNotFoundException %7B%0A        initImplicitEncryption();%0A        List%3CAesEncryptable%3CString%3E%3E results = AesEncryptableUtils.chunkString(%22cool%22, config);%0A%0A        Assert.assertEquals(1, results.size());%0A        Assert.assertTrue(results.get(0).isEncrypted());%0A        Assert.assertEquals(4 * Chars.BYTES, results.get(0).getEncryptedData().getContents().length);%0A        Assert.assertEquals(4 * Chars.BYTES, results.get(0).decrypt(config).getData().getBytes().length
 );%0A 

add platform tests for encryption