SearchHelper { public static String insertVariableValues(ConnectorManager connector, Long projectId, String text) throws IllegalStateException { final VariableStoreConnector variableStore = connector.getConnector(VariableStoreConnector.class); final CounterStoreConnector counterStore = connector.getConnector(CounterStoreConnector.class); final FileStoreConnector fileStore = connector.getConnector(FileStoreConnector.class); String result = "" + text; final Pattern pattern = Pattern.compile("\\{\\{(\\$|\\#|\\\\|:)(.*?)}}"); final Matcher matcher = pattern.matcher(result); while (matcher.find()) { final String type = matcher.group(1); final String name = matcher.group(2); switch (type) { case "$": result = result.replaceAll("\\{\\{\\$" + name + "}}", Matcher.quoteReplacement(variableStore.get(name))); break; case "#": result = result.replaceAll("\\{\\{\\#" + name + "}}", String.valueOf(counterStore.get(name))); break; case "\\": final String path = fileStore.getAbsoluteFileLocation(projectId, name); result = result.replaceAll("\\{\\{\\\\" + name + "}}", path); break; case ":": final String value = connector.getEnvironment().getVariablesAsMap().get(name); result = result.replaceAll("\\{\\{:" + name + "}}", value); break; default: break; } } return result; } private  SearchHelper(); static boolean search(String value, String text, boolean regex); static boolean searchWithRegex(String regex, String text); static String insertVariableValues(ConnectorManager connector, Long projectId, String text);  }
@Test public void shouldReplaceVariablesCorrectly() { VariableStoreConnector variables = mock(VariableStoreConnector.class); given(variables.get("name")).willReturn("Jon Doe"); CounterStoreConnector counter = mock(CounterStoreConnector.class); given(counter.get("counter")).willReturn(COUNTER_VALUE); ConnectorManager connector = mock(ConnectorManager.class); WebSiteConnector webSiteConnector = mock(WebSiteConnector.class); FileStoreConnector fileStoreConnector = mock(FileStoreConnector.class); given(fileStoreConnector.getAbsoluteFileLocation(PROJECT_ID, "file.txt")).willReturn("/dir/file.text"); given(connector.getConnector(VariableStoreConnector.class)).willReturn(variables); given(connector.getConnector(CounterStoreConnector.class)).willReturn(counter); given(connector.getConnector(WebSiteConnector.class)).willReturn(webSiteConnector); given(connector.getConnector(FileStoreConnector.class)).willReturn(fileStoreConnector); String result = SearchHelper.insertVariableValues(connector, PROJECT_ID, "Hello {{$name}}, you are {{user}} no. {{#counter}} and want to upload file {{\\file.txt}} that belongs to {{$name}}!"); assertEquals("Hello Jon Doe, you are {{user}} no. " + COUNTER_VALUE + " and want to upload file /dir/file.text that belongs to Jon Doe!", result); }
@Test public void shouldNotReplaceAnythingIfTextContainsNoVariables() { ConnectorManager connector = mock(ConnectorManager.class); String result = SearchHelper.insertVariableValues(connector, PROJECT_ID, "Hello Jon Doe, you are user no. 42!"); assertEquals("Hello Jon Doe, you are user no. " + COUNTER_VALUE + "!", result); }
@Test public void shouldReplaceEnvironmentVariables() { final ProjectEnvironmentVariable variable = new ProjectEnvironmentVariable(); variable.setName("TEST"); variable.setValue("muffin"); final ProjectEnvironment environment = new ProjectEnvironment(); environment.getVariables().add(variable); final ConnectorManager connectors = new ConnectorManager(environment); final String result = SearchHelper.insertVariableValues(connectors, PROJECT_ID, "env: {{:TEST}}"); assertEquals("env: muffin", result); }
@Test public void shouldEscapeDollarSignAndBackSlashInVariableValues() { final ConnectorManager connector = mock(ConnectorManager.class); final VariableStoreConnector variables = mock(VariableStoreConnector.class); given(variables.get("text")).willReturn("text with $ and \\ sign"); given(connector.getConnector(VariableStoreConnector.class)).willReturn(variables); final String result = SearchHelper.insertVariableValues(connector, PROJECT_ID, "this is a {{$text}}"); assertEquals("this is a text with $ and \\ sign", result); }