/** * Use {@link #isString()} to check first, if required. */ public String asString() { return getString(null, asJsonNode()); }
@Override public DomainObjectReprRenderer with(final ObjectAdapter objectAdapter) { this.objectAdapter = objectAdapter; String domainTypeHref = DomainTypeReprRenderer.newLinkToBuilder(getRendererContext(), Rel.DOMAIN_TYPE, objectAdapter.getSpecification()).build().getString("href"); addMediaTypeParams(X_RO_DOMAIN_TYPE, domainTypeHref); return this; }
@Test public void whenJodaLocalDate() throws Exception { allowingObjectSpecToReturnSpecIdFor(org.joda.time.LocalDate.class); allowingObjectAdapterToReturn(new org.joda.time.LocalDate(2014,4,25)); JsonValueEncoder.appendValueAndFormat(mockObjectSpec, mockObjectAdapter, representation, null, false); assertThat(representation.isString("value"), is(true)); assertThat(representation.getString("value"), is("2014-04-25")); assertThat(representation.getString("format"), is("date")); assertThat(representation.getString("extensions.x-isis-format"), is("jodalocaldate")); }
@Test public void whenCharWrapper() throws Exception { allowingObjectSpecToReturnSpecIdFor(Character.class); allowingObjectAdapterToReturn(Character.valueOf('a')); JsonValueEncoder.appendValueAndFormat(mockObjectSpec, mockObjectAdapter, representation, null); assertThat(representation.isString("value"), is(true)); assertThat(representation.getChar("value"), is(Character.valueOf('a'))); assertThat(representation.getString("format"), is(nullValue())); assertThat(representation.getString("extensions.x-isis-format"), is("char")); }
@Test public void whenJavaUtilDate() throws Exception { allowingObjectSpecToReturnSpecIdFor(java.util.Date.class); allowingObjectAdapterToReturn(asDateTime("2014-04-25T12:34:45Z")); JsonValueEncoder.appendValueAndFormat(mockObjectSpec, mockObjectAdapter, representation, null, false); assertThat(representation.isString("value"), is(true)); assertThat(representation.getString("value"), is("2014-04-25T12:34:45Z")); assertThat(representation.getString("format"), is("date-time")); assertThat(representation.getString("extensions.x-isis-format"), is("javautildate")); }
@Test public void forList() throws IOException { assertThat(jsonRepresentation.isString("aSubList"), is(false)); try { jsonRepresentation.getString("aSubList"); fail(); } catch (final IllegalArgumentException e) { assertThat(e.getMessage(), is("'aSubList' is not a string")); } }
@Test public void forList() throws JsonParseException, JsonMappingException, IOException { assertThat(jsonRepresentation.isString("aSubList"), is(false)); try { jsonRepresentation.getString("aSubList"); fail(); } catch (final IllegalArgumentException e) { assertThat(e.getMessage(), is("'aSubList' is not a string")); } }
@Test public void whenCharWrapper() throws Exception { allowingObjectSpecToReturnSpecIdFor(Character.class); allowingObjectAdapterToReturn(Character.valueOf('a')); JsonValueEncoder.appendValueAndFormat(mockObjectSpec, mockObjectAdapter, representation, null, false); assertThat(representation.isString("value"), is(true)); assertThat(representation.getChar("value"), is(Character.valueOf('a'))); assertThat(representation.getString("format"), is(nullValue())); assertThat(representation.getString("extensions.x-isis-format"), is("char")); }
@Test public void forMultipartKey() throws IOException { assertThat(jsonRepresentation.isString("aSubMap.aString"), is(true)); assertThat(jsonRepresentation.getString("aSubMap.aString"), is("aSubMapStringValue")); }
@Test public void whenCharPrimitive() throws Exception { allowingObjectSpecToReturnSpecIdFor(char.class); allowingObjectAdapterToReturn('a'); JsonValueEncoder.appendValueAndFormat(mockObjectSpec, mockObjectAdapter, representation, null, false); assertThat(representation.isString("value"), is(true)); assertThat(representation.getChar("value"), is('a')); assertThat(representation.getString("format"), is(nullValue())); assertThat(representation.getString("extensions.x-isis-format"), is("char")); }
@Test public void whenByteWrapper() throws Exception { allowingObjectSpecToReturnSpecIdFor(Byte.class); allowingObjectAdapterToReturn(Byte.valueOf((byte)123)); JsonValueEncoder.appendValueAndFormat(mockObjectSpec, mockObjectAdapter, representation, null, false); assertThat(representation.isIntegralNumber("value"), is(true)); assertThat(representation.getByte("value"), is(Byte.valueOf((byte)123))); assertThat(representation.getString("format"), is("int")); assertThat(representation.getString("extensions.x-isis-format"), is("byte")); }
@Test public void whenShortWrapper() throws Exception { allowingObjectSpecToReturnSpecIdFor(Short.class); allowingObjectAdapterToReturn(Short.valueOf((short)12345)); JsonValueEncoder.appendValueAndFormat(mockObjectSpec, mockObjectAdapter, representation, null, false); assertThat(representation.isIntegralNumber("value"), is(true)); assertThat(representation.getShort("value"), is(Short.valueOf((short)12345))); assertThat(representation.getString("format"), is("int")); assertThat(representation.getString("extensions.x-isis-format"), is("short")); }
@Test public void whenIntWrapper() throws Exception { allowingObjectSpecToReturnSpecIdFor(Integer.class); allowingObjectAdapterToReturn(Integer.valueOf(12345678)); JsonValueEncoder.appendValueAndFormat(mockObjectSpec, mockObjectAdapter, representation, null, false); assertThat(representation.isIntegralNumber("value"), is(true)); assertThat(representation.getInt("value"), is(Integer.valueOf(12345678))); assertThat(representation.getString("format"), is("int")); assertThat(representation.getString("extensions.x-isis-format"), is("int")); }
@Test public void whenFloatWrapper() throws Exception { allowingObjectSpecToReturnSpecIdFor(Float.class); allowingObjectAdapterToReturn(Float.valueOf((float)123.45)); JsonValueEncoder.appendValueAndFormat(mockObjectSpec, mockObjectAdapter, representation, null, false); assertThat(representation.isDecimal("value"), is(true)); assertThat(representation.getFloat("value"), is(Float.valueOf((float)123.45))); assertThat(representation.getString("format"), is("decimal")); assertThat(representation.getString("extensions.x-isis-format"), is("float")); }
@Test public void whenJavaSqlTimestamp() throws Exception { allowingObjectSpecToReturnSpecIdFor(java.sql.Timestamp.class); final long time = asDateTime("2014-04-25T12:34:45Z").getTime(); allowingObjectAdapterToReturn(new Timestamp(time)); JsonValueEncoder.appendValueAndFormat(mockObjectSpec, mockObjectAdapter, representation, null); assertThat(representation.isLong("value"), is(true)); assertThat(representation.getLong("value"), is(time)); assertThat(representation.getString("format"), is("utc-millisec")); assertThat(representation.getString("extensions.x-isis-format"), is("javasqltimestamp")); }
@Test public void whenFloatPrimitive() throws Exception { allowingObjectSpecToReturnSpecIdFor(Float.class); allowingObjectAdapterToReturn((float)123.45); JsonValueEncoder.appendValueAndFormat(mockObjectSpec, mockObjectAdapter, representation, null, false); assertThat(representation.isDecimal("value"), is(true)); assertThat(representation.getFloat("value"), is((float)123.45)); assertThat(representation.getString("format"), is("decimal")); assertThat(representation.getString("extensions.x-isis-format"), is("float")); }
@Test public void forMap() throws IOException { final JsonRepresentation mapRepresentation = jsonRepresentation.getRepresentation("aLink"); assertThat(mapRepresentation.getString("rel"), is("someRel")); assertThat(mapRepresentation.isMap(), is(true)); }
@Test public void whenIntPrimitive() throws Exception { allowingObjectSpecToReturnSpecIdFor(int.class); allowingObjectAdapterToReturn(12345678); JsonValueEncoder.appendValueAndFormat(mockObjectSpec, mockObjectAdapter, representation, null); assertThat(representation.isIntegralNumber("value"), is(true)); assertThat(representation.getInt("value"), is(12345678)); assertThat(representation.getString("format"), is("int")); assertThat(representation.getString("extensions.x-isis-format"), is("int")); }
@Test public void whenBigInteger() throws Exception { allowingObjectSpecToReturnSpecIdFor(BigInteger.class); allowingObjectAdapterToReturn(new BigInteger("12345678901234567890")); JsonValueEncoder.appendValueAndFormat(mockObjectSpec, mockObjectAdapter, representation, "big-integer(22)", false); assertThat(representation.isString("value"), is(true)); assertThat(representation.isBigInteger("value"), is(true)); assertThat(representation.getBigInteger("value"), is(new BigInteger("12345678901234567890"))); assertThat(representation.getString("format"), is("big-integer(22)")); assertThat(representation.getString("extensions.x-isis-format"), is("javamathbiginteger")); }
@Test public void whenShortPrimitive() throws Exception { allowingObjectSpecToReturnSpecIdFor(short.class); allowingObjectAdapterToReturn((short)12345); JsonValueEncoder.appendValueAndFormat(mockObjectSpec, mockObjectAdapter, representation, null, false); assertThat(representation.isIntegralNumber("value"), is(true)); assertThat(representation.getShort("value"), is((short)12345)); assertThat(representation.getString("format"), is("int")); assertThat(representation.getString("extensions.x-isis-format"), is("short")); }