-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add test for plugin management
- Loading branch information
1 parent
cfb4dd1
commit a0f8113
Showing
7 changed files
with
305 additions
and
28 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
common/src/main/java/org/apache/hertzbeat/common/constants/PluginType.java
This file contains 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,29 @@ | ||
/* | ||
* 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.constants; | ||
|
||
/** | ||
* plugin type | ||
*/ | ||
public enum PluginType { | ||
|
||
/** | ||
* do something after alter | ||
*/ | ||
POST_ALERT | ||
} |
This file contains 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 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 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 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
116 changes: 116 additions & 0 deletions
116
manager/src/test/java/org/apache/hertzbeat/manager/controller/PluginControllerTest.java
This file contains 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,116 @@ | ||
/* | ||
* 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 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.List; | ||
import org.apache.hertzbeat.common.constants.CommonConstants; | ||
import org.apache.hertzbeat.common.entity.manager.PluginMetadata; | ||
import org.apache.hertzbeat.common.util.JsonUtil; | ||
import org.apache.hertzbeat.manager.service.PluginService; | ||
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.mock.web.MockMultipartFile; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; | ||
import org.springframework.test.web.servlet.setup.MockMvcBuilders; | ||
|
||
/** | ||
* test case for plugin controller | ||
*/ | ||
@ExtendWith(MockitoExtension.class) | ||
class PluginControllerTest { | ||
|
||
private MockMvc mockMvc; | ||
|
||
@InjectMocks | ||
private PluginController pluginController; | ||
|
||
@Mock | ||
private PluginService pluginService; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
this.mockMvc = MockMvcBuilders.standaloneSetup(pluginController).build(); | ||
} | ||
|
||
@Test | ||
void uploadNewPlugin() throws Exception { | ||
MockMultipartFile jarFile = new MockMultipartFile( | ||
"jarFile", | ||
"plugin-test.jar", | ||
"application/java-archive", | ||
"This is the file content".getBytes() | ||
); | ||
|
||
this.mockMvc.perform(MockMvcRequestBuilders.multipart("/api/plugin") | ||
.file(jarFile) | ||
.contentType(MediaType.MULTIPART_FORM_DATA) | ||
.param("name", "test-plugin") | ||
.param("enableStatus", "true")) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE)) | ||
.andExpect(jsonPath("$.msg").value("Add success")) | ||
.andReturn(); | ||
} | ||
|
||
@Test | ||
void getPlugins() throws Exception { | ||
this.mockMvc.perform(MockMvcRequestBuilders.get("/api/plugin?&search={search}", "test")) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE)) | ||
.andReturn(); | ||
} | ||
|
||
@Test | ||
void deleteTags() throws Exception { | ||
List<Long> ids = new ArrayList<>(); | ||
ids.add(6565463543L); | ||
|
||
this.mockMvc.perform(MockMvcRequestBuilders.delete("/api/plugin") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(JsonUtil.toJson(ids))) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE)) | ||
.andExpect(jsonPath("$.msg").value("Delete success")) | ||
.andReturn(); | ||
} | ||
|
||
@Test | ||
void updatePluginStatus() throws Exception { | ||
PluginMetadata metadata = new PluginMetadata(); | ||
metadata.setId(6565463543L); | ||
metadata.setEnableStatus(true); | ||
|
||
this.mockMvc.perform(MockMvcRequestBuilders.put("/api/plugin") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(JsonUtil.toJson(metadata))) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE)) | ||
.andExpect(jsonPath("$.msg").value("Update success")) | ||
.andReturn(); | ||
} | ||
|
||
} |
Oops, something went wrong.