@Test public void shouldContainMaterialRevisions() throws Exception { JsonMap jsonMap = presenter.toJson(); JsonValue jsonValue = JsonUtils.from(jsonMap); JsonValue revision = jsonValue.getObject("groups", 0, "history", 0, "materialRevisions", 0); assertThat(revision.getString("revision"), is("svn.100")); assertThat(revision.getString("user"), is("user")); assertThat(revision.getString("date"), is(DateUtils.formatISO8601(modificationDate))); }
@Test public void shouldContainPipelineCounterOrLabel() throws Exception { JsonMap jsonMap = presenter.toJson(); JsonValue jsonValue = JsonUtils.from(jsonMap); JsonValue pipeline = jsonValue.getObject("groups", 0, "history", 0); assertThat(pipeline.getString("counterOrLabel"), is("1")); }
@Test public void shouldShowFirstStageApproverNameInBuildCauseBy() throws Exception { JsonMap jsonMap = presenter.toJson(); JsonValue jsonValue = JsonUtils.from(jsonMap); String revision = jsonValue.getString("groups", 0, "history", 0, "buildCauseBy"); assertThat(revision, is("Triggered by " + GoConstants.DEFAULT_APPROVED_BY)); }
@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 shouldReturnJsonWithModifications() throws Exception { StageJsonPresentationModel presenter = new StageJsonPresentationModel(pipeline, stage, null, new Agents()); Map json = presenter.toJson(); JsonValue jsonValue = JsonUtils.from(json); JsonValue revision = jsonValue.getObject("materialRevisions", 0); // TODO: TRAINWRECK! WE should fix this when we re-do the JSON. We don't think this test will // last long in the new UI String expected = modifications .getMaterialRevisions() .getMaterialRevision(0) .getModifications() .get(0) .getRevision(); assertThat(revision.getString("revision"), is(expected)); assertThat(revision.getString("user"), is(ModificationsMother.MOD_USER_WITH_HTML_CHAR)); assertThat( revision.getString("date"), is(DateUtils.formatISO8601(ModificationsMother.TODAY_CHECKIN))); }
@Test public void shouldEncodeStageLocator() { PipelineConfig pipelineConfig1 = PipelineConfigMother.createPipelineConfigWithStages("mingle-%", "stage1-%", "stage2"); PipelineHistoryJsonPresentationModel presenter = new PipelineHistoryJsonPresentationModel( pipelinePauseInfo, preparePipelineHistoryGroups(pipelineConfig1), pipelineConfig1, pagination(), CAN_FORCE, hasForceBuildCause, hasModification, true); JsonValue json = JsonUtils.from(presenter.toJson()); String stageLocator = json.getString("groups", 0, "history", 0, "stages", 0, "stageLocator"); assertThat(stageLocator, is("mingle-%25/1/stage1-%25/1")); }
/** * @param clazz May be null if the type is unknown. * @param elementType May be null if the type is unknown. * @return May be null. */ public <T> T readValue(Class<T> clazz, Class elementType, JsonValue jsonData) { if (jsonData == null) return null; Type type = ReflectionCache.getType(clazz); if (jsonData.isObject()) { String className = typeName == null ? null : jsonData.getString(typeName, null); if (className != null) { jsonData.remove(typeName); try { type = ReflectionCache.forName(className); } catch (ClassNotFoundException ex) { type = tagToClass.get(className); if (type == null) throw new SerializationException(ex); } } Object object; if (type != null) { Serializer serializer = classToSerializer.get(type); if (serializer != null) return (T) serializer.read(this, jsonData, type.getClassOfType()); object = newInstance(type); if (object instanceof Serializable) { ((Serializable) object).read(this, jsonData); return (T) object; } if (object instanceof HashMap) { HashMap result = (HashMap) object; for (JsonValue child = jsonData.child(); child != null; child = child.next()) result.put(child.name(), readValue(elementType, null, child)); return (T) result; } } else object = new OrderedMap(); if (object instanceof ObjectMap) { ObjectMap result = (ObjectMap) object; for (JsonValue child = jsonData.child(); child != null; child = child.next()) result.put(child.name(), readValue(elementType, null, child)); return (T) result; } readFields(object, jsonData); return (T) object; } if (type != null) { Serializer serializer = classToSerializer.get(type); if (serializer != null) return (T) serializer.read(this, jsonData, type.getClassOfType()); } if (jsonData.isArray()) { if (type == null || type.isAssignableFrom(ReflectionCache.getType(Array.class))) { Array newArray = new Array(); for (JsonValue child = jsonData.child(); child != null; child = child.next()) newArray.add(readValue(elementType, null, child)); return (T) newArray; } if (type.isAssignableFrom(ReflectionCache.getType(ArrayList.class))) { ArrayList newArray = new ArrayList(); for (JsonValue child = jsonData.child(); child != null; child = child.next()) newArray.add(readValue(elementType, null, child)); return (T) newArray; } if (type.isArray()) { Class componentType = type.getComponentType(); if (elementType == null) elementType = componentType; Object newArray = ReflectionCache.newArray(componentType, jsonData.size()); Type arrayType = ReflectionCache.getType(newArray.getClass()); int i = 0; for (JsonValue child = jsonData.child(); child != null; child = child.next()) arrayType.setArrayElement(newArray, i++, readValue(elementType, null, child)); return (T) newArray; } throw new SerializationException( "Unable to convert value to required type: " + jsonData + " (" + type.getName() + ")"); } Class t = type == null ? null : type.getClassOfType(); if (jsonData.isNumber()) { try { if (type == null || t == float.class || t == Float.class) return (T) (Float) jsonData.asFloat(); if (t == int.class || t == Integer.class) return (T) (Integer) jsonData.asInt(); if (t == long.class || t == Long.class) return (T) (Long) jsonData.asLong(); if (t == double.class || t == Double.class) return (T) (Double) (double) jsonData.asFloat(); if (t == String.class) return (T) Float.toString(jsonData.asFloat()); if (t == short.class || t == Short.class) return (T) (Short) (short) jsonData.asInt(); if (t == byte.class || t == Byte.class) return (T) (Byte) (byte) jsonData.asInt(); } catch (NumberFormatException ignored) { } jsonData = new JsonValue(jsonData.asString()); } if (jsonData.isBoolean()) { try { if (type == null || t == boolean.class || t == Boolean.class) return (T) (Boolean) jsonData.asBoolean(); } catch (NumberFormatException ignored) { } jsonData = new JsonValue(jsonData.asString()); } if (jsonData.isString()) { String string = jsonData.asString(); if (type == null || t == String.class) return (T) string; try { if (t == int.class || t == Integer.class) return (T) Integer.valueOf(string); if (t == float.class || t == Float.class) return (T) Float.valueOf(string); if (t == long.class || t == Long.class) return (T) Long.valueOf(string); if (t == double.class || t == Double.class) return (T) Double.valueOf(string); if (t == short.class || t == Short.class) return (T) Short.valueOf(string); if (t == byte.class || t == Byte.class) return (T) Byte.valueOf(string); } catch (NumberFormatException ignored) { } if (t == boolean.class || t == Boolean.class) return (T) Boolean.valueOf(string); if (t == char.class || t == Character.class) return (T) (Character) string.charAt(0); if (type.isEnum()) { Object[] constants = type.getEnumConstants(); for (int i = 0, n = constants.length; i < n; i++) if (string.equals(constants[i].toString())) return (T) constants[i]; } if (t == CharSequence.class) return (T) string; throw new SerializationException( "Unable to convert value to required type: " + jsonData + " (" + type.getName() + ")"); } return null; }