### Question:
FileChunkUtil { public static Chunk getChunk(File file, int chunkSize, int chunkNumber, String chunkId) throws IOException { if (file == null || !file.exists()) { throw new IOException("File does not exist"); } else if (chunkSize <= 0) { throw new IOException("Chunk size cannot be smaller or equal to 0"); } else if (chunkNumber < 0) { throw new IOException("Chunk number cannot be smaller than 0"); } if (FileUtil.getFileSize(file) == 0 && chunkNumber == 0) { return new Chunk(chunkId, new byte[0], 0); } int read = 0; long offset = chunkSize * (long) chunkNumber; byte[] data = new byte[chunkSize]; RandomAccessFile rndAccessFile = new RandomAccessFile(file, "r"); rndAccessFile.seek(offset); read = rndAccessFile.read(data); rndAccessFile.close(); if (read > 0) { data = truncateData(data, read); return new Chunk(chunkId, data, chunkNumber); } else { return null; } } private  FileChunkUtil(); static int getNumberOfChunks(File file, int chunkSize); static Chunk getChunk(File file, int chunkSize, int chunkNumber, String chunkId); static void reassembly(List<File> fileParts, File destination);  }### Answer:
@Test public void testGetChunkRandom() throws IOException { Random rnd = new Random(); for (int i = 0; i < 10; i++) { int genNOC = rnd.nextInt(100) + 2; File randomFile = FileTestUtil.createFileRandomContent(genNOC, parent); int chosenChunk = rnd.nextInt(genNOC - 1); Chunk chunk = FileChunkUtil.getChunk(randomFile, TestFileConfiguration.CHUNK_SIZE, chosenChunk, randomString()); assertEquals(TestFileConfiguration.CHUNK_SIZE, chunk.getSize()); assertEquals(chosenChunk, chunk.getOrder()); int lastChunkIndex = genNOC - 1; chunk = FileChunkUtil.getChunk(randomFile, TestFileConfiguration.CHUNK_SIZE, lastChunkIndex, randomString()); assertTrue(TestFileConfiguration.CHUNK_SIZE > chunk.getSize()); assertEquals(lastChunkIndex, chunk.getOrder()); randomFile.deleteOnExit(); } }
@Test public void testGetChunkEmpty() throws IOException { File file = new File(parent, randomString()); FileUtils.write(file, ""); Chunk chunk = FileChunkUtil.getChunk(file, TestFileConfiguration.CHUNK_SIZE, 0, randomString()); assertEquals(0, chunk.getSize()); assertEquals(0, chunk.getOrder()); file.deleteOnExit(); }
@Test(expected = IOException.class) public void testGetChunkNotExisting() throws IOException { File file = new File(parent, randomString()); FileChunkUtil.getChunk(file, TestFileConfiguration.CHUNK_SIZE, 0, randomString()); }
@Test(expected = IOException.class) public void testGetChunkNull() throws IOException { FileChunkUtil.getChunk(null, TestFileConfiguration.CHUNK_SIZE, 0, randomString()); }
@Test(expected = IOException.class) public void testGetChunkNegativeChunkSize() throws IOException { File file = new File(parent, randomString()); FileChunkUtil.getChunk(file, -1 * TestFileConfiguration.CHUNK_SIZE, 0, randomString()); }
@Test(expected = IOException.class) public void testGetChunkNegativeOrderNumber() throws IOException { File file = new File(parent, randomString()); FileChunkUtil.getChunk(file, TestFileConfiguration.CHUNK_SIZE, -10, randomString()); }
@Test public void testGetChunkTooHighIndex() throws IOException { File file = new File(parent, randomString()); FileUtils.write(file, "test"); Chunk chunk = FileChunkUtil.getChunk(file, TestFileConfiguration.CHUNK_SIZE, 100, randomString()); assertNull(chunk); }