// Path: copybook4java/src/main/java/com/nordea/oss/ByteUtils.java
// public class ByteUtils {
// 
//     public static boolean allEquals(byte[] array, byte value, int offset, int maxLength) {
//         maxLength = Math.min(offset + maxLength, array.length);
//         for(int i = offset; i < maxLength; i++) {
//             if(array[i] != value) {
//                 return false;
//             }
//         }
//         return true;
//     }
// 
//     public static int indexOf(byte[] array, byte needle, int offset, int maxLength) {
//         int result = -1;
//         maxLength = Math.min(offset + maxLength, array.length);
//         for(int i = offset; i < maxLength; i++) {
//             if(array[i] == needle) {
//                 result = i;
//                 break;
//             }
//         }
//         return result;
//     }
// 
//     public static byte[] trim(byte[] src, byte padding, boolean right, int minLength) {
//         if(src.length < minLength) {
//             throw new RuntimeException("src array is smaller than minLength: " + src.length + " < " + minLength);
//         }
//         if(right) {
//             int offset;
//             for (offset = src.length - 1; offset > minLength - 1; offset--) {  // [44, 32]
//                 if(padding != src[offset]) {
//                     break;
//                 }
//             }
//             if(offset < 0) {
//                 return new byte[0];
//             } else if (offset < src.length - 1){
//                 return Arrays.copyOfRange(src, 0, offset + 1);
//             }
// 
//         } else {
//             int offset;
//             for (offset = 0; offset < src.length - minLength; offset++) {
//                 if(padding != src[offset]) {
//                     break;
//                 }
//             }
//             if(offset == src.length) {
//                 return new byte[0];
//             } else if(offset > 0) {
//                 return Arrays.copyOfRange(src, offset, src.length);
//             }
//         }
// 
//         return src;
//     }
// 
//     public static byte[] toByteArray(InputStream inputStream) throws IOException {
//         ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
// 
//         byte[] buf = new byte[8192];
//         while(true) {
//             int r = inputStream.read(buf);
//             if(r == -1) {
//                 break;
//             }
//             outputStream.write(buf, 0, r);
//         }
// 
//         return outputStream.toByteArray();
//     }
// }


