ModelStorateThinClientProcessor implements CustomQueryProcessor { private ClientResponse mkdirs(long reqId, BinaryRawReader reader) { String path = reader.readString(); boolean onlyIfNotExists = reader.readBoolean(); return modelStorage.lockPaths(() -> { if (onlyIfNotExists && modelStorage.exists(path)) return error(reqId, "Directory already exists [path=" + path + "]"); modelStorage.mkdirs(path); return new ClientResponse(reqId); }, path); } ModelStorateThinClientProcessor(ModelStorage modelStorage); @Override ClientResponse call(long requestId, byte methodId, BinaryRawReader reader); @Override String id(); static final String PROCESSOR_ID; }
@Test public void testRemoveEmptyDirectory() { long reqId = 13; ms.mkdirs("/a/b/test_dir"); BinaryRawWriterEx message = createMessage(reqId, ModelStorateThinClientProcessor.PROCESSOR_ID, ModelStorateThinClientProcessor.Method.REMOVE); message.writeString("/a/b/test_dir"); ClientCustomQueryRequest req = new ClientCustomQueryRequest(toReader(message)); ClientResponse resp = req.process(connCtx); BinaryRawWriterEx out = createWriter(); resp.encode(connCtx, out); byte[] result = out.out().arrayCopy(); assertFalse(ms.exists("/a/b/test_dir")); assertTrue(ms.exists("/a/b")); assertArrayEquals(getExpectedMessageHeader(reqId).out().arrayCopy(), result); }
@Test public void testRepeatedMkDir2() { long reqId = 15; String path = "/mk/dirs/test"; ms.mkdirs(path); BinaryRawWriterEx message = createMessage(reqId, ModelStorateThinClientProcessor.PROCESSOR_ID, ModelStorateThinClientProcessor.Method.MKDIR); message.writeString(path); message.writeBoolean(false); ClientCustomQueryRequest req = new ClientCustomQueryRequest(toReader(message)); ClientResponse resp = req.process(connCtx); BinaryRawWriterEx out = createWriter(); resp.encode(connCtx, out); byte[] result = out.out().arrayCopy(); assertArrayEquals(getExpectedMessageHeader(reqId).out().arrayCopy(), result); }
@Test public void testListFiles1() { long reqId = 16; String path = "/list/files/test"; ms.mkdirs(path); String file1path = path + "/a"; String file2path = path + "/b"; ms.putFile(file1path, file1); ms.putFile(file2path, file1); BinaryRawWriterEx message = createMessage(reqId, ModelStorateThinClientProcessor.PROCESSOR_ID, ModelStorateThinClientProcessor.Method.LIST_FILES); message.writeString(path); ClientCustomQueryRequest req = new ClientCustomQueryRequest(toReader(message)); ClientResponse resp = req.process(connCtx); assertTrue(((FilesListResponse)resp).getFilesList().containsAll(Arrays.asList(file1path, file2path))); BinaryRawWriterEx out = createWriter(); resp.encode(connCtx, out); byte[] result = out.out().arrayCopy(); BinaryRawWriterEx expectedOut = getExpectedMessageHeader(reqId); expectedOut.writeInt(2); expectedOut.writeString(file1path); expectedOut.writeString(file2path); byte[] expected = expectedOut.out().arrayCopy(); assertArrayEquals(expected, result); }