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

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


The following commit(s) were added to refs/heads/master by this push:
     new a4c6a6045 [Improve] add MetricsController unit test (#2445)
a4c6a6045 is described below

commit a4c6a6045f51f50a8f53b8b25e5463b7b6413d67
Author: YuLuo <[email protected]>
AuthorDate: Sat Aug 3 23:54:40 2024 +0800

    [Improve] add MetricsController unit test (#2445)
    
    Signed-off-by: yuluo-yx <[email protected]>
    Co-authored-by: tomsun28 <[email protected]>
---
 .../manager/controller/MetricsControllerTest.java  | 77 ++++++++++++++++++++++
 1 file changed, 77 insertions(+)

diff --git 
a/manager/src/test/java/org/apache/hertzbeat/manager/controller/MetricsControllerTest.java
 
b/manager/src/test/java/org/apache/hertzbeat/manager/controller/MetricsControllerTest.java
new file mode 100644
index 000000000..c269a4ad8
--- /dev/null
+++ 
b/manager/src/test/java/org/apache/hertzbeat/manager/controller/MetricsControllerTest.java
@@ -0,0 +1,77 @@
+/*
+ * 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.hertzbeat.manager.controller;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.hertzbeat.common.queue.impl.InMemoryCommonDataQueue;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+import org.springframework.http.MediaType;
+import org.springframework.test.web.servlet.MockMvc;
+
+import static org.mockito.Mockito.when;
+import static 
org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
+import static 
org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
+import static 
org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
+import static 
org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup;
+
+/**
+ * Test case for {@link MetricsController}
+ */
+
+@ExtendWith(MockitoExtension.class)
+class MetricsControllerTest {
+
+       private MockMvc mockMvc;
+
+       @Mock
+       private InMemoryCommonDataQueue commonDataQueue;
+
+       @InjectMocks
+       private MetricsController metricsController;
+
+       @BeforeEach
+       public void setup() {
+
+               mockMvc = standaloneSetup(metricsController).build();
+       }
+
+       @Test
+       public void testGetMetricsInfo() throws Exception {
+
+               Map<String, Integer> queueInfo = new HashMap<>();
+               queueInfo.put("metric1", 100);
+               queueInfo.put("metric2", 200);
+
+               
when(commonDataQueue.getQueueSizeMetricsInfo()).thenReturn(queueInfo);
+
+               mockMvc.perform(get("/api/metrics")
+                                               
.accept(MediaType.APPLICATION_JSON))
+                               .andExpect(status().isOk())
+                               .andExpect(jsonPath("$.code").value(0))
+                               
.andExpect(jsonPath("$.data.metric1").value(100))
+                               
.andExpect(jsonPath("$.data.metric2").value(200));
+       }
+
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to