Skip to content

Commit

Permalink
test: add test for plugin management
Browse files Browse the repository at this point in the history
  • Loading branch information
LiuTianyou committed Jul 7, 2024
1 parent cfb4dd1 commit a0f8113
Show file tree
Hide file tree
Showing 7 changed files with 305 additions and 28 deletions.
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
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@


import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.web.multipart.MultipartFile;

/**
* data transfer class for uploading plugins
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class PluginUpload {

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,6 @@ public ResponseEntity<Message<Void>> deleteTags(
@Operation(summary = "Update enable status", description = "Delete plugins based on ID")
public ResponseEntity<Message<Void>> updatePluginStatus(@RequestBody PluginMetadata plugin) {
pluginService.updateStatus(plugin);
return ResponseEntity.ok(Message.success("Delete success"));
return ResponseEntity.ok(Message.success("Update success"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@

package org.apache.hertzbeat.manager.service;

import java.io.File;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;
import org.apache.hertzbeat.common.entity.dto.PluginUpload;
import org.apache.hertzbeat.common.entity.manager.PluginItem;
import org.apache.hertzbeat.common.entity.manager.PluginMetadata;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
Expand All @@ -33,21 +30,6 @@
*/
public interface PluginService {

/**
* load jar to classloader
*/
void loadJarToClassLoader();


/**
* verify the type of the jar package
*
* @param jarFile jar file
* @return return the full path of the Plugin interface implementation class
*/
List<PluginItem> validateJarFile(File jarFile);


/**
* save plugin
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@
@RequiredArgsConstructor
public class PluginServiceImpl implements PluginService {

private final PluginMetadataDao metadataDao;

private final PluginItemDao itemDao;

public static Map<Class<?>, PluginType> PLUGIN_TYPE_MAPPING = new HashMap<>();

/**
* plugin status
*/
private static final Map<String, Boolean> PLUGIN_ENABLE_STATUS = new ConcurrentHashMap<>();


private URLClassLoader pluginClassLoader;

@Override
Expand Down Expand Up @@ -102,21 +114,18 @@ public void updateStatus(PluginMetadata plugin) {
}
}

public static Map<Class<?>, PluginType> PLUGIN_TYPE_MAPPING = new HashMap<>();

static {
PLUGIN_TYPE_MAPPING.put(Plugin.class, PluginType.POST_ALERT);
}

private final PluginMetadataDao metadataDao;

private final PluginItemDao itemDao;
/**
* plugin status
* verify the type of the jar package
*
* @param jarFile jar file
* @return return the full path of the Plugin interface implementation class
*/
private static final Map<String, Boolean> PLUGIN_ENABLE_STATUS = new ConcurrentHashMap<>();

@Override
public List<PluginItem> validateJarFile(File jarFile) {
List<PluginItem> pluginItems = new ArrayList<>();
try {
Expand Down Expand Up @@ -223,9 +232,11 @@ private void syncPluginStatus() {
PLUGIN_ENABLE_STATUS.putAll(statusMap);
}

/**
* load jar to classloader
*/
@PostConstruct
@Override
public void loadJarToClassLoader() {
private void loadJarToClassLoader() {
try {
if (pluginClassLoader != null) {
pluginClassLoader.close();
Expand Down
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();
}

}
Loading

0 comments on commit a0f8113

Please sign in to comment.