@Test public void lockBlockTest() throws Exception { Map<String, String> params = new HashMap<>(); params.put("blockId", "1"); params.put("sessionId", "1"); LockBlockResult lockBlockResult = LockBlockResultTest.createRandom(); Mockito.doReturn(lockBlockResult.getLockId()) .when(mBlockWorker) .lockBlock(Mockito.anyLong(), Mockito.anyLong()); Mockito.doReturn(lockBlockResult.getBlockPath()) .when(mBlockWorker) .readBlock(Mockito.anyLong(), Mockito.anyLong(), Mockito.anyLong()); TestCaseFactory.newWorkerTestCase( getEndpoint(BlockWorkerClientRestServiceHandler.LOCK_BLOCK), params, "POST", lockBlockResult, mResource) .run(); Mockito.verify(mBlockWorker).lockBlock(Mockito.anyLong(), Mockito.anyLong()); Mockito.verify(mBlockWorker).readBlock(Mockito.anyLong(), Mockito.anyLong(), Mockito.anyLong()); }
@Test public void serviceVersionTest() throws Exception { TestCaseFactory.newWorkerTestCase( getEndpoint(BlockWorkerClientRestServiceHandler.SERVICE_VERSION), NO_PARAMS, "GET", Constants.BLOCK_WORKER_CLIENT_SERVICE_VERSION, mResource) .run(); }
@Test public void asyncCheckpointTest() throws Exception { Map<String, String> params = new HashMap<>(); params.put("fileId", "1"); TestCaseFactory.newWorkerTestCase( getEndpoint(BlockWorkerClientRestServiceHandler.ASYNC_CHECKPOINT), params, "POST", false, mResource) .run(); }
@Test public void accessBlockTest() throws Exception { Map<String, String> params = new HashMap<>(); params.put("blockId", "1"); TestCaseFactory.newWorkerTestCase( getEndpoint(BlockWorkerClientRestServiceHandler.ACCESS_BLOCK), params, "POST", null, mResource) .run(); Mockito.verify(mBlockWorker).accessBlock(Mockito.anyLong(), Mockito.anyLong()); }
@Test public void requestSpaceTest() throws Exception { Map<String, String> params = new HashMap<>(); params.put("blockId", "1"); params.put("sessionId", "1"); params.put("requestBytes", "1"); TestCaseFactory.newWorkerTestCase( getEndpoint(BlockWorkerClientRestServiceHandler.REQUEST_SPACE), params, "POST", null, mResource) .run(); Mockito.verify(mBlockWorker) .requestSpace(Mockito.anyLong(), Mockito.anyLong(), Mockito.anyLong()); }
@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 requestBlockLocationTest() throws Exception { Map<String, String> params = new HashMap<>(); params.put("blockId", "1"); params.put("sessionId", "1"); params.put("initialBytes", "1"); String blockLocation = CommonUtils.randomString(10); Mockito.doReturn(blockLocation) .when(mBlockWorker) .createBlock(Mockito.anyLong(), Mockito.anyLong(), Mockito.anyString(), Mockito.anyLong()); TestCaseFactory.newWorkerTestCase( getEndpoint(BlockWorkerClientRestServiceHandler.REQUEST_BLOCK_LOCATION), params, "POST", blockLocation, mResource) .run(); Mockito.verify(mBlockWorker) .createBlock(Mockito.anyLong(), Mockito.anyLong(), Mockito.anyString(), 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)); }