### Question:
WebHookPayloadTailoredJson extends WebHookPayloadGeneric implements WebHookPayload { @Override protected String getStatusAsString(WebHookPayloadContent content, WebHookTemplateContent webHookTemplate) { try { return content.getExtraParameters(getVariableResolverFactory()).get("body"); } catch (NullPointerException npe){ throw new WebHookPayloadContentAssemblyException("Failure building message content :: Unable to retreive 'body' content."); } } WebHookPayloadTailoredJson(WebHookPayloadManager manager, WebHookVariableResolverManager variableResolverManager); @Override void register(); @Override String getFormatDescription(); @Override String getFormatShortName(); @Override String getFormatToolTipText(); @Override String getContentType(); @Override Integer getRank(); @Override void setRank(Integer rank); @Override String getCharset(); @Override WebHookStringRenderer getWebHookStringRenderer(); @Override PayloadTemplateEngineType getTemplateEngineType(); static final String FORMAT_SHORT_NAME; }### Answer:
@Test (expected=WebHookPayloadContentAssemblyException.class) public void testForNullPointerWithoutBody() throws WebHookPayloadContentAssemblyException{ WebHookPayloadTailoredJson whp = new WebHookPayloadTailoredJson(null, variableResolverManager); ExtraParameters extraParameters = new ExtraParameters(); extraParameters.addAll("params", new TreeMap<String,String>()); extraParameters.addAll("teamcity", new TreeMap<String,String>()); Map<String,String> templates = new TreeMap<>(); templates.put(WebHookPayloadDefaultTemplates.HTML_BUILDSTATUS_TEMPLATE, "test template"); WebHookMockingFramework framework = WebHookMockingFrameworkImpl.create(BuildStateEnum.BUILD_FINISHED, extraParameters); VariableResolverFactory variableResolverFactory = variableResolverManager.getVariableResolverFactory(PayloadTemplateEngineType.LEGACY); WebHookPayloadContent content = new WebHookPayloadContent(variableResolverFactory, framework.getServer(), framework.getRunningBuild(), sFinishedBuild, BuildStateEnum.BUILD_FINISHED, extraParameters, templates); whp.getStatusAsString(content, null); }
@Test public void testForNotNullPointerWithBody() throws WebHookPayloadContentAssemblyException{ WebHookPayloadTailoredJson whp = new WebHookPayloadTailoredJson(null, variableResolverManager); ExtraParameters extraParameters = new ExtraParameters(new TreeMap<String,String>()); extraParameters.put("body", "{ \"someBody\" : \"This is a body for project ${projectName} \"}"); Map<String, String> templates = new TreeMap<>(); templates.put(WebHookPayloadDefaultTemplates.HTML_BUILDSTATUS_TEMPLATE, "test template"); WebHookMockingFramework framework = WebHookMockingFrameworkImpl.create(BuildStateEnum.BUILD_FINISHED, extraParameters); VariableResolverFactory variableResolverFactory = variableResolverManager.getVariableResolverFactory(PayloadTemplateEngineType.LEGACY); extraParameters.addAll("teamcity", framework.getRunningBuild().getParametersProvider().getAll()); WebHookPayloadContent content = new WebHookPayloadContent(variableResolverFactory, framework.getServer(), framework.getRunningBuild(), sFinishedBuild, BuildStateEnum.BUILD_FINISHED, extraParameters, templates); whp.getStatusAsString(content, null); assertEquals("{ \"someBody\" : \"This is a body for project Test Project \"}", whp.getStatusAsString(content, null)); }