You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@submarine.apache.org by li...@apache.org on 2020/09/28 15:06:06 UTC

[submarine] branch master updated: SUBMARINE-583. Add unit test for ClusterRestAPI.java

This is an automated email from the ASF dual-hosted git repository.

liuxun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/submarine.git


The following commit(s) were added to refs/heads/master by this push:
     new b88940b  SUBMARINE-583. Add unit test for ClusterRestAPI.java
b88940b is described below

commit b88940b84829f6fd4f9a1e83e4020cc71b0d3077
Author: Lisa <ae...@gmail.com>
AuthorDate: Sun Sep 27 16:47:05 2020 +0800

    SUBMARINE-583. Add unit test for ClusterRestAPI.java
    
    ### What is this PR for?
    Add unit test for ClusterRestAPI.java
    
    ### What type of PR is it?
    [Improvement]
    
    ### Todos
    * [ ] - Task
    
    ### What is the Jira issue?
    https://issues.apache.org/jira/browse/SUBMARINE-583
    
    ### How should this be tested?
    https://travis-ci.org/github/aeioulisa/submarine/builds/730509829
    
    ### Screenshots (if appropriate)
    
    ### Questions:
    * Does the licenses files need update? No
    * Is there breaking changes for older versions? No
    * Does this needs documentation? No
    
    Author: Lisa <ae...@gmail.com>
    
    Closes #412 from aeioulisa/SUBMARINE-583 and squashes the following commits:
    
    3814f08 [Lisa] update
    a510a12 [Lisa] Merge branch 'master' of https://github.com/apache/submarine into SUBMARINE-583
    e468e28 [Lisa] update
    1a541d8 [Lisa] add unit test for ClusterRestApi
    452a99b [Lisa] add unit test for ClusterRestApi
    dfa3bc1 [Lisa] update
---
 .../submarine/server/rest/ClusterRestApi.java      |   5 +
 .../submarine/server/rest/ClusterRestApiTest.java  | 175 +++++++++++++++++++++
 2 files changed, 180 insertions(+)

diff --git a/submarine-server/server-core/src/main/java/org/apache/submarine/server/rest/ClusterRestApi.java b/submarine-server/server-core/src/main/java/org/apache/submarine/server/rest/ClusterRestApi.java
index 4ce09fe..67ce12d 100644
--- a/submarine-server/server-core/src/main/java/org/apache/submarine/server/rest/ClusterRestApi.java
+++ b/submarine-server/server-core/src/main/java/org/apache/submarine/server/rest/ClusterRestApi.java
@@ -17,6 +17,7 @@
 
 package org.apache.submarine.server.rest;
 
+import com.google.common.annotations.VisibleForTesting;
 import com.google.gson.Gson;
 import org.apache.submarine.commons.cluster.ClusterServer;
 import org.apache.submarine.commons.cluster.meta.ClusterMeta;