import com.nordea.oss.ByteUtils;
import org.junit.Rule;
import org.junit.rules.ExpectedException;
import javax.script.ScriptException;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import static org.junit.Assert.assertEquals;
        InputStream rootObjSample = this.getClass().getResourceAsStream("../../../../../rootobj.txt");
        convertedJavaSources.addAll(converter.convert(rootObjSample, "mypackage", "RootObj", "none", "UTF-8", "nested", null));

        List<String> errors = CompileGeneratedSource(convertedJavaSources);
        assertEquals(0, errors.size());
    }

    @org.junit.Test
    public void testBasicConversionWithError() throws Exception {
        expectedEx.expect(ScriptException.class);
        expectedEx.expectMessage("Could not parse line");
        String sample = "05 ERROR PIC 9(5) OCCURSZ 6.";
        CopyBookConverter converter = new CopyBookConverter();
        List<String> result = converter.convert(sample, "mypackage", "MyHospital", "none", "UTF-8", "nested", null);
    }


    @org.junit.Test
    public void testMultiFileConversion() throws Exception {
        File basePath = new File(this.getClass().getResource("../../../../").toURI());
        CopyBookConverter converter = new CopyBookConverter();
        converter.convertFiles(basePath.getAbsolutePath(), Pattern.compile("\\.txt$"), basePath.getAbsolutePath(), "mypackage", "none", "UTF-8", "nested");
    }

    private List<String> CompileGeneratedSource(List<String> convertedJavaSources) throws Exception {
        File annotationsFolder = new File(this.getClass().getResource(".").getFile() + "../../../../../../../../copybook4java/src/main/java/com/nordea/oss/copybook/annotations");

        List<String> sources = new ArrayList<>();
        for(File file : annotationsFolder.listFiles()) {
            if(file.isFile() && file.getName().endsWith(".java")) {
// Path: copybook4java/src/main/java/com/nordea/oss/ByteUtils.java
// public class ByteUtils {
// 
//     public static boolean allEquals(byte[] array, byte value, int offset, int maxLength) {
//         maxLength = Math.min(offset + maxLength, array.length);
//         for(int i = offset; i < maxLength; i++) {
//             if(array[i] != value) {
//                 return false;
//             }
//         }
//         return true;
//     }
// 
//     public static int indexOf(byte[] array, byte needle, int offset, int maxLength) {
//         int result = -1;
//         maxLength = Math.min(offset + maxLength, array.length);
//         for(int i = offset; i < maxLength; i++) {
//             if(array[i] == needle) {
//                 result = i;
//                 break;
//             }
//         }
//         return result;
//     }
// 
//     public static byte[] trim(byte[] src, byte padding, boolean right, int minLength) {
//         if(src.length < minLength) {
//             throw new RuntimeException("src array is smaller than minLength: " + src.length + " < " + minLength);
//         }
//         if(right) {
//             int offset;
//             for (offset = src.length - 1; offset > minLength - 1; offset--) {  // [44, 32]
//                 if(padding != src[offset]) {
//                     break;
//                 }
//             }
//             if(offset < 0) {
//                 return new byte[0];
//             } else if (offset < src.length - 1){
//                 return Arrays.copyOfRange(src, 0, offset + 1);
//             }
// 
//         } else {
//             int offset;
//             for (offset = 0; offset < src.length - minLength; offset++) {
//                 if(padding != src[offset]) {
//                     break;
//                 }
//             }
//             if(offset == src.length) {
//                 return new byte[0];
//             } else if(offset > 0) {
//                 return Arrays.copyOfRange(src, offset, src.length);
//             }
//         }
// 
//         return src;
//     }
// 
//     public static byte[] toByteArray(InputStream inputStream) throws IOException {
//         ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
// 
//         byte[] buf = new byte[8192];
//         while(true) {
//             int r = inputStream.read(buf);
//             if(r == -1) {
//                 break;
//             }
//             outputStream.write(buf, 0, r);
//         }
// 
//         return outputStream.toByteArray();
//     }
// }


// Path: copybook4java-codegen-maven-plugin/src/test/java/com/nordea/oss/copybook/codegen/CopyBookConverterTest.java
import com.nordea.oss.ByteUtils;
import org.junit.Rule;
import org.junit.rules.ExpectedException;
import javax.script.ScriptException;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import static org.junit.Assert.assertEquals;

        InputStream rootObjSample = this.getClass().getResourceAsStream("../../../../../rootobj.txt");
        convertedJavaSources.addAll(converter.convert(rootObjSample, "mypackage", "RootObj", "none", "UTF-8", "nested", null));

        List<String> errors = CompileGeneratedSource(convertedJavaSources);
        assertEquals(0, errors.size());
    }

    @org.junit.Test
    public void testBasicConversionWithError() throws Exception {
        expectedEx.expect(ScriptException.class);
        expectedEx.expectMessage("Could not parse line");
        String sample = "05 ERROR PIC 9(5) OCCURSZ 6.";
        CopyBookConverter converter = new CopyBookConverter();
        List<String> result = converter.convert(sample, "mypackage", "MyHospital", "none", "UTF-8", "nested", null);
    }


    @org.junit.Test
    public void testMultiFileConversion() throws Exception {
        File basePath = new File(this.getClass().getResource("../../../../").toURI());
        CopyBookConverter converter = new CopyBookConverter();
        converter.convertFiles(basePath.getAbsolutePath(), Pattern.compile("\\.txt$"), basePath.getAbsolutePath(), "mypackage", "none", "UTF-8", "nested");
    }

    private List<String> CompileGeneratedSource(List<String> convertedJavaSources) throws Exception {
        File annotationsFolder = new File(this.getClass().getResource(".").getFile() + "../../../../../../../../copybook4java/src/main/java/com/nordea/oss/copybook/annotations");

        List<String> sources = new ArrayList<>();
        for(File file : annotationsFolder.listFiles()) {
            if(file.isFile() && file.getName().endsWith(".java")) {
                sources.add(new String(ByteUtils.toByteArray(new FileInputStream(file)), StandardCharsets.UTF_8));