@Test
  public void postBuildAsXmlPartial_shouldEnforcePipelineGroupAdminPermissions() throws Exception {
    String md5 = setUpPipelineGroupsWithAdminPermissions();

    controller.postBuildAsXmlPartial("go", "stage", 0, NEW_BUILD, md5, response);
    assertThat(response.getStatus(), is(SC_UNAUTHORIZED));

    controller.postBuildAsXmlPartial("bcg", "stage", 0, NEW_BUILD, md5, response);
    assertThat(response.getStatus(), is(SC_OK));
  }
 @Test
 public void shouldPostBuildAsPartialXml() throws Exception {
   configHelper.addPipeline("pipeline", "stage", "build1", "build2");
   String newXml = "<job name=\"build3\" />";
   String md5 = goConfigDao.md5OfConfigFile();
   ModelAndView mav =
       controller.postBuildAsXmlPartial("pipeline", "stage", 0, newXml, md5, response);
   assertThat(response.getStatus(), is(SC_OK));
   assertThat(response.getContentType(), is(RESPONSE_CHARSET_JSON));
   Map json = (Map) mav.getModel().get("json");
   new JsonTester(json).shouldContain("{ 'result' : 'JobConfig changed successfully.' }");
 }
 @Test
 public void shouldReturnXmlAndErrorMessageWhenInvalidPostOfBuildAsPartialXml() throws Exception {
   configHelper.addPipeline("pipeline", "stage", "build1", "build2");
   String newXml = "<job name=\"build3\" />";
   String md5 = goConfigDao.md5OfConfigFile();
   ModelAndView mav =
       controller.postBuildAsXmlPartial("pipeline", "stage", 4, newXml, md5, response);
   assertThat(response.getStatus(), is(SC_NOT_FOUND));
   assertThat(response.getContentType(), is(RESPONSE_CHARSET_JSON));
   Map json = (Map) mav.getModel().get("json");
   new JsonTester(json)
       .shouldContain(
           "{ 'result' : 'Build does not exist.'," + "  'originalContent' : '" + newXml + "' }");
 }
 @Test
 public void shouldReturnXmlAndErrorMessageWhenPostOfBuildAsInvalidPartialXml() throws Exception {
   configHelper.addPipeline("pipeline", "stage", "build1", "build2");
   String badXml = "<;askldjfa;dsklfja;sdjas;lkdfjob name=\"build3\" />";
   String md5 = goConfigDao.md5OfConfigFile();
   ModelAndView mav =
       controller.postBuildAsXmlPartial("pipeline", "stage", 0, badXml, md5, response);
   assertThat(response.getStatus(), is(SC_CONFLICT));
   assertThat(response.getContentType(), is(RESPONSE_CHARSET_JSON));
   Map json = (Map) mav.getModel().get("json");
   new JsonTester(json)
       .shouldContain(
           "{ 'result' : 'Error on line 1 of document  : The markup in the document preceding the root element must be well-formed. Nested exception: The markup in the document preceding the root element must be well-formed.',"
               + "  'originalContent' : '"
               + badXml
               + "' }");
 }
 @Test
 public void shouldReturnXmlAndErrorMessageWhenPostOfBuildWithDuplicatedNameAsPartialXml()
     throws Exception {
   configHelper.addPipeline("pipeline", "stage", "build1", "build2");
   String newXml = "<job name=\"build2\" />";
   String md5 = goConfigDao.md5OfConfigFile();
   ModelAndView mav =
       controller.postBuildAsXmlPartial("pipeline", "stage", 0, newXml, md5, response);
   assertThat(response.getStatus(), is(SC_CONFLICT));
   assertThat(response.getContentType(), is(RESPONSE_CHARSET_JSON));
   Map<String, Object> json = (Map) mav.getModel().get("json");
   Map<String, Object> expected = new LinkedHashMap<>();
   expected.put(
       "result",
       "Duplicate unique value [build2] declared for identity constraint \"uniqueJob\" of element \"jobs\".");
   expected.put("originalContent", newXml);
   assertThat(json, is(expected));
 }
 @Test
 public void shouldReturnErrorMessageWhenPostOfJobContainsDotForExecWorkingDir() throws Exception {
   configHelper.addPipeline("pipeline", "stage", "build1", "build2");
   String badXml =
       "<job name=\"build3\" >"
           + "<tasks>"
           + "<exec command=\"ant\" workingdir=\".\"/>"
           + "</tasks>"
           + "</job>";
   String md5 = goConfigDao.md5OfConfigFile();
   ModelAndView mav =
       controller.postBuildAsXmlPartial("pipeline", "stage", 0, badXml, md5, response);
   assertThat(response.getStatus(), is(SC_CONFLICT));
   assertThat(response.getContentType(), is(RESPONSE_CHARSET_JSON));
   Map json = (Map) mav.getModel().get("json");
   JsonValue jsonValue = JsonUtils.from(json);
   assertThat(unescapeJavaScript(jsonValue.getString("originalContent")), is(badXml));
   assertThat(
       unescapeJavaScript(jsonValue.getString("result")), containsString("File path is invalid"));
 }
 @Test
 public void shouldReturnErrorMessageIfConfigFileChangesBeforePostBuildAsPartialXml()
     throws Exception {
   String md5 = goConfigDao.md5OfConfigFile();
   configHelper.addPipeline("pipeline", "stage", "build1", "build2");
   String newXml = "<job name=\"build3\" />";
   ModelAndView mav =
       controller.postBuildAsXmlPartial("pipeline", "stage", 0, newXml, md5, response);
   assertThat(response.getStatus(), is(SC_CONFLICT));
   assertThat(response.getContentType(), is(RESPONSE_CHARSET_JSON));
   Map json = (Map) mav.getModel().get("json");
   new JsonTester(json)
       .shouldContain(
           "{ 'result' : '"
               + ConfigFileHasChangedException.CONFIG_CHANGED_PLEASE_REFRESH
               + "',"
               + "  'originalContent' : '"
               + newXml
               + "' }");
 }