-
Notifications
You must be signed in to change notification settings - Fork 0
[Improve] add unit test #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
65c0522
dd22619
7133347
723e6ae
a2b866d
84a4bff
00dbcbf
c9ba2e4
9099d75
fdfbcaa
4c9c82f
1dfb83f
3def4ef
827d723
f79f15e
35b6249
4c0fbe0
074b56b
3e3bbf7
88bb8ab
fa8681d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,16 +17,9 @@ | |
|
||
package org.apache.hertzbeat.alert.controller; | ||
|
||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
import org.apache.catalina.Manager; | ||
import org.apache.hertzbeat.alert.service.AlertDefineService; | ||
import org.apache.hertzbeat.common.constants.CommonConstants; | ||
import org.apache.hertzbeat.common.entity.alerter.AlertDefine; | ||
|
@@ -36,116 +29,60 @@ | |
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.data.domain.PageImpl; | ||
import org.springframework.data.domain.PageRequest; | ||
import org.springframework.data.domain.Sort; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; | ||
import org.springframework.test.web.servlet.setup.MockMvcBuilders; | ||
|
||
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 AlertDefinesController} | ||
* Test whether the data mocked at the mock is correct, and test whether the format of the returned data is correct | ||
*/ | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
@SpringBootTest(classes = Manager.class) | ||
Comment on lines
47
to
+48
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Inconsistent testing approach. The test class combines Choose one of these approaches:
@ExtendWith(MockitoExtension.class)
-@SpringBootTest(classes = Manager.class)
class AlertDefinesControllerTest {
// Keep the standaloneSetup approach
}
-@ExtendWith(MockitoExtension.class)
@SpringBootTest(classes = Manager.class)
+@AutoConfigureMockMvc
class AlertDefinesControllerTest {
+ @Autowired
private MockMvc mockMvc;
@BeforeEach
void setUp() {
- this.mockMvc = standaloneSetup(alertDefinesController).build();
// Other setup code...
}
} Also applies to: 64-64 |
||
class AlertDefinesControllerTest { | ||
|
||
private MockMvc mockMvc; | ||
|
||
@InjectMocks | ||
private AlertDefinesController alertDefinesController; | ||
private MockMvc mockMvc; | ||
|
||
@Mock | ||
AlertDefineService alertDefineService; | ||
@InjectMocks | ||
private AlertDefinesController alertDefinesController; | ||
|
||
// Parameters to avoid default values interference, default values have been replaced | ||
List<Long> ids = Stream.of(6565463543L, 6565463544L).collect(Collectors.toList()); | ||
Byte priority = Byte.parseByte("1"); | ||
String sort = "gmtCreate"; | ||
String order = "asc"; | ||
Integer pageIndex = 1; | ||
Integer pageSize = 7; | ||
|
||
// Parameter collection | ||
Map<String, Object> content = new HashMap<>(); | ||
@Mock | ||
private AlertDefineService alertDefineService; | ||
|
||
// Object for mock | ||
PageRequest pageRequest; | ||
private AlertDefine alertDefine; | ||
|
||
// Since the specification is used in dynamic proxy, it cannot be mocked | ||
// Missing debugging parameters are ids, priority | ||
// The missing part has been manually output for testing | ||
@BeforeEach | ||
void setUp() { | ||
|
||
@BeforeEach | ||
void setUp() { | ||
this.mockMvc = MockMvcBuilders.standaloneSetup(alertDefinesController).build(); | ||
content.put("ids", ids); | ||
content.put("priority", priority); | ||
content.put("sort", sort); | ||
content.put("order", order); | ||
content.put("pageIndex", pageIndex); | ||
content.put("pageSize", pageSize); | ||
Sort sortExp = Sort.by(new Sort.Order(Sort.Direction.fromString(content.get("order").toString()), content.get("sort").toString())); | ||
pageRequest = PageRequest.of((Integer) content.get("pageIndex"), (Integer) content.get("pageSize"), sortExp); | ||
} | ||
this.mockMvc = standaloneSetup(alertDefinesController).build(); | ||
|
||
// @Test | ||
// todo: fix this test | ||
void getAlertDefines() throws Exception { | ||
alertDefine = AlertDefine.builder() | ||
.id(9L) | ||
.app("linux") | ||
.metric("disk") | ||
.field("usage") | ||
.expr("x") | ||
.times(1) | ||
.tags(new LinkedList<>()) | ||
.build(); | ||
} | ||
|
||
// Test the correctness of the mock | ||
// Although objects cannot be mocked, stubs can be stored using class files | ||
// Mockito.when(alertDefineService.getAlertDefines(Mockito.any(Specification.class), Mockito.argThat(new ArgumentMatcher<PageRequest>() { | ||
// @Override | ||
// public boolean matches(PageRequest pageRequestMidden) { | ||
// // There are three methods in the source code that need to be compared, namely getPageNumber(), getPageSize(), getSort() | ||
// if(pageRequestMidden.getPageSize() == pageRequest.getPageSize() && | ||
// pageRequestMidden.getPageNumber() == pageRequest.getPageNumber() && | ||
// pageRequestMidden.getSort().equals(pageRequest.getSort())) { | ||
// return true; | ||
// } | ||
// return false; | ||
// } | ||
// }))).thenReturn(new PageImpl<AlertDefine>(new ArrayList<AlertDefine>())); | ||
AlertDefine define = AlertDefine.builder().id(9L).app("linux").metric("disk").field("usage").expr("x").times(1).tags(new LinkedList<>()).build(); | ||
Mockito.when(alertDefineService.getAlertDefines(null, null, null, "id", "desc", 1, 10)).thenReturn(new PageImpl<>(Collections.singletonList(define))); | ||
@Test | ||
void deleteAlertDefines() throws Exception { | ||
|
||
mockMvc.perform(MockMvcRequestBuilders.get( | ||
"/api/alert/defines") | ||
.param("ids", ids.toString().substring(1, ids.toString().length() - 1)) | ||
.param("priority", priority.toString()) | ||
.param("sort", sort) | ||
.param("order", order) | ||
.param("pageIndex", pageIndex.toString()) | ||
.param("pageSize", pageSize.toString())) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE)) | ||
.andExpect(jsonPath("$.data.content").value(new ArrayList<>())) | ||
.andExpect(jsonPath("$.data.pageable").value("INSTANCE")) | ||
.andExpect(jsonPath("$.data.totalPages").value(1)) | ||
.andExpect(jsonPath("$.data.totalElements").value(0)) | ||
.andExpect(jsonPath("$.data.last").value(true)) | ||
.andExpect(jsonPath("$.data.number").value(0)) | ||
.andExpect(jsonPath("$.data.size").value(0)) | ||
.andExpect(jsonPath("$.data.first").value(true)) | ||
.andExpect(jsonPath("$.data.numberOfElements").value(0)) | ||
.andExpect(jsonPath("$.data.empty").value(true)) | ||
.andExpect(jsonPath("$.data.sort.empty").value(true)) | ||
.andExpect(jsonPath("$.data.sort.sorted").value(false)) | ||
.andExpect(jsonPath("$.data.sort.unsorted").value(true)) | ||
.andReturn(); | ||
} | ||
this.mockMvc.perform(MockMvcRequestBuilders.delete("/api/alert/defines") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(JsonUtil.toJson(Collections.singletonList(1)))) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE)) | ||
.andReturn(); | ||
} | ||
|
||
@Test | ||
void deleteAlertDefines() throws Exception { | ||
this.mockMvc.perform(MockMvcRequestBuilders.delete("/api/alert/defines") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(JsonUtil.toJson(ids))) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE)) | ||
.andReturn(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,81 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/* | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* 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.alert.controller; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import org.apache.hertzbeat.alert.service.AlertSilenceService; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import org.apache.hertzbeat.common.constants.CommonConstants; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import org.apache.hertzbeat.common.entity.alerter.AlertSilence; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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.ArgumentMatchers.any; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import static org.mockito.Mockito.doNothing; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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 AlertSilencesController} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@ExtendWith(MockitoExtension.class) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
class AlertSilencesControllerTest { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
private MockMvc mockMvc; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@Mock | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
private AlertSilenceService alertSilenceService; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
private AlertSilence alertSilence; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@InjectMocks | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
private AlertSilencesController alertSilencesController; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@BeforeEach | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
void setUp() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
this.mockMvc = standaloneSetup(alertSilencesController).build(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
alertSilence = AlertSilence | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.builder() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.id(1L) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.type((byte) 1) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.name("Test Silence") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.build(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@Test | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
void testDeleteAlertDefines() throws Exception { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
doNothing().when(alertSilenceService).deleteAlertSilences(any()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
mockMvc.perform(delete("/api/alert/silences") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.param("ids", "1,2,3") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.accept(MediaType.APPLICATION_JSON)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.andExpect(status().isOk()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+69
to
+79
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Test method needs better naming and more test cases. The test method name -@Test
-void testDeleteAlertDefines() throws Exception {
+@Test
+void testDeleteAlertSilences() throws Exception {
doNothing().when(alertSilenceService).deleteAlertSilences(any());
mockMvc.perform(delete("/api/alert/silences")
.param("ids", "1,2,3")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE));
}
+
+@Test
+void testDeleteAlertSilences_withEmptyIdList() throws Exception {
+ mockMvc.perform(delete("/api/alert/silences")
+ .accept(MediaType.APPLICATION_JSON))
+ .andExpect(status().isOk())
+ .andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE));
+}
+
+@Test
+void testGetAlertSilences() throws Exception {
+ Page<AlertSilence> mockPage = new PageImpl<>(List.of(alertSilence));
+ when(alertSilenceService.getAlertSilences(any(), any(), any(), any(), anyInt(), anyInt()))
+ .thenReturn(mockPage);
+
+ mockMvc.perform(get("/api/alert/silences")
+ .accept(MediaType.APPLICATION_JSON))
+ .andExpect(status().isOk())
+ .andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE))
+ .andExpect(jsonPath("$.data.content[0].id").value(1));
+} 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,96 @@ | ||||||
/* | ||||||
* 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.common.config; | ||||||
|
||||||
import java.text.SimpleDateFormat; | ||||||
import java.time.LocalDate; | ||||||
import java.time.LocalDateTime; | ||||||
import java.time.LocalTime; | ||||||
import java.time.format.DateTimeFormatter; | ||||||
import java.util.List; | ||||||
import java.util.TimeZone; | ||||||
|
||||||
import com.fasterxml.jackson.annotation.JsonAutoDetect; | ||||||
import com.fasterxml.jackson.annotation.PropertyAccessor; | ||||||
import com.fasterxml.jackson.databind.ObjectMapper; | ||||||
import com.fasterxml.jackson.databind.SerializationFeature; | ||||||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; | ||||||
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer; | ||||||
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer; | ||||||
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer; | ||||||
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer; | ||||||
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; | ||||||
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer; | ||||||
|
||||||
import org.springframework.beans.factory.annotation.Autowired; | ||||||
import org.springframework.context.annotation.Configuration; | ||||||
import org.springframework.data.web.config.SpringDataJacksonConfiguration; | ||||||
import org.springframework.http.converter.HttpMessageConverter; | ||||||
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; | ||||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||||||
|
||||||
/** | ||||||
* MVC Configuration. | ||||||
*/ | ||||||
|
||||||
@Configuration | ||||||
public class MVCConfig implements WebMvcConfigurer { | ||||||
|
||||||
public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss"; | ||||||
|
||||||
public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd"; | ||||||
|
||||||
public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss"; | ||||||
|
||||||
@Autowired | ||||||
private SpringDataJacksonConfiguration.PageModule pageModule; | ||||||
|
||||||
@Override | ||||||
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) { | ||||||
|
||||||
MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter(); | ||||||
|
||||||
ObjectMapper objectMapper = new ObjectMapper(); | ||||||
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); | ||||||
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); | ||||||
|
||||||
final SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DEFAULT_DATE_TIME_FORMAT); | ||||||
simpleDateFormat.setTimeZone(TimeZone.getDefault()); | ||||||
|
||||||
JavaTimeModule javaTimeModule = new JavaTimeModule(); | ||||||
|
||||||
DateTimeFormatter defaultDateTimeFormatter = DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT); | ||||||
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT); | ||||||
DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT); | ||||||
|
||||||
javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(defaultDateTimeFormatter)); | ||||||
javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(dateTimeFormatter)); | ||||||
javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(timeFormatter)); | ||||||
|
||||||
javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(dateTimeFormatter)); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix incorrect date format for LocalDateTime deserialization. You're using the date formatter ( Use the -javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(dateTimeFormatter));
+javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(defaultDateTimeFormatter)); 📝 Committable suggestion
Suggested change
|
||||||
javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(dateTimeFormatter)); | ||||||
javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(timeFormatter)); | ||||||
|
||||||
objectMapper.registerModule(javaTimeModule) | ||||||
.registerModule(pageModule) | ||||||
.setDateFormat(simpleDateFormat); | ||||||
|
||||||
messageConverter.setObjectMapper(objectMapper); | ||||||
converters.add(0, messageConverter); | ||||||
} | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Incorrect import for Manager class.
The import
org.apache.catalina.Manager
is incorrect for this context. This is a Tomcat-specific interface, not a Spring Boot application class that should be used with@SpringBootTest(classes = Manager.class)
.Replace with the correct import for your application's main class, which is likely something like
org.apache.hertzbeat.manager.Manager
based on the class reference on line 48.