@Override public ObjectAdapter asAdapter(JsonRepresentation repr, String format) { if (repr.isString()) { return adapterFor(repr.asString()); } return null; } @Override
@Override public ObjectAdapter asAdapter(JsonRepresentation repr, String format) { if (repr.isString()) { return adapterFor(repr.asString()); } return null; } @Override
@Test public void forNonExistent() throws JsonParseException, JsonMappingException, IOException { assertThat(jsonRepresentation.isString("doesNotExist"), is(false)); assertThat(jsonRepresentation.getString("doesNotExist"), is(nullValue())); }
@Test public void forMultipartKey() throws IOException { assertThat(jsonRepresentation.isString("aSubMap.aString"), is(true)); assertThat(jsonRepresentation.getString("aSubMap.aString"), is("aSubMapStringValue")); }
@Test public void forMultipartKey() throws JsonParseException, JsonMappingException, IOException { assertThat(jsonRepresentation.isString("aSubMap.aString"), is(true)); assertThat(jsonRepresentation.getString("aSubMap.aString"), is("aSubMapStringValue")); }
@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 forMap() throws IOException { assertThat(jsonRepresentation.isString("aSubMap"), is(false)); try { jsonRepresentation.getString("aSubMap"); fail(); } catch (final IllegalArgumentException e) { assertThat(e.getMessage(), is("'aSubMap' is not a string")); } }
@Test public void forMap() throws JsonParseException, JsonMappingException, IOException { assertThat(jsonRepresentation.isString("aSubMap"), is(false)); try { jsonRepresentation.getString("aSubMap"); fail(); } catch (final IllegalArgumentException e) { assertThat(e.getMessage(), is("'aSubMap' 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 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 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 whenBigDecimal() throws Exception { allowingObjectSpecToReturnSpecIdFor(BigDecimal.class); allowingObjectAdapterToReturn(new BigDecimal("12345678901234567890.1234")); JsonValueEncoder.appendValueAndFormat(mockObjectSpec, mockObjectAdapter, representation, "big-decimal(27,4)"); assertThat(representation.isString("value"), is(true)); assertThat(representation.isBigDecimal("value"), is(true)); assertThat(representation.getBigDecimal("value"), is(new BigDecimal("12345678901234567890.1234"))); assertThat(representation.getString("format"), is("big-decimal(27,4)")); assertThat(representation.getString("extensions.x-isis-format"), is("javamathbigdecimal")); }
@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 whenString() throws Exception { allowingObjectSpecToReturnSpecIdFor(String.class); allowingObjectAdapterToReturn("aString"); JsonValueEncoder.appendValueAndFormat(mockObjectSpec, mockObjectAdapter, representation, null); assertThat(representation.isString("value"), is(true)); assertThat(representation.getString("value"), is("aString")); assertThat(representation.getString("format"), is(nullValue())); assertThat(representation.getString("extensions.x-isis-format"), is("string")); }
@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 whenCharPrimitive() throws Exception { allowingObjectSpecToReturnSpecIdFor(char.class); allowingObjectAdapterToReturn('a'); JsonValueEncoder.appendValueAndFormat(mockObjectSpec, mockObjectAdapter, representation, null); 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 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 whenJavaSqlDate() throws Exception { allowingObjectSpecToReturnSpecIdFor(java.sql.Date.class); allowingObjectAdapterToReturn(asSqlDate("2014-04-25")); JsonValueEncoder.appendValueAndFormat(mockObjectSpec, mockObjectAdapter, representation, null); 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("javasqldate")); }
@Test public void whenJavaSqlDate() throws Exception { allowingObjectSpecToReturnSpecIdFor(java.sql.Date.class); allowingObjectAdapterToReturn(asSqlDate("2014-04-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("javasqldate")); }