@Test public void readBlockTest() throws Exception { Map<String, String> params = new HashMap<>(); params.put("blockId", "1"); params.put("sessionId", "1"); params.put("lockId", "1"); params.put("offset", "0"); params.put("length", "-1"); Random random = new Random(); byte[] bytes = CommonUtils.randomBytes(random.nextInt(64)); ByteBuffer byteBuffer = ByteBuffer.wrap(bytes); BlockReader blockReader = PowerMockito.mock(BlockReader.class); Mockito.doReturn(byteBuffer).when(blockReader).read(Mockito.anyLong(), Mockito.anyLong()); Mockito.doReturn((long) bytes.length).when(blockReader).getLength(); Mockito.doReturn(blockReader) .when(mBlockWorker) .readBlockRemote(Mockito.anyLong(), Mockito.anyLong(), Mockito.anyLong()); TestCase testCase = TestCaseFactory.newWorkerTestCase( getEndpoint(BlockWorkerClientRestServiceHandler.READ_BLOCK), params, "GET", byteBuffer, mResource); HttpURLConnection connection = (HttpURLConnection) testCase.createURL().openConnection(); connection.setRequestMethod(testCase.getMethod()); connection.connect(); Assert.assertEquals( testCase.getEndpoint(), connection.getResponseCode(), Response.Status.OK.getStatusCode()); Assert.assertEquals(new String(byteBuffer.array()), testCase.getResponse(connection)); Mockito.verify(mBlockWorker) .readBlockRemote(Mockito.anyLong(), Mockito.anyLong(), Mockito.anyLong()); }
@Test public void writeBlockTest() throws Exception { Map<String, String> params = new HashMap<>(); params.put("blockId", "1"); params.put("sessionId", "1"); params.put("offset", "0"); params.put("length", "-1"); Random random = new Random(); byte[] bytes = CommonUtils.randomBytes(random.nextInt(64)); BlockWriter blockWriter = PowerMockito.mock(BlockWriter.class); Mockito.doReturn(blockWriter) .when(mBlockWorker) .getTempBlockWriterRemote(Mockito.anyLong(), Mockito.anyLong()); TestCase testCase = TestCaseFactory.newWorkerTestCase( getEndpoint(BlockWorkerClientRestServiceHandler.WRITE_BLOCK), params, "POST", null, mResource); HttpURLConnection connection = (HttpURLConnection) testCase.createURL().openConnection(); connection.setRequestProperty("Content-Type", MediaType.APPLICATION_OCTET_STREAM); connection.setRequestMethod(testCase.getMethod()); connection.setDoOutput(true); connection.connect(); connection.getOutputStream().write(bytes); Assert.assertEquals( testCase.getEndpoint(), Response.Status.OK.getStatusCode(), connection.getResponseCode()); Assert.assertEquals("", testCase.getResponse(connection)); Mockito.verify(mBlockWorker).getTempBlockWriterRemote(Mockito.anyLong(), Mockito.anyLong()); Mockito.verify(blockWriter).append(ByteBuffer.wrap(bytes)); }