Skip to content

Commit 4346d5e

Browse files
add helper methods
1 parent 553fb8a commit 4346d5e

File tree

1 file changed

+48
-39
lines changed

1 file changed

+48
-39
lines changed

newrelic-agent/src/test/java/com/newrelic/agent/config/ApplicationLoggingLabelsConfigTest.java

Lines changed: 48 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.newrelic.agent.config;
22

33
import com.newrelic.agent.SaveSystemPropertyProviderRule;
4+
import org.junit.Before;
45
import org.junit.Rule;
56
import org.junit.Test;
67

@@ -18,12 +19,17 @@
1819
public class ApplicationLoggingLabelsConfigTest {
1920

2021
@Rule
21-
public SaveSystemPropertyProviderRule saveSystemPropertyProviderRule = new SaveSystemPropertyProviderRule();
22+
public SaveSystemPropertyProviderRule saveSystemPropertyProviderRule;
2223

2324
public static final String ENV_VAR_ENABLE = "NEW_RELIC_APPLICATION_LOGGING_FORWARDING_LABELS_ENABLED";
2425
public static final String ENV_VAR_EXCLUDE = "NEW_RELIC_APPLICATION_LOGGING_FORWARDING_LABELS_EXCLUDE";
2526
private static final String PARENT_ROOT = "newrelic.config.application_logging.";
2627

28+
@Before
29+
public void setup() {
30+
saveSystemPropertyProviderRule = new SaveSystemPropertyProviderRule();
31+
}
32+
2733
@Test
2834
public void testDefaultLabelsConfig() {
2935
Map<String, Object> props = new HashMap<>();
@@ -36,8 +42,7 @@ public void testDefaultLabelsConfig() {
3642

3743
@Test
3844
public void testSettingExcludeViaSystemProps() {
39-
Properties props = new Properties();
40-
props.put("newrelic.config.application_logging.forwarding.labels.exclude", "test1, test2");
45+
Properties props = createSystemProperties("newrelic.config.application_logging.forwarding.labels.exclude", "test1, test2");
4146

4247
SystemPropertyFactory.setSystemPropertyProvider(
4348
new SystemPropertyProvider(new SaveSystemPropertyProviderRule.TestSystemProps(props), //only test the system property
@@ -72,8 +77,7 @@ public void testLabelsExcludeDefaultsToEmptySet() {
7277

7378
@Test
7479
public void canEnableLabelsViaSystemProperty() {
75-
Properties props = new Properties();
76-
props.put(PARENT_ROOT + "forwarding.labels.enabled", "true");
80+
Properties props = createSystemProperties(PARENT_ROOT + "forwarding.labels.enabled", "true");
7781

7882
SystemPropertyFactory.setSystemPropertyProvider(
7983
new SystemPropertyProvider(new SaveSystemPropertyProviderRule.TestSystemProps(props), //only test the system property
@@ -85,8 +89,7 @@ public void canEnableLabelsViaSystemProperty() {
8589

8690
@Test
8791
public void canDisableLabelsViaSystemProperty() {
88-
Properties props = new Properties();
89-
props.put(PARENT_ROOT + "forwarding.labels.enabled", "false");
92+
Properties props = createSystemProperties(PARENT_ROOT + "forwarding.labels.enabled", "false");
9093

9194
SystemPropertyFactory.setSystemPropertyProvider(
9295
new SystemPropertyProvider(new SaveSystemPropertyProviderRule.TestSystemProps(props), //only test the system property
@@ -125,8 +128,7 @@ public void canDisableViaEnvironmentVariables() {
125128
@Test
126129
public void canEnableLabelsViaEnvVarsOverSysProps() {
127130
Map<String, String> envVars = Collections.singletonMap(ENV_VAR_ENABLE, "true");
128-
Properties props = new Properties();
129-
props.put(PARENT_ROOT + "forwarding.labels.enabled", "false");
131+
Properties props = createSystemProperties(PARENT_ROOT + "forwarding.labels.enabled", "false");
130132

131133
SystemPropertyFactory.setSystemPropertyProvider(new SystemPropertyProvider(new SaveSystemPropertyProviderRule.TestSystemProps(props),
132134
new SaveSystemPropertyProviderRule.TestEnvironmentFacade(envVars)));
@@ -138,8 +140,7 @@ public void canEnableLabelsViaEnvVarsOverSysProps() {
138140
@Test
139141
public void canDisableLabelsViaEnvVarsOverSysProps() {
140142
Map<String, String> envVars = Collections.singletonMap(ENV_VAR_ENABLE, "false");
141-
Properties props = new Properties();
142-
props.put(PARENT_ROOT + "forwarding.labels.enabled", "true");
143+
Properties props = createSystemProperties(PARENT_ROOT + "forwarding.labels.enabled", "true");
143144

144145
SystemPropertyFactory.setSystemPropertyProvider(new SystemPropertyProvider(new SaveSystemPropertyProviderRule.TestSystemProps(props),
145146
new SaveSystemPropertyProviderRule.TestEnvironmentFacade(envVars)));
@@ -201,46 +202,54 @@ public void testInitExcludesHandlesEmptyList() {
201202
@Test
202203
public void testRemoveExcludeLabelsRemoveCorrectly() {
203204
Map<String, Object> props = new HashMap<>();
204-
props.put("exclude", Arrays.asList("exclude1", "exclude2", "exclude3"));
205+
props.put("exclude", Arrays.asList("label4", "label5", "label6"));
205206

206207
ApplicationLoggingLabelsConfig config = new ApplicationLoggingLabelsConfig(props, PARENT_ROOT);
207-
Map<String, String> labels = new HashMap<>();
208-
labels.put("exclude1", "value1");
209-
labels.put("exclude2", "value2");
210-
labels.put("exclude3", "value3");
211-
labels.put("include1", "value1");
212-
labels.put("include2", "value2");
213-
labels.put("include3", "value3");
208+
Map<String, String> labels = createLabelsMap();
209+
labels.put("label4", "value4");
210+
labels.put("label5", "value5");
211+
labels.put("label6", "value6");
212+
213+
assertEquals(6, labels.size());
214+
assertTrue(labels.containsKey("label1"));
215+
assertTrue(labels.containsKey("label2"));
216+
assertTrue(labels.containsKey("label3"));
217+
assertTrue(labels.containsKey("label4"));
218+
assertTrue(labels.containsKey("label5"));
219+
assertTrue(labels.containsKey("label6"));
214220

215221
Map<String, String> filteredLabels = config.removeExcludedLabels(labels);
216222

217223
assertEquals(3, filteredLabels.size());
218-
assertTrue(filteredLabels.containsKey("include1"));
219-
assertTrue(filteredLabels.containsKey("include2"));
220-
assertTrue(filteredLabels.containsKey("include3"));
224+
assertTrue(filteredLabels.containsKey("label1"));
225+
assertTrue(filteredLabels.containsKey("label2"));
226+
assertTrue(filteredLabels.containsKey("label3"));
221227
}
222228

223229
@Test
224230
public void testRemoveExcludedLabelsWhenNoExcludes() {
225-
Map<String, Object> props = new HashMap<>();
226-
ApplicationLoggingLabelsConfig config = new ApplicationLoggingLabelsConfig(props, PARENT_ROOT);
227-
Map<String, String> labels = new HashMap<>();
228-
labels.put("exclude1", "value1");
229-
labels.put("exclude2", "value2");
230-
labels.put("exclude3", "value3");
231-
labels.put("include1", "value1");
232-
labels.put("include2", "value2");
233-
labels.put("include3", "value3");
234-
231+
ApplicationLoggingLabelsConfig config = new ApplicationLoggingLabelsConfig(new HashMap<>(), PARENT_ROOT);
232+
Map<String, String> labels = createLabelsMap();
235233
Map<String, String> filteredLabels = config.removeExcludedLabels(labels);
236234

237-
assertEquals(6, filteredLabels.size());
238-
assertTrue(filteredLabels.containsKey("exclude1"));
239-
assertTrue(filteredLabels.containsKey("exclude2"));
240-
assertTrue(filteredLabels.containsKey("exclude3"));
241-
assertTrue(filteredLabels.containsKey("include1"));
242-
assertTrue(filteredLabels.containsKey("include2"));
243-
assertTrue(filteredLabels.containsKey("include3"));
235+
assertEquals(3, filteredLabels.size());
236+
assertTrue(filteredLabels.containsKey("label1"));
237+
assertTrue(filteredLabels.containsKey("label2"));
238+
assertTrue(filteredLabels.containsKey("label3"));
239+
}
240+
241+
private Properties createSystemProperties(String key, String value) {
242+
Properties properties = new Properties();
243+
properties.put(key, value);
244+
return properties;
245+
}
246+
247+
private Map<String, String> createLabelsMap() {
248+
Map<String, String> labelsMap = new HashMap<>();
249+
labelsMap.put("label1", "value1");
250+
labelsMap.put("label2", "value2");
251+
labelsMap.put("label3", "value3");
252+
return labelsMap;
244253
}
245254

246255
}

0 commit comments

Comments
 (0)