Skip to content

Commit 95ed934

Browse files
Merge pull request #99 from vnandwana/master
Fix per-tenant configuration update at runtime
2 parents 4f66dce + 84e3571 commit 95ed934

File tree

2 files changed

+101
-1
lines changed

2 files changed

+101
-1
lines changed

src/main/java/org/killbill/billing/plugin/api/notification/PluginConfigurationHandler.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
import org.slf4j.Logger;
3535
import org.slf4j.LoggerFactory;
3636

37+
import com.fasterxml.jackson.core.JsonProcessingException;
38+
import com.fasterxml.jackson.databind.JsonNode;
39+
import com.fasterxml.jackson.databind.ObjectMapper;
40+
3741
public abstract class PluginConfigurationHandler {
3842

3943
private static final Logger logger = LoggerFactory.getLogger(PluginConfigurationHandler.class);
@@ -49,7 +53,18 @@ public PluginConfigurationHandler(final String pluginName, final OSGIKillbillAPI
4953
protected abstract void configure(@Nullable UUID kbTenantId);
5054

5155
protected void configure(final String eventConfigKeyName, @Nullable final UUID kbTenantId) {
52-
if (eventConfigKeyName.equals(configKeyName)) {
56+
String extractedKeyName = eventConfigKeyName;
57+
58+
try {
59+
final JsonNode jsonNode = new ObjectMapper().readTree(eventConfigKeyName);
60+
if (jsonNode.has("key")) {
61+
extractedKeyName = jsonNode.get("key").asText();
62+
}
63+
} catch (final JsonProcessingException e) {
64+
// No action required, eventConfigKeyName is a plain string.
65+
}
66+
67+
if (extractedKeyName.equals(configKeyName)) {
5368
configure(kbTenantId);
5469
}
5570
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright 2020-2025 Equinix, Inc
3+
* Copyright 2014-2025 The Billing Project, LLC
4+
*
5+
* The Billing Project licenses this file to you under the Apache License, version 2.0
6+
* (the "License"); you may not use this file except in compliance with the
7+
* License. You may obtain a copy of the License at:
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
* License for the specific language governing permissions and limitations
15+
* under the License.
16+
*/
17+
18+
package org.killbill.billing.plugin.api.notification;
19+
20+
import java.util.UUID;
21+
22+
import org.killbill.billing.osgi.libs.killbill.OSGIKillbillAPI;
23+
import org.mockito.Mock;
24+
import org.mockito.MockitoAnnotations;
25+
import org.testng.annotations.BeforeMethod;
26+
import org.testng.annotations.Test;
27+
28+
import com.fasterxml.jackson.databind.JsonNode;
29+
import com.fasterxml.jackson.databind.ObjectMapper;
30+
31+
import static org.mockito.Mockito.mock;
32+
import static org.mockito.Mockito.spy;
33+
import static org.mockito.Mockito.times;
34+
import static org.mockito.Mockito.verify;
35+
import static org.mockito.Mockito.when;
36+
37+
public class TestPluginConfigurationHandler {
38+
39+
private PluginConfigurationHandler pluginConfigurationHandler;
40+
41+
@Mock
42+
private OSGIKillbillAPI osgiKillbillAPI;
43+
44+
@Mock
45+
private ObjectMapper objectMapper;
46+
47+
@BeforeMethod(groups = "fast")
48+
void setUp() {
49+
MockitoAnnotations.openMocks(this);
50+
51+
pluginConfigurationHandler = spy(new PluginConfigurationHandler("email-notification-plugin", osgiKillbillAPI) {
52+
@Override
53+
protected void configure(final UUID kbTenantId) {
54+
// Empty implementation for testing
55+
}
56+
});
57+
}
58+
59+
@Test(groups = "fast")
60+
void testConfigureWithValidJsonKey() throws Exception {
61+
final String eventConfigKeyName = "{\"key\":\"PLUGIN_CONFIG_email-notification-plugin\"}";
62+
final UUID kbTenantId = UUID.randomUUID();
63+
64+
final JsonNode mockJsonNode = mock(JsonNode.class);
65+
when(mockJsonNode.has("key")).thenReturn(true);
66+
when(mockJsonNode.get("key")).thenReturn(mock(JsonNode.class));
67+
when(mockJsonNode.get("key").asText()).thenReturn("PLUGIN_CONFIG_email-notification-plugin");
68+
69+
when(objectMapper.readTree(eventConfigKeyName)).thenReturn(mockJsonNode);
70+
71+
pluginConfigurationHandler.configure(eventConfigKeyName, kbTenantId);
72+
73+
verify(pluginConfigurationHandler, times(1)).configure(kbTenantId);
74+
}
75+
76+
@Test(groups = "fast")
77+
void testConfigureWithPlainString() {
78+
final String eventConfigKeyName = "PLUGIN_CONFIG_email-notification-plugin";
79+
final UUID kbTenantId = UUID.randomUUID();
80+
81+
pluginConfigurationHandler.configure(eventConfigKeyName, kbTenantId);
82+
83+
verify(pluginConfigurationHandler, times(1)).configure(kbTenantId);
84+
}
85+
}

0 commit comments

Comments
 (0)