-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[Improve] add unit test #2454
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
Open
yuluo-yx
wants to merge
21
commits into
apache:master
Choose a base branch
from
yuluo-yx:0804-yuluo/tets-1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[Improve] add unit test #2454
Changes from 20 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
65c0522
[Improve] add unit tet
yuluo-yx dd22619
Merge branch 'master' into 0804-yuluo/tets-1
tomsun28 7133347
fix
yuluo-yx 723e6ae
Merge branch '0804-yuluo/tets-1' of https://github.com/yuluo-yx/hertz…
yuluo-yx a2b866d
Merge branch 'master' into 0804-yuluo/tets-1
yuluo-yx 84a4bff
Merge branch 'master' into 0804-yuluo/tets-1
yuluo-yx 00dbcbf
Merge branch 'master' into 0804-yuluo/tets-1
yuluo-yx c9ba2e4
fix
yuluo-yx 9099d75
Merge branch '0804-yuluo/tets-1' of https://github.com/yuluo-yx/hertz…
yuluo-yx fdfbcaa
fix
yuluo-yx 4c9c82f
fix
yuluo-yx 1dfb83f
fix
yuluo-yx 3def4ef
Merge branch 'master' into 0804-yuluo/tets-1
yuluo-yx 827d723
Merge branch 'master' into 0804-yuluo/tets-1
yuluo-yx f79f15e
Merge branch 'master' into 0804-yuluo/tets-1
yuluo-yx 35b6249
Merge branch 'master' into 0804-yuluo/tets-1
tomsun28 4c0fbe0
Merge branch 'master' into 0804-yuluo/tets-1
yuluo-yx 074b56b
Merge branch 'master' into 0804-yuluo/tets-1
yuluo-yx 3e3bbf7
Merge branch 'master' into 0804-yuluo/tets-1
yuluo-yx 88bb8ab
fix
yuluo-yx fa8681d
fix
yuluo-yx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
alerter/src/test/java/org/apache/hertzbeat/alert/controller/AlertSilencesControllerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
|
||
} |
84 changes: 84 additions & 0 deletions
84
common/src/main/java/org/apache/hertzbeat/common/config/MVCConfig.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
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.OffsetDateTime; | ||
import java.time.OffsetTime; | ||
import java.time.ZonedDateTime; | ||
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; | ||
|
||
/** | ||
* @author yuluo | ||
* @author <a href="mailto:yuluo08290126@gmail.com">yuluo</a> | ||
*/ | ||
|
||
@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)); | ||
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); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.