@@ -50,6 +51,10 @@ public class ClusterRestApi {
   Gson gson = new Gson();
 
   private ClusterServer clusterServer = ClusterServer.getInstance();
+  @VisibleForTesting
+  public void setClusterServer(ClusterServer clusterServer) {
+    this.clusterServer = clusterServer;
+  }
 
   @GET
   @Path("/" + RestConstants.ADDRESS)
diff --git a/submarine-server/server-core/src/test/java/org/apache/submarine/server/rest/ClusterRestApiTest.java b/submarine-server/server-core/src/test/java/org/apache/submarine/server/rest/ClusterRestApiTest.java
new file mode 100644
index 0000000..4adedda
--- /dev/null
+++ b/submarine-server/server-core/src/test/java/org/apache/submarine/server/rest/ClusterRestApiTest.java
@@ -0,0 +1,175 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.submarine.server.rest;
+
+import com.google.gson.Gson;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonArray;
+import com.google.gson.GsonBuilder;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParser;
+import com.google.gson.internal.LinkedTreeMap;
+import org.apache.submarine.commons.cluster.meta.ClusterMeta;
+import org.apache.submarine.commons.cluster.meta.ClusterMetaType;
+import org.apache.submarine.commons.utils.SubmarineConfiguration;
+import org.apache.submarine.commons.cluster.ClusterServer;
+import org.junit.Test;
+import org.junit.BeforeClass;
+
+import javax.ws.rs.core.Response;
+import java.time.LocalDateTime;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static org.apache.submarine.commons.cluster.meta.ClusterMetaType.SERVER_META;
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class ClusterRestApiTest {
+  private static ClusterServer mockClusterServer;
+  private static ClusterRestApi clusterRestApi;
+
+  private static final GsonBuilder gsonBuilder = new GsonBuilder();
+
+  private static Gson gson = gsonBuilder.setDateFormat("yyyy-MM-dd HH:mm:ss").create();
+
+  private static HashMap<String, HashMap<String, Object>> clusterMetas = new HashMap<>();
+
+  private static HashMap<String, Object> meta1 = new HashMap<>();
+  private static HashMap<String, Object> meta2 = new HashMap<>();
+  private static String nodeName1 = "dummy";
+  private static LocalDateTime SERVER_START_TIME1 = LocalDateTime.now();
+  private static LocalDateTime INTP_START_TIME = LocalDateTime.now();
+  private static LocalDateTime LATEST_HEARTBEAT = LocalDateTime.now();
+  private static long cpuUsed1 = 20;
+  private static long cpuCapacity1 = 40;
+  private static long memoryUsed1 = 536870912;
+  private static long memoryCapacity1 = 1073741824;
+  private static LocalDateTime SERVER_START_TIME2 = LocalDateTime.now();
+  private static long cpuUsed2 = 25;
+  private static long cpuCapacity2 = 40;
+  private static long memoryUsed2 = 268435456;
+  private static long memoryCapacity2 = 1073741824;
+  private static String nodeName2 = "dummydummy";
+
+  @BeforeClass
+  public static void init() {
+    mockClusterServer = mock(ClusterServer.class);
+    clusterRestApi = new ClusterRestApi();
+    clusterRestApi.setClusterServer(mockClusterServer);
+
+    meta1.put(ClusterMeta.NODE_NAME, nodeName1);
+    meta1.put(ClusterMeta.SERVER_START_TIME, SERVER_START_TIME1);
+    meta1.put(ClusterMeta.CPU_USED, cpuUsed1);
+    meta1.put(ClusterMeta.CPU_CAPACITY, cpuCapacity1);
+    meta1.put(ClusterMeta.MEMORY_USED, memoryUsed1);
+    meta1.put(ClusterMeta.MEMORY_CAPACITY, memoryCapacity1);
+    meta1.put(ClusterMeta.INTP_START_TIME, INTP_START_TIME);
+    meta1.put(ClusterMeta.LATEST_HEARTBEAT, LATEST_HEARTBEAT);
+    meta1.put(ClusterMeta.STATUS, ClusterMeta.ONLINE_STATUS);
+
+    meta2.put(ClusterMeta.NODE_NAME, nodeName2);
+    meta2.put(ClusterMeta.SERVER_START_TIME, SERVER_START_TIME2);
+    meta2.put(ClusterMeta.CPU_USED, cpuUsed2);
+    meta2.put(ClusterMeta.CPU_CAPACITY, cpuCapacity2);
+    meta2.put(ClusterMeta.MEMORY_USED, memoryUsed2);
+    meta2.put(ClusterMeta.MEMORY_CAPACITY, memoryCapacity2);
+    meta2.put(ClusterMeta.STATUS, ClusterMeta.OFFLINE_STATUS);
+
+    clusterMetas.put(nodeName1, meta1);
+    clusterMetas.put(nodeName2, meta2);
+    mockClusterServer.putClusterMeta(SERVER_META, nodeName1, meta1);
+    mockClusterServer.putClusterMeta(SERVER_META, nodeName2, meta2);
+  }
+
+  @Test
+  public void testGetClusterAddress() {
+    SubmarineConfiguration conf = SubmarineConfiguration.getInstance();
+    String addr1 = "127.0.0.1:4569";
+    String addr2 = "127.0.0.1:8888";
+    conf.setClusterAddress(addr1 + "," + addr2);
+
+    Response response = clusterRestApi.getClusterAddress();
+    List<String> result = getResultListFromResponse(response, String.class);
+    assertEquals(addr1, result.get(0));
+    assertEquals(addr2, result.get(1));
+  }
+
+  @Test
+  public void testGetClusterNodes() {
+    when(mockClusterServer.getClusterMeta(ClusterMetaType.SERVER_META, "")).thenReturn(clusterMetas);
+    Response response = clusterRestApi.getClusterNodes();
+    ArrayList<HashMap<String, Object>> result = getResultListFromResponse(response);
+    Map<String, Object> properties1 = (LinkedTreeMap) result.get(0).get(ClusterMeta.PROPERTIES);
+    Map<String, Object> properties2 = (LinkedTreeMap) result.get(1).get(ClusterMeta.PROPERTIES);
+
+    assertEquals(nodeName1, result.get(0).get(ClusterMeta.NODE_NAME));
+    assertEquals("ONLINE", properties1.get("STATUS"));
+    assertEquals("0.50GB / 1.00GB = 50.00%", properties1.get("MEMORY_USED / MEMORY_CAPACITY"));
+    assertEquals("0.20 / 0.40 = 50.00%", properties1.get("CPU_USED / CPU_CAPACITY"));
+
+    assertEquals(nodeName2, result.get(1).get(ClusterMeta.NODE_NAME));
+    assertEquals("OFFLINE", properties2.get("STATUS"));
+    assertEquals("0.25GB / 1.00GB = 25.00%", properties2.get("MEMORY_USED / MEMORY_CAPACITY"));
+    assertEquals("0.25 / 0.40 = 62.50%", properties2.get("CPU_USED / CPU_CAPACITY"));
+  }
+
+  @Test
+  public void testGetClusterNode() {
+    when(mockClusterServer.getClusterMeta(ClusterMetaType.INTP_PROCESS_META, "")).thenReturn(clusterMetas);
+    Response response = clusterRestApi.getClusterNode(nodeName1, "");
+    ArrayList<HashMap<String, Object>> result = getResultListFromResponse(response);
+    Map<String, Object> properties = (LinkedTreeMap) result.get(0).get(ClusterMeta.PROPERTIES);
+
+    assertEquals(clusterMetas.get(nodeName1).get(ClusterMeta.NODE_NAME),
+        result.get(0).get(ClusterMeta.NODE_NAME));
+    assertEquals("ONLINE", properties.get("STATUS"));
+    assertEquals(INTP_START_TIME.toString(), properties.get("INTP_START_TIME"));
+    assertEquals(LATEST_HEARTBEAT.toString(), properties.get("LATEST_HEARTBEAT"));
+  }
+
+  private <T> List<T> getResultListFromResponse(Response response, Class<T> typeT) {
+    String entity = (String) response.getEntity();
+    JsonObject object = new JsonParser().parse(entity).getAsJsonObject();
+    JsonElement result = object.get("result");
+    List<T> list = new ArrayList<T>();
+    JsonArray array = result.getAsJsonArray();
+    for (JsonElement jsonElement : array) {
+      list.add(gson.fromJson(jsonElement, typeT));
+    }
+    return list;
+  }
+
+  private ArrayList<HashMap<String, Object>> getResultListFromResponse(Response response) {
+    String entity = (String) response.getEntity();
+    JsonObject object = new JsonParser().parse(entity).getAsJsonObject();
+    JsonElement result = object.get("result");
+    ArrayList<HashMap<String, Object>> list = new ArrayList<>();
+    JsonArray array = result.getAsJsonArray();
+    for (JsonElement jsonElement : array) {
+      HashMap<String, Object> meta = new HashMap<>();
+      meta = gson.fromJson(jsonElement, meta.getClass());
+      list.add(meta);
+    }
+    return list;
+  }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@submarine.apache.org
For additional commands, e-mail: dev-help@submarine.apache.org