fix ut error

This commit is contained in:
CalvinKirs 2021-01-29 11:57:39 +08:00
parent 40e3795bec
commit a0725e93ee
4 changed files with 14 additions and 45 deletions

View File

@ -1,41 +0,0 @@
/*
* 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.dolphinscheduler.alert.cache;
import java.util.HashMap;
import java.util.Map;
public class AlertPluginDefineCache {
public AlertPluginDefineCache() {
throw new IllegalStateException("Utility class");
}
/**
* k->pluginDefineId v->pluginDefineName
*/
private static Map<Integer, String> pluginDefineMap = new HashMap<>();
public static void addPlugin(int id, String name) {
pluginDefineMap.put(id, name);
}
public static String getNameById(int id) {
return pluginDefineMap.get(id);
}
}

View File

@ -22,7 +22,6 @@ import static java.util.Objects.requireNonNull;
import static com.google.common.base.Preconditions.checkState;
import org.apache.dolphinscheduler.alert.cache.AlertPluginDefineCache;
import org.apache.dolphinscheduler.common.enums.PluginType;
import org.apache.dolphinscheduler.dao.entity.PluginDefine;
import org.apache.dolphinscheduler.spi.DolphinSchedulerPlugin;
@ -32,6 +31,7 @@ import org.apache.dolphinscheduler.spi.classloader.ThreadContextClassLoader;
import org.apache.dolphinscheduler.spi.params.PluginParamsTransfer;
import org.apache.dolphinscheduler.spi.params.base.PluginParams;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@ -48,6 +48,11 @@ public class AlertPluginManager extends AbstractDolphinPluginManager {
private final Map<String, AlertChannelFactory> alertChannelFactoryMap = new ConcurrentHashMap<>();
private final Map<String, AlertChannel> alertChannelMap = new ConcurrentHashMap<>();
/**
* k->pluginDefineId v->pluginDefineName
*/
private final Map<Integer, String> pluginDefineMap = new HashMap<>();
public void addAlertChannelFactory(AlertChannelFactory alertChannelFactory) {
requireNonNull(alertChannelFactory, "alertChannelFactory is null");
@ -84,6 +89,10 @@ public class AlertPluginManager extends AbstractDolphinPluginManager {
return alertChannelMap;
}
public String getPluginNameById(int id) {
return pluginDefineMap.get(id);
}
@Override
public void installPlugin(DolphinSchedulerPlugin dolphinSchedulerPlugin) {
for (AlertChannelFactory alertChannelFactory : dolphinSchedulerPlugin.getAlertChannelFactorys()) {
@ -95,7 +104,7 @@ public class AlertPluginManager extends AbstractDolphinPluginManager {
PluginDefine pluginDefine = new PluginDefine(nameEn, PluginType.ALERT.getDesc(), paramsJson);
int id = pluginDao.addOrUpdatePluginDefine(pluginDefine);
AlertPluginDefineCache.addPlugin(id, pluginDefine.getPluginName());
pluginDefineMap.put(id, pluginDefine.getPluginName());
}
}
}

View File

@ -17,7 +17,6 @@
package org.apache.dolphinscheduler.alert.runner;
import org.apache.dolphinscheduler.alert.cache.AlertPluginDefineCache;
import org.apache.dolphinscheduler.alert.plugin.AlertPluginManager;
import org.apache.dolphinscheduler.common.enums.AlertStatus;
import org.apache.dolphinscheduler.common.utils.CollectionUtils;
@ -146,7 +145,7 @@ public class AlertSender {
* @return AlertResult
*/
private AlertResult alertResultHandler(AlertPluginInstance instance, AlertData alertData) {
String pluginName = AlertPluginDefineCache.getNameById(instance.getPluginDefineId());
String pluginName = alertPluginManager.getPluginNameById(instance.getPluginDefineId());
AlertChannel alertChannel = alertPluginManager.getAlertChannelMap().get(pluginName);
AlertResult alertResultExtend = new AlertResult();
String pluginInstanceName = instance.getInstanceName();

View File

@ -34,6 +34,7 @@ import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
@ -62,6 +63,7 @@ public class AlertServerTest {
PowerMockito.whenNew(AlertPluginManager.class).withNoArguments().thenReturn(alertPluginManager);
ConcurrentHashMap alertChannelMap = new ConcurrentHashMap<>();
alertChannelMap.put("pluginName",alertChannelMock);
PowerMockito.when(alertPluginManager.getPluginNameById(Mockito.anyInt())).thenReturn("pluginName");
PowerMockito.when(alertPluginManager.getAlertChannelMap()).thenReturn(alertChannelMap);
DolphinPluginManagerConfig alertPluginManagerConfig = PowerMockito.mock(DolphinPluginManagerConfig.class);