SetCommand implements CommandRunnable { @Override public ReplyMessage run(int argc, byte[][] argv) throws Exception { if (argc != 3) { return ErrorMessage.WrongArgcMessage; } String key = new String(argv[1], Protocol.KEYCAFE_CHARSET); String val = new String(argv[2], Protocol.KEYCAFE_CHARSET); kvMap.put(key, val); logger.info("set key: {} - result: {}", key, val); tMap.put(key, System.currentTimeMillis()); return StringMessage.OkMessage; } SetCommand(Map<String, String> kvMap, Map<String, Long> tMap); @Override ReplyMessage run(int argc, byte[][] argv); @Override int keyIndex();  }
@Test public void test() throws Exception { byte[][] args = new byte[2][]; args[0] = new byte[]{(byte) Command.SET.ordinal()}; args[1] = "abc".getBytes(); SetCommand s = new SetCommand(null, null); ReplyMessage reply = s.run(1, args); assertEquals(reply, ErrorMessage.WrongArgcMessage); }
@Test public void test2() throws Exception { byte[][] args = new byte[2][]; args[0] = new byte[]{(byte) Command.SET.ordinal()}; args[1] = "abc".getBytes(); SetCommand s = new SetCommand(null, null); ReplyMessage reply = s.run(2, args); assertEquals(reply, ErrorMessage.WrongArgcMessage); }
@Test public void test3() throws Exception { byte[][] args = new byte[1][]; args[0] = new byte[]{(byte) Command.SET.ordinal()}; SetCommand s = new SetCommand(null, null); ReplyMessage reply = s.run(2, args); assertEquals(reply, ErrorMessage.WrongArgcMessage); }
@Test public void test4() throws Exception { Map<String, String> map = new HashMap<>(); Map<String, Long> map2 = new HashMap<>(); byte[][] args = new byte[3][]; args[0] = new byte[]{(byte) Command.SET.ordinal()}; args[1] = "TEST".getBytes(); args[2] = "VALUE".getBytes(); SetCommand s = new SetCommand(map, map2); ReplyMessage reply = s.run(3, args); assertEquals(StringMessage.OkMessage.message(), reply.message()); }
@Test public void test5() throws Exception { Map<String, String> map = new HashMap<String, String>() { { put("TEST", "PASTVAL"); } }; Map<String, Long> map2 = new HashMap<>(); byte[][] args = new byte[3][]; args[0] = new byte[]{(byte) Command.SET.ordinal()}; args[1] = "TEST".getBytes(); args[2] = "VALUE".getBytes(); SetCommand s = new SetCommand(map, map2); ReplyMessage reply = s.run(3, args); assertEquals(StringMessage.OkMessage.message(), reply.message()); }