JobCallbackUtil { public static String replaceTokens(final String value, final Map<String, String> contextInfo, final boolean withEncoding) { String result = value; String tokenValue = encodeQueryParam(contextInfo.get(CONTEXT_SERVER_TOKEN), withEncoding); result = result.replaceAll(Pattern.quote(CONTEXT_SERVER_TOKEN), tokenValue); tokenValue = encodeQueryParam(contextInfo.get(CONTEXT_PROJECT_TOKEN), withEncoding); result = result.replaceAll(Pattern.quote(CONTEXT_PROJECT_TOKEN), tokenValue); tokenValue = encodeQueryParam(contextInfo.get(CONTEXT_FLOW_TOKEN), withEncoding); result = result.replaceAll(Pattern.quote(CONTEXT_FLOW_TOKEN), tokenValue); tokenValue = encodeQueryParam(contextInfo.get(CONTEXT_JOB_TOKEN), withEncoding); result = result.replaceAll(Pattern.quote(CONTEXT_JOB_TOKEN), tokenValue); tokenValue = encodeQueryParam(contextInfo.get(CONTEXT_EXECUTION_ID_TOKEN), withEncoding); result = result.replaceAll(Pattern.quote(CONTEXT_EXECUTION_ID_TOKEN), tokenValue); tokenValue = encodeQueryParam(contextInfo.get(CONTEXT_JOB_STATUS_TOKEN), withEncoding); result = result.replaceAll(Pattern.quote(CONTEXT_JOB_STATUS_TOKEN), tokenValue); return result; }  static boolean isThereJobCallbackProperty(final Props props,
      final JobCallbackStatusEnum status); static boolean isThereJobCallbackProperty(final Props props,
      final JobCallbackStatusEnum... jobStatuses); static List<HttpRequestBase> parseJobCallbackProperties(final Props props,
      final JobCallbackStatusEnum status, final Map<String, String> contextInfo,
      final int maxNumCallback); static List<HttpRequestBase> parseJobCallbackProperties(final Props props,
      final JobCallbackStatusEnum status, final Map<String, String> contextInfo,
      final int maxNumCallback, final Logger privateLogger); static Header[] parseHttpHeaders(final String headers); static Map<String, String> buildJobContextInfoMap(final Event event,
      final String server); static String replaceTokens(final String value,
      final Map<String, String> contextInfo, final boolean withEncoding);  }
@Test public void noTokenTest() { final String urlWithNoToken = "http: final String result = JobCallbackUtil.replaceTokens(urlWithNoToken, contextInfo, true); Assert.assertEquals(urlWithNoToken, result); }
@Test public void oneTokenTest() { final String urlWithOneToken = "http: final String result = JobCallbackUtil.replaceTokens(urlWithOneToken, contextInfo, true); Assert.assertEquals("http: + "&another=yes", result); }
@Test public void twoTokensTest() { final String urlWithOneToken = "http: + CONTEXT_FLOW_TOKEN; final String result = JobCallbackUtil.replaceTokens(urlWithOneToken, contextInfo, true); Assert.assertEquals("http: + "&flow=" + FLOW_NAME, result); }
@Test public void multipleOccurrencesOfTheSameTokenTest() { final String urlWithOneToken = "test1=" + CONTEXT_FLOW_TOKEN + "&test2=" + CONTEXT_FLOW_TOKEN; final String result = JobCallbackUtil.replaceTokens(urlWithOneToken, contextInfo, true); Assert.assertEquals("test1=" + FLOW_NAME + "&test2=" + FLOW_NAME, result); }
@Test public void allTokensTest() { final String urlWithOneToken = "http: + CONTEXT_PROJECT_TOKEN + "&flow=" + CONTEXT_FLOW_TOKEN + "&executionId=" + CONTEXT_EXECUTION_ID_TOKEN + "&job=" + CONTEXT_JOB_TOKEN + "&status=" + CONTEXT_JOB_STATUS_TOKEN; final String result = JobCallbackUtil.replaceTokens(urlWithOneToken, contextInfo, true); final String expectedResult = "http: + PROJECT_NAME + "&flow=" + FLOW_NAME + "&executionId=" + EXECUTION_ID + "&job=" + JOB_NAME + "&status=" + JOB_STATUS_NAME; Assert.assertEquals(expectedResult, result); }
@Test public void tokenWithEncoding() throws Exception { final String jobNameWithSpaces = "my job"; final String encodedJobName = URLEncoder.encode(jobNameWithSpaces, "UTF-8"); Map<String, String> customContextInfo = new HashMap<>(); customContextInfo = new HashMap<>(); customContextInfo.put(CONTEXT_SERVER_TOKEN, SERVER_NAME); customContextInfo.put(CONTEXT_PROJECT_TOKEN, PROJECT_NAME); customContextInfo.put(CONTEXT_FLOW_TOKEN, FLOW_NAME); customContextInfo.put(CONTEXT_EXECUTION_ID_TOKEN, EXECUTION_ID); customContextInfo.put(CONTEXT_JOB_TOKEN, jobNameWithSpaces); customContextInfo.put(CONTEXT_JOB_STATUS_TOKEN, JOB_STATUS_NAME); final String urlWithOneToken = "http: final String result = JobCallbackUtil.replaceTokens(urlWithOneToken, customContextInfo, true); Assert.assertEquals("http: + "&flow=" + FLOW_NAME, result); }