|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.hertzbeat.manager.service; |
| 19 | + |
| 20 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 21 | +import org.apache.hertzbeat.manager.dao.GeneralConfigDao; |
| 22 | +import org.apache.hertzbeat.manager.pojo.dto.TemplateConfig; |
| 23 | +import org.apache.hertzbeat.manager.service.impl.TemplateConfigServiceImpl; |
| 24 | +import org.junit.jupiter.api.BeforeEach; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 27 | +import org.mockito.InjectMocks; |
| 28 | +import org.mockito.Mock; |
| 29 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 30 | +import org.springframework.test.util.ReflectionTestUtils; |
| 31 | + |
| 32 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 33 | +import static org.mockito.ArgumentMatchers.any; |
| 34 | +import static org.mockito.Mockito.mock; |
| 35 | +import static org.mockito.Mockito.times; |
| 36 | +import static org.mockito.Mockito.verify; |
| 37 | + |
| 38 | +/** |
| 39 | + * test case for {@link TemplateConfigServiceImpl} |
| 40 | + */ |
| 41 | + |
| 42 | +@ExtendWith(MockitoExtension.class) |
| 43 | +class TemplateConfigServiceTest { |
| 44 | + |
| 45 | + @Mock |
| 46 | + private GeneralConfigDao generalConfigDao; |
| 47 | + |
| 48 | + @Mock |
| 49 | + private ObjectMapper objectMapper; |
| 50 | + |
| 51 | + @Mock |
| 52 | + private AppService appService; |
| 53 | + |
| 54 | + @InjectMocks |
| 55 | + private TemplateConfigServiceImpl templateConfigServiceImpl; |
| 56 | + |
| 57 | + @BeforeEach |
| 58 | + void setUp() { |
| 59 | + |
| 60 | + templateConfigServiceImpl = new TemplateConfigServiceImpl(generalConfigDao, objectMapper); |
| 61 | + ReflectionTestUtils.setField(templateConfigServiceImpl, "appService", appService); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + void testHandlerValidTemplateConfig() { |
| 66 | + |
| 67 | + TemplateConfig templateConfig = mock(TemplateConfig.class); |
| 68 | + templateConfigServiceImpl.handler(templateConfig); |
| 69 | + |
| 70 | + verify( |
| 71 | + appService, |
| 72 | + times(1) |
| 73 | + ).updateCustomTemplateConfig(templateConfig); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + void testHandlerNullTemplateConfig() { |
| 78 | + |
| 79 | + templateConfigServiceImpl.handler(null); |
| 80 | + |
| 81 | + verify( |
| 82 | + appService, |
| 83 | + times(0) |
| 84 | + ).updateCustomTemplateConfig(any()); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + void testType() { |
| 89 | + |
| 90 | + String type = templateConfigServiceImpl.type(); |
| 91 | + assertEquals("template", type); |
| 92 | + } |
| 93 | + |
| 94 | +} |
0 commit comments