Ethanlm commented on a change in pull request #3312:
URL: https://github.com/apache/storm/pull/3312#discussion_r467261976



##########
File path: 
storm-webapp/src/test/java/org/apache/storm/daemon/ui/UIHelpersTest.java
##########
@@ -0,0 +1,543 @@
+/*
+ * 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.storm.daemon.ui;
+
+import org.apache.storm.Config;
+import org.apache.storm.Constants;
+import org.apache.storm.generated.BoltAggregateStats;
+import org.apache.storm.generated.CommonAggregateStats;
+import org.apache.storm.generated.ComponentAggregateStats;
+import org.apache.storm.generated.ErrorInfo;
+import org.apache.storm.generated.SpecificAggregateStats;
+import org.apache.storm.generated.SpoutAggregateStats;
+import org.apache.storm.generated.TopologyPageInfo;
+import org.apache.storm.generated.TopologyStats;
+import org.json.simple.JSONValue;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class UIHelpersTest {
+    private static final String TOPOLOGY_ID = "Test-Topology-Id";
+    private static final long TOPOLOGY_MESSAGE_TIMEOUT_SECS = 100L;
+    private static final String WINDOW = ":all-time";
+
+    /**
+     * Default empty TopologyPageInfo instance to be extended in each test 
case.
+     */
+    private TopologyPageInfo topoPageInfo;
+
+    /**
+     * Setups up bare minimum TopologyPageInfo instance such that we can pass 
to
+     * UIHelpers.getTopologySummary() without it throwing a NPE.
+     *
+     * This should provide a base for which other tests can be written, but 
will
+     * require populating additional values as needed for each test case.
+     */
+    @BeforeEach
+    void setup() {
+        // Create topology config and serialize to JSON.
+        final Map<String, Object> topologyConfig = new HashMap<>();
+        topologyConfig.put(Config.TOPOLOGY_MESSAGE_TIMEOUT_SECS, 
TOPOLOGY_MESSAGE_TIMEOUT_SECS);
+        final String topoConfigJson = JSONValue.toJSONString(topologyConfig);
+
+        // Create empty TopologyStats instance
+        final TopologyStats topologyStats = new TopologyStats();
+        topologyStats.set_window_to_emitted(new HashMap<>());
+        topologyStats.set_window_to_transferred(new HashMap<>());
+        topologyStats.set_window_to_acked(new HashMap<>());
+        topologyStats.set_window_to_complete_latencies_ms(new HashMap<>());
+        topologyStats.set_window_to_failed(new HashMap<>());
+
+        // Create empty AggregateStats instances.
+        final Map<String,ComponentAggregateStats> idToSpoutAggStats = new 
HashMap<>();
+
+        final Map<String,ComponentAggregateStats> idToBoltAggStats = new 
HashMap<>();
+
+        // Build up TopologyPageInfo instance
+        topoPageInfo = new TopologyPageInfo();
+        topoPageInfo.set_topology_conf(topoConfigJson);
+        topoPageInfo.set_id(TOPOLOGY_ID);
+        topoPageInfo.set_topology_stats(topologyStats);
+        topoPageInfo.set_id_to_spout_agg_stats(idToSpoutAggStats);
+        topoPageInfo.set_id_to_bolt_agg_stats(idToBoltAggStats);
+    }
+
+    /**
+     * Very narrow test case to validate that 'last error' fields are 
populated for a bolt
+     * with an error is present.
+     */
+    @Test
+    void test_getTopologyBoltAggStatsMap_includesLastError() {
+        // Define inputs
+        final String expectedBoltId = "MyBoltId";
+        final String expectedErrorMsg = "This is my test error message";
+        final int expectedErrorTime = (int) 
TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis());
+        final int expectedErrorPort = 4321;
+        final String expectedErrorHost = "my.errored.host";
+
+        // Define our Last Error
+        final ErrorInfo expectedLastError = new ErrorInfo(expectedErrorMsg, 
expectedErrorTime);
+        expectedLastError.set_port(expectedErrorPort);
+        expectedLastError.set_host(expectedErrorHost);
+
+        // Build stats instance for our bolt
+        final ComponentAggregateStats aggregateStats = 
buildBoltAggregateStatsBase();
+        aggregateStats.set_last_error(expectedLastError);
+        addBoltStats(expectedBoltId, aggregateStats);
+
+        // Call method under test.
+        final Map<String, Object> result = UIHelpers.getTopologySummary(
+            topoPageInfo,
+            WINDOW,
+            new HashMap<>(),
+            "spp"
+        );
+
+        // Validate
+        assertNotNull(result, "Should never return null");
+
+        // Validate our Bolt result
+        final Map<String, Object> boltResult = 
getBoltStatsFromTopologySummaryResult(result, expectedBoltId);
+        assertNotNull(boltResult, "Should have an entry for bolt");
+
+        // Verify each piece
+        assertEquals(expectedBoltId, boltResult.get("boltId"));
+        assertEquals(expectedBoltId, boltResult.get("encodedBoltId"));
+
+        // Verify error
+        assertEquals(expectedErrorMsg, boltResult.get("error"));
+        assertEquals(expectedErrorMsg, boltResult.get("lastError"));
+        assertEquals(expectedErrorPort, boltResult.get("errorPort"));
+        assertEquals(expectedErrorHost, boltResult.get("errorHost"));
+        assertEquals(expectedErrorTime, boltResult.get("errorTime"));
+
+        // Fuzzy matching

Review comment:
       You can use simulatedTime 
https://github.com/apache/storm/blob/master/storm-client/src/jvm/org/apache/storm/utils/Time.java#L30.
 
   
   And then the check can be accurate. 
   
   Also please check if 
https://github.com/apache/storm/blob/master/docs/STORM-UI-REST-API.md#apiv1topologyid-get
 needs to be updated.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to