diff --git a/src/main/java/com/loafle/overflow/Server.java b/src/main/java/com/loafle/overflow/Server.java index 516a101..2d9146d 100644 --- a/src/main/java/com/loafle/overflow/Server.java +++ b/src/main/java/com/loafle/overflow/Server.java @@ -143,10 +143,12 @@ public class Server { Object obj = null; for (Init in : list) { Crawler crawler = crawlerMap.get(in.getName().name()); - try { - obj = crawler.init(in.getData().toByteArray()); - } catch (Exception e) { - e.printStackTrace(); + if (crawler != null) { + try { + obj = crawler.init(in.getData().toByteArray()); + } catch (Exception e) { + e.printStackTrace(); + } } } diff --git a/src/test/java/com/loafle/overflow/ServerTest.java b/src/test/java/com/loafle/overflow/ServerTest.java index cb3c513..8fa5806 100644 --- a/src/test/java/com/loafle/overflow/ServerTest.java +++ b/src/test/java/com/loafle/overflow/ServerTest.java @@ -1,5 +1,8 @@ package com.loafle.overflow; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.protobuf.ByteString; import com.loafle.overflow.crawler.Crawler; import com.loafle.overflow.crawler.config.Config; import com.loafle.overflow.rpc.*; @@ -11,6 +14,9 @@ import org.junit.Before; import org.junit.Test; import java.io.*; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.logging.Logger; import static org.junit.Assert.assertEquals; @@ -54,14 +60,16 @@ public class ServerTest { return ManagedChannelBuilder.forAddress(ip,port).usePlaintext(true).build(); } - public Output init() { + public Output init() throws IOException { ConfigGrpc.ConfigBlockingStub client = newConfigClient("localhost",50052); ClassLoader classLoader = getClass().getClassLoader(); + Path path = Paths.get(classLoader.getResource("config/example.json").getFile()); + byte[] data = Files.readAllBytes(path); + ByteString sss = ByteString.copyFrom(data); - // path is test resources/config/example.json Init input = Init.newBuilder(). - setPath(classLoader.getResource("config").getFile()). + setData(sss). setName(Crawlers.REDIS).build(); InputArray arr = InputArray.newBuilder().addIn(input).build(); @@ -89,13 +97,15 @@ public class ServerTest { ConfigGrpc.ConfigBlockingStub client = newConfigClient("localhost",50052); - ClassLoader classLoader = getClass().getClassLoader(); - // path is test resources/config/example.json - Input input = Input.newBuilder(). - setId("example.json"). - setName(Crawlers.REDIS).build(); + ClassLoader classLoader = getClass().getClassLoader(); + Path path = Paths.get(classLoader.getResource("config/example.json").getFile()); + byte[] data = Files.readAllBytes(path); + ByteString sss = ByteString.copyFrom(data); + InputAdd input = InputAdd.newBuilder(). + setData(sss). + setName(Crawlers.REDIS).build(); Output out = client.add(input); @@ -141,7 +151,7 @@ public class ServerTest { DataGrpc.DataBlockingStub client = newDataClient("localhost",50052); Input input = Input.newBuilder(). - setId("example.json"). + setId("SOEJWEOJWOEJOSDJFOASDJFOSDFO2903870928734"). setName(Crawlers.REDIS).build(); Output out = client.get(input); diff --git a/src/test/java/com/loafle/overflow/TestClient.java b/src/test/java/com/loafle/overflow/TestClient.java index 2f2d594..c255bc7 100644 --- a/src/test/java/com/loafle/overflow/TestClient.java +++ b/src/test/java/com/loafle/overflow/TestClient.java @@ -35,7 +35,7 @@ public class TestClient { /** Say hello to server. */ public void add() { - Input request = Input.newBuilder().setId("test111").setName(Crawlers.SNMP).build(); + InputAdd request = InputAdd.newBuilder().setData(null).setName(Crawlers.SNMP).build(); Output response; try { response = configStub.add(request);