Skip to content
This repository was archived by the owner on Dec 23, 2017. It is now read-only.

Commit f8bdc7b

Browse files
committed
Switch isEnabled() to ClusterConfigService#getOrDefault()
This needs adjustment for the tests to be able to test that properly.
1 parent 511bda4 commit f8bdc7b

File tree

3 files changed

+27
-22
lines changed

3 files changed

+27
-22
lines changed

src/main/java/org/graylog/plugins/usagestatistics/UsageStatsPeriodical.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,10 @@ protected boolean isEnabled() {
7676
return false;
7777
}
7878

79-
final UsageStatsOptOutState optOutState = clusterConfigService.get(UsageStatsOptOutState.class);
79+
final UsageStatsOptOutState state = clusterConfigService.getOrDefault(UsageStatsOptOutState.class,
80+
UsageStatsOptOutState.create(false));
8081

81-
return optOutState == null || !optOutState.isOptOut();
82+
return !state.isOptOut();
8283
}
8384

8485
@Override

src/test/java/org/graylog/plugins/usagestatistics/UsageStatsClusterPeriodicalTest.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import com.squareup.okhttp.OkHttpClient;
2020
import org.graylog.plugins.usagestatistics.providers.SmileObjectMapperProvider;
2121
import org.graylog2.plugin.ServerStatus;
22-
import org.graylog2.plugin.cluster.ClusterConfigService;
2322
import org.graylog2.plugin.cluster.ClusterId;
2423
import org.junit.Before;
2524
import org.junit.Test;
@@ -39,16 +38,17 @@ public class UsageStatsClusterPeriodicalTest {
3938
private ServerStatus serverStatus;
4039
private UsageStatsConfiguration configuration;
4140
@Mock
42-
private ClusterConfigService clusterConfigService;
43-
@Mock
4441
private OkHttpClient httpClient;
42+
43+
private TestClusterConfigService clusterConfigService;
4544
private ObjectMapper objectMapper;
4645
private UsageStatsClusterPeriodical periodical;
4746

4847
@Before
4948
public void setUp() throws Exception {
5049
configuration = spy(new UsageStatsConfiguration());
51-
when(clusterConfigService.get(ClusterId.class)).thenReturn(ClusterId.create("test-cluster-id"));
50+
51+
clusterConfigService = new TestClusterConfigService();
5252
objectMapper = new SmileObjectMapperProvider().get();
5353
periodical = new UsageStatsClusterPeriodical(
5454
clusterService,
@@ -58,6 +58,8 @@ public void setUp() throws Exception {
5858
httpClient,
5959
objectMapper
6060
);
61+
62+
clusterConfigService.write(ClusterId.create("test-cluster-id"));
6163
}
6264

6365
@Test
@@ -102,27 +104,27 @@ public void testStartOnThisNode() throws Exception {
102104
@Test
103105
public void testIsEnabled() throws Exception {
104106
when(configuration.isEnabled()).thenReturn(true);
105-
when(clusterConfigService.get(UsageStatsOptOutState.class)).thenReturn(null);
107+
clusterConfigService.remove(AutoValue_UsageStatsOptOutState.class);
106108
assertThat(periodical.isEnabled()).isTrue();
107109

108110
when(configuration.isEnabled()).thenReturn(true);
109-
when(clusterConfigService.get(UsageStatsOptOutState.class)).thenReturn(UsageStatsOptOutState.create(false));
111+
clusterConfigService.write(UsageStatsOptOutState.create(false));
110112
assertThat(periodical.isEnabled()).isTrue();
111113

112114
when(configuration.isEnabled()).thenReturn(false);
113-
when(clusterConfigService.get(UsageStatsOptOutState.class)).thenReturn(null);
115+
clusterConfigService.remove(AutoValue_UsageStatsOptOutState.class);
114116
assertThat(periodical.isEnabled()).isFalse();
115117

116118
when(configuration.isEnabled()).thenReturn(false);
117-
when(clusterConfigService.get(UsageStatsOptOutState.class)).thenReturn(UsageStatsOptOutState.create(false));
119+
clusterConfigService.write(UsageStatsOptOutState.create(false));
118120
assertThat(periodical.isEnabled()).isFalse();
119121

120122
when(configuration.isEnabled()).thenReturn(true);
121-
when(clusterConfigService.get(UsageStatsOptOutState.class)).thenReturn(UsageStatsOptOutState.create(true));
123+
clusterConfigService.write(UsageStatsOptOutState.create(true));
122124
assertThat(periodical.isEnabled()).isFalse();
123125

124126
when(configuration.isEnabled()).thenReturn(false);
125-
when(clusterConfigService.get(UsageStatsOptOutState.class)).thenReturn(UsageStatsOptOutState.create(true));
127+
clusterConfigService.write(UsageStatsOptOutState.create(true));
126128
assertThat(periodical.isEnabled()).isFalse();
127129
}
128130
}

src/test/java/org/graylog/plugins/usagestatistics/UsageStatsNodePeriodicalTest.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import com.squareup.okhttp.OkHttpClient;
2020
import org.graylog.plugins.usagestatistics.providers.SmileObjectMapperProvider;
2121
import org.graylog2.plugin.ServerStatus;
22-
import org.graylog2.plugin.cluster.ClusterConfigService;
2322
import org.graylog2.plugin.cluster.ClusterId;
2423
import org.graylog2.plugin.system.NodeId;
2524
import org.junit.Before;
@@ -42,17 +41,18 @@ public class UsageStatsNodePeriodicalTest {
4241
private ServerStatus serverStatus;
4342
private UsageStatsConfiguration configuration;
4443
@Mock
45-
private ClusterConfigService clusterConfigService;
46-
@Mock
4744
private OkHttpClient httpClient;
45+
46+
private TestClusterConfigService clusterConfigService;
4847
private ObjectMapper objectMapper;
4948
private UsageStatsNodePeriodical periodical;
5049

5150
@Before
5251
public void setUp() throws Exception {
5352
when(nodeId.anonymize()).thenReturn("test-node-id");
5453
configuration = spy(new UsageStatsConfiguration());
55-
when(clusterConfigService.get(ClusterId.class)).thenReturn(ClusterId.create("test-cluster-id"));
54+
55+
clusterConfigService = new TestClusterConfigService();
5656
objectMapper = new SmileObjectMapperProvider().get();
5757
periodical = new UsageStatsNodePeriodical(
5858
nodeService,
@@ -63,6 +63,8 @@ public void setUp() throws Exception {
6363
httpClient,
6464
objectMapper
6565
);
66+
67+
clusterConfigService.write(ClusterId.create("test-cluster-id"));
6668
}
6769

6870
@Test
@@ -93,27 +95,27 @@ public void testStartOnThisNode() throws Exception {
9395
@Test
9496
public void testIsEnabled() throws Exception {
9597
when(configuration.isEnabled()).thenReturn(true);
96-
when(clusterConfigService.get(UsageStatsOptOutState.class)).thenReturn(null);
98+
clusterConfigService.remove(AutoValue_UsageStatsOptOutState.class);
9799
assertThat(periodical.isEnabled()).isTrue();
98100

99101
when(configuration.isEnabled()).thenReturn(true);
100-
when(clusterConfigService.get(UsageStatsOptOutState.class)).thenReturn(UsageStatsOptOutState.create(false));
102+
clusterConfigService.write(UsageStatsOptOutState.create(false));
101103
assertThat(periodical.isEnabled()).isTrue();
102104

103105
when(configuration.isEnabled()).thenReturn(false);
104-
when(clusterConfigService.get(UsageStatsOptOutState.class)).thenReturn(null);
106+
clusterConfigService.remove(AutoValue_UsageStatsOptOutState.class);
105107
assertThat(periodical.isEnabled()).isFalse();
106108

107109
when(configuration.isEnabled()).thenReturn(false);
108-
when(clusterConfigService.get(UsageStatsOptOutState.class)).thenReturn(UsageStatsOptOutState.create(false));
110+
clusterConfigService.write(UsageStatsOptOutState.create(false));
109111
assertThat(periodical.isEnabled()).isFalse();
110112

111113
when(configuration.isEnabled()).thenReturn(true);
112-
when(clusterConfigService.get(UsageStatsOptOutState.class)).thenReturn(UsageStatsOptOutState.create(true));
114+
clusterConfigService.write(UsageStatsOptOutState.create(true));
113115
assertThat(periodical.isEnabled()).isFalse();
114116

115117
when(configuration.isEnabled()).thenReturn(false);
116-
when(clusterConfigService.get(UsageStatsOptOutState.class)).thenReturn(UsageStatsOptOutState.create(true));
118+
clusterConfigService.write(UsageStatsOptOutState.create(true));
117119
assertThat(periodical.isEnabled()).isFalse();
118120
}
119121
}

0 commit comments

Comments
 (0)