Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ public interface VariableRepositoryFactory {
*/
String ROUTE_VARIABLE_REPOSITORY_ID = "route-variable-repository";

/**
* Registry bean id for group {@link VariableRepository}.
*
* @since 4.21
*/
String GROUP_VARIABLE_REPOSITORY_ID = "group-variable-repository";

/**
* Gets the {@link VariableRepository} for the given id
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.camel.spi.VariableRepositoryFactory;
import org.apache.camel.support.CamelContextHelper;
import org.apache.camel.support.GlobalVariableRepository;
import org.apache.camel.support.GroupVariableRepository;
import org.apache.camel.support.LifecycleStrategySupport;
import org.apache.camel.support.RouteVariableRepository;
import org.apache.camel.support.service.ServiceSupport;
Expand All @@ -45,6 +46,7 @@ public class DefaultVariableRepositoryFactory extends ServiceSupport implements
private final CamelContext camelContext;
private VariableRepository global;
private VariableRepository route;
private VariableRepository group;
private FactoryFinder factoryFinder;

public DefaultVariableRepositoryFactory(CamelContext camelContext) {
Expand All @@ -64,6 +66,9 @@ public VariableRepository getVariableRepository(String id) {
if (route != null && "route".equals(id)) {
return route;
}
if (group != null && "group".equals(id)) {
return group;
}

VariableRepository repo = CamelContextHelper.lookup(camelContext, id, VariableRepository.class);
if (repo == null) {
Expand Down Expand Up @@ -119,6 +124,18 @@ protected void doStart() throws Exception {
camelContext.getRegistry().bind(ROUTE_VARIABLE_REPOSITORY_ID, route);
}

// let's see if there is a custom group repo
repo = getVariableRepository("group");
if (repo != null) {
if (!(repo instanceof GroupVariableRepository)) {
LOG.info("Using VariableRepository: {} as group repository", repo.getId());
}
group = repo;
} else {
group = new GroupVariableRepository();
camelContext.getRegistry().bind(GROUP_VARIABLE_REPOSITORY_ID, group);
}

if (!camelContext.hasService(global)) {
camelContext.addService(global);
}
Expand All @@ -134,6 +151,9 @@ public void onRoutesRemove(Collection<Route> routes) {
}
});
}
if (!camelContext.hasService(group)) {
camelContext.addService(group);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* 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.camel.processor;

import java.util.List;

import org.apache.camel.ContextTestSupport;
import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

public class SetGroupVariableTest extends ContextTestSupport {

private MockEndpoint end;

@Test
public void testSetGroupVariable() throws Exception {
assertNull(context.getVariable("group:teamA:foo"));
assertNull(context.getVariable("group:teamB:foo"));

end.expectedMessageCount(2);

template.sendBody("direct:teamA", "<blah/>");
template.sendBody("direct:teamB", "<blah/>");

assertMockEndpointsSatisfied();

// variables should be stored on exchange, not accessible from exchange directly
List<Exchange> exchanges = end.getExchanges();
Exchange exchange = exchanges.get(0);
assertNull(exchange.getVariable("foo"));

// should be stored as group variables
assertEquals("bar", context.getVariable("group:teamA:foo"));
assertEquals("baz", context.getVariable("group:teamB:foo"));
}

@Test
public void testGroupVariableIsolation() throws Exception {
end.expectedMessageCount(1);

template.sendBody("direct:teamA", "<blah/>");

assertMockEndpointsSatisfied();

// teamA has the variable, teamB does not
assertEquals("bar", context.getVariable("group:teamA:foo"));
assertNull(context.getVariable("group:teamB:foo"));
}

@Test
public void testGroupVariableSimpleLanguage() throws Exception {
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedBodiesReceived("Value is bar");

template.sendBody("direct:simple", "<blah/>");

assertMockEndpointsSatisfied();
}

@Test
public void testCrossRouteGroupVariable() throws Exception {
MockEndpoint mock = getMockEndpoint("mock:cross");
mock.expectedBodiesReceived("shared-value");

// route "setter" sets the group variable, route "reader" reads it
template.sendBody("direct:setter", "<blah/>");
template.sendBody("direct:reader", "<blah/>");

assertMockEndpointsSatisfied();
}

@Override
@BeforeEach
public void setUp() throws Exception {
super.setUp();
end = getMockEndpoint("mock:end");
}

@Override
protected RouteBuilder createRouteBuilder() {
return new RouteBuilder() {
public void configure() {
from("direct:teamA").routeId("routeA")
.setVariable("group:teamA:foo").constant("bar")
.to("mock:end");

from("direct:teamB").routeId("routeB")
.setVariable("group:teamB:foo").constant("baz")
.to("mock:end");

from("direct:simple").routeId("routeSimple")
.setVariable("group:teamA:foo").constant("bar")
.transform().simple("Value is ${variable.group:teamA:foo}")
.to("mock:result");

from("direct:setter").routeId("routeSetter")
.setVariable("group:shared:myKey").constant("shared-value")
.to("mock:end");

from("direct:reader").routeId("routeReader")
.setBody().variable("group:shared:myKey")
.to("mock:cross");
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
/*
* 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.camel.support;

import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class GroupVariableRepositoryTest {

private GroupVariableRepository repo;

@BeforeEach
public void setUp() {
repo = new GroupVariableRepository();
}

@Test
public void testGetId() {
assertEquals("group", repo.getId());
}

@Test
public void testSetAndGetVariable() {
repo.setVariable("teamA:foo", "bar");
assertEquals("bar", repo.getVariable("teamA:foo"));
}

@Test
public void testVariableIsolationBetweenGroups() {
repo.setVariable("teamA:key", "valueA");
repo.setVariable("teamB:key", "valueB");

assertEquals("valueA", repo.getVariable("teamA:key"));
assertEquals("valueB", repo.getVariable("teamB:key"));
}

@Test
public void testMultipleVariablesInSameGroup() {
repo.setVariable("teamA:foo", "1");
repo.setVariable("teamA:bar", "2");
repo.setVariable("teamA:baz", "3");

assertEquals("1", repo.getVariable("teamA:foo"));
assertEquals("2", repo.getVariable("teamA:bar"));
assertEquals("3", repo.getVariable("teamA:baz"));
assertEquals(3, repo.size());
}

@Test
public void testGetNonExistentVariable() {
assertNull(repo.getVariable("teamA:missing"));
}

@Test
public void testGetNonExistentGroup() {
assertNull(repo.getVariable("noSuchGroup:key"));
}

@Test
public void testRemoveVariable() {
repo.setVariable("teamA:foo", "bar");
Object removed = repo.removeVariable("teamA:foo");

assertEquals("bar", removed);
assertNull(repo.getVariable("teamA:foo"));
}

@Test
public void testRemoveWildcard() {
repo.setVariable("teamA:foo", "1");
repo.setVariable("teamA:bar", "2");
repo.setVariable("teamB:baz", "3");

repo.removeVariable("teamA:*");

assertNull(repo.getVariable("teamA:foo"));
assertNull(repo.getVariable("teamA:bar"));
assertEquals("3", repo.getVariable("teamB:baz"));
assertEquals(1, repo.size());
}

@Test
public void testSetNullRemoves() {
repo.setVariable("teamA:foo", "bar");
repo.setVariable("teamA:foo", null);

assertNull(repo.getVariable("teamA:foo"));
}

@Test
public void testGetGroupIds() {
repo.setVariable("teamA:foo", "1");
repo.setVariable("teamB:bar", "2");
repo.setVariable("teamC:baz", "3");

Set<String> ids = repo.getGroupIds();
assertEquals(Set.of("teamA", "teamB", "teamC"), ids);
}

@Test
public void testGetGroupIdsEmpty() {
assertTrue(repo.getGroupIds().isEmpty());
}

@Test
public void testGetGroupIdsAfterRemoveWildcard() {
repo.setVariable("teamA:foo", "1");
repo.setVariable("teamB:bar", "2");

repo.removeVariable("teamA:*");

assertEquals(Set.of("teamB"), repo.getGroupIds());
}

@Test
public void testNames() {
repo.setVariable("teamA:foo", "1");
repo.setVariable("teamB:bar", "2");

Set<String> names = repo.names().collect(Collectors.toSet());
assertEquals(Set.of("teamA:foo", "teamB:bar"), names);
}

@Test
public void testGetVariables() {
repo.setVariable("teamA:foo", "1");
repo.setVariable("teamB:bar", "2");

Map<String, Object> vars = repo.getVariables();
assertEquals(2, vars.size());
assertEquals("1", vars.get("teamA:foo"));
assertEquals("2", vars.get("teamB:bar"));
}

@Test
public void testHasVariables() {
assertFalse(repo.hasVariables());

repo.setVariable("teamA:foo", "bar");
assertTrue(repo.hasVariables());
}

@Test
public void testSize() {
assertEquals(0, repo.size());

repo.setVariable("teamA:foo", "1");
repo.setVariable("teamA:bar", "2");
repo.setVariable("teamB:baz", "3");
assertEquals(3, repo.size());
}

@Test
public void testClear() {
repo.setVariable("teamA:foo", "1");
repo.setVariable("teamB:bar", "2");

repo.clear();

assertFalse(repo.hasVariables());
assertEquals(0, repo.size());
}

@Test
public void testMissingColonThrows() {
assertThrows(IllegalArgumentException.class, () -> repo.getVariable("noColon"));
assertThrows(IllegalArgumentException.class, () -> repo.setVariable("noColon", "value"));
assertThrows(IllegalArgumentException.class, () -> repo.removeVariable("noColon"));
}
}
Loading