MetricConfigManagerTest.java
5.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package com.dianping.cat.consumer.metric;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import junit.framework.Assert;
import org.junit.Test;
import org.unidal.dal.jdbc.DalException;
import org.unidal.dal.jdbc.Readset;
import org.unidal.dal.jdbc.Updateset;
import com.dianping.cat.consumer.metric.MetricAnalyzer.ConfigItem;
import com.dianping.cat.consumer.metric.config.entity.MetricConfig;
import com.dianping.cat.consumer.metric.config.entity.MetricItemConfig;
import com.dianping.cat.core.config.Config;
import com.dianping.cat.core.config.ConfigDao;
public class MetricConfigManagerTest {
private static int s_storeCount;
@Test
public void testInitNormal() throws Exception {
MetricConfigManager manager = new MockMetricConfigManager();
((MockMetricConfigManager) manager).setConfigDao(new MockConfigDao1());
manager.initialize();
String domain1 = "domain1";
String domain2 = "domain2";
manager.insertMetricIfNotExist(domain1, "type", "metricKey", new ConfigItem());
Assert.assertEquals(1, s_storeCount);
manager.insertMetricIfNotExist(domain1, "type", "metricKey", new ConfigItem());
Assert.assertEquals(1, s_storeCount);
manager.insertMetricIfNotExist(domain2, "type", "metricKey", new ConfigItem());
Assert.assertEquals(2, s_storeCount);
manager.insertMetricItemConfig(new MetricItemConfig());
Assert.assertEquals(3, s_storeCount);
MetricItemConfig item = manager.queryMetricItemConfig(manager.buildMetricKey(domain1, "type", "metricKey"));
Assert.assertEquals(true, item != null);
manager.deleteDomainConfig(manager.buildMetricKey(domain1, "type", "metricKey"));
Assert.assertEquals(4, s_storeCount);
Assert.assertEquals(2, manager.getMetricConfig().getMetricItemConfigs().size());
HashSet<String> hashSet = new HashSet<String>();
hashSet.add(domain1);
hashSet.add(domain2);
List<MetricItemConfig> sets = manager.queryMetricItemConfigs(hashSet);
Assert.assertEquals(1, sets.size());
manager.refreshConfig();
Assert.assertEquals(1, manager.getMetricConfig().getMetricItemConfigs().size());
String id = manager.buildMetricKey(item.getDomain(), item.getType(), item.getMetricKey());
item.setId(id);
Map<MetricItemConfig, Long> configs = new HashMap<MetricItemConfig, Long>();
configs.put(item, 1L);
configs.put(item, 2L);
configs.put(item, 3L);
Assert.assertEquals(3L, configs.get(item).longValue());
Assert.assertEquals(1, configs.size());
item = new MetricItemConfig();
item.setId(id);
configs.put(item, 1L);
configs.put(item, 3L);
Assert.assertEquals(3L, configs.get(item).longValue());
Assert.assertEquals(1, configs.size());
}
@Test
public void testInit(){
MetricConfigManager manager = new MockMetricConfigManager();
((MockMetricConfigManager) manager).setConfigDao(new MockConfigDao2());
try {
manager.initialize();
} catch (Exception e) {
}
s_storeCount=0;
manager.insertMetricIfNotExist("test", "test", "test", new ConfigItem());
manager.insertMetricIfNotExist("test", "test", "test", new ConfigItem());
manager.insertMetricIfNotExist("test1", "test", "test", new ConfigItem());
manager.insertMetricIfNotExist("test1", "test", "test", new ConfigItem());
Assert.assertEquals(s_storeCount, 2);
}
@Test
public void testInitThrowException() throws Exception {
MetricConfigManager manager = new MockMetricConfigManager();
((MockMetricConfigManager) manager).setConfigDao(new MockConfigDao2());
try {
manager.initialize();
} catch (Exception e) {
}
MetricConfig config = manager.getMetricConfig();
Assert.assertEquals(0, config.getMetricItemConfigs().size());
}
public static class MockMetricConfigManager extends MetricConfigManager {
public void setConfigDao(ConfigDao configDao) {
m_configDao = configDao;
}
}
public static class MockConfigDao2 extends MockConfigDao1 {
@Override
public Config findByName(String name, Readset<Config> readset) throws DalException {
throw new DalException("this is a exception for test");
}
}
public static class MockConfigDao1 extends ConfigDao {
@Override
public Config createLocal() {
return super.createLocal();
}
@Override
public int deleteByPK(Config proto) throws DalException {
return 1;
}
@Override
public Config findByPK(int keyId, Readset<Config> readset) throws DalException {
Config config = new Config();
config.setId(keyId);
config.setContent(new MetricConfig().toString());
config.setModifyDate(new Date());
return config;
}
@Override
public Config findByName(String name, Readset<Config> readset) throws DalException {
Config config = new Config();
config.setName(name);
MetricConfig metricConfig = new MetricConfig();
MetricItemConfig metricItemConfig = new MetricItemConfig();
metricItemConfig.setDomain("domain");
metricItemConfig.setMetricKey("domain:URL:metricKey");
metricItemConfig.setType("URL");
metricItemConfig.setMetricKey("metricKey");
metricConfig.addMetricItemConfig(metricItemConfig);
config.setContent(metricConfig.toString());
config.setModifyDate(new Date());
return config;
}
@Override
public int insert(Config proto) throws DalException {
return 1;
}
@Override
public int updateByPK(Config proto, Updateset<Config> updateset) throws DalException {
s_storeCount++;
return 1;
}
}
}