You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by ah...@apache.org on 2022/08/19 11:07:13 UTC
[isis] branch master updated: ISIS-3127: [RO] converts JsonValueConverter mockup-tests to integration- tests
This is an automated email from the ASF dual-hosted git repository.
ahuber pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/isis.git
The following commit(s) were added to refs/heads/master by this push:
new 05dcc60a59 ISIS-3127: [RO] converts JsonValueConverter mockup-tests to integration- tests
05dcc60a59 is described below
commit 05dcc60a59fcc40b86af3310aec52781495c8422
Author: andi-huber <ah...@apache.org>
AuthorDate: Fri Aug 19 13:07:02 2022 +0200
ISIS-3127: [RO] converts JsonValueConverter mockup-tests to integration-
tests
---
.../isis/commons/internal/base/_Temporals.java | 6 +
.../isis/testdomain/rest/JsonValueEncoderTest.java | 352 ++++++++++++++++-
.../domainobjects/JsonValueConverters.java | 8 +-
.../JsonValueEncoderTest_appendValueAndFormat.java | 439 ---------------------
4 files changed, 346 insertions(+), 459 deletions(-)
diff --git a/commons/src/main/java/org/apache/isis/commons/internal/base/_Temporals.java b/commons/src/main/java/org/apache/isis/commons/internal/base/_Temporals.java
index 292d001436..29b3bcecc1 100644
--- a/commons/src/main/java/org/apache/isis/commons/internal/base/_Temporals.java
+++ b/commons/src/main/java/org/apache/isis/commons/internal/base/_Temporals.java
@@ -237,6 +237,12 @@ public final class _Temporals {
: null;
}
+ // -- ISO UTILS
+
+ public static OffsetDateTime parseIsoDateTime(final String isoDateTime) {
+ return DateTimeFormatter.ISO_DATE_TIME.parse(isoDateTime, OffsetDateTime::from);
+ }
+
// -- TEMPORAL SAMPLERS
public Can<LocalDateTime> sampleLocalDateTime() {
diff --git a/regressiontests/stable-rest/src/test/java/org/apache/isis/testdomain/rest/JsonValueEncoderTest.java b/regressiontests/stable-rest/src/test/java/org/apache/isis/testdomain/rest/JsonValueEncoderTest.java
index 9d2b730c5a..318437a30d 100644
--- a/regressiontests/stable-rest/src/test/java/org/apache/isis/testdomain/rest/JsonValueEncoderTest.java
+++ b/regressiontests/stable-rest/src/test/java/org/apache/isis/testdomain/rest/JsonValueEncoderTest.java
@@ -18,15 +18,19 @@
*/
package org.apache.isis.testdomain.rest;
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.time.Instant;
+
import javax.inject.Inject;
import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.condition.DisabledIfSystemProperty;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;
import org.apache.isis.applib.value.Blob;
import org.apache.isis.applib.value.NamedWithMimeType.CommonMimeType;
+import org.apache.isis.commons.internal.base._Temporals;
import org.apache.isis.core.config.presets.IsisPresets;
import org.apache.isis.core.metamodel.context.MetaModelContext;
import org.apache.isis.testdomain.conf.Configuration_headless;
@@ -35,43 +39,357 @@ import org.apache.isis.viewer.restfulobjects.jaxrsresteasy4.IsisModuleViewerRest
import org.apache.isis.viewer.restfulobjects.rendering.domainobjects.JsonValueConverter.Context;
import org.apache.isis.viewer.restfulobjects.rendering.domainobjects.JsonValueEncoder;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.nullValue;
+
import lombok.val;
@SpringBootTest(classes={
Configuration_headless.class,
IsisModuleViewerRestfulObjectsJaxrsResteasy4.class
})
-@DisabledIfSystemProperty(named = "isRunningWithSurefire", matches = "true") //TODO WIP
+//@DisabledIfSystemProperty(named = "isRunningWithSurefire", matches = "true") //TODO WIP
@TestPropertySource(IsisPresets.UseLog4j2Test)
class JsonValueEncoderTest {
@Inject MetaModelContext mmc;
@Test
- void whenBlob() {
- val value = Blob.of("a Blob", CommonMimeType.BIN, new byte[] {1, 2, 3});
- val valueAdapter = mmc.getObjectManager().adapt(value);
+ public void whenString() {
+ val value = "aString";
+ val representation = representationFor(value);
- val jsonValueEncoder = JsonValueEncoder.forTesting(mmc.getSpecificationLoader());
- val representation = JsonRepresentation.newMap();
- jsonValueEncoder.appendValueAndFormat(valueAdapter, representation, defaultContext());
+ assertThat(representation.isString("value"), is(true));
+ assertThat(representation.getString("value"), is("aString"));
- System.err.printf("representation %s%n", representation);
+ assertThat(representation.getString("format"), is(nullValue()));
+ assertThat(representation.getString("extensions.x-isis-format"), is("string"));
+ }
+
+ @Test
+ public void whenBooleanWrapper() {
+ val value = Boolean.TRUE;
+ val representation = representationFor(value);
+
+ assertThat(representation.isBoolean("value"), is(true));
+ assertThat(representation.getBoolean("value"), is(Boolean.TRUE));
+
+ assertThat(representation.getString("format"), is(nullValue()));
+ }
+
+ @Test
+ public void whenBooleanPrimitive() {
+ val value = true;
+ val representation = representationFor(value);
+
+ assertThat(representation.isBoolean("value"), is(true));
+ assertThat(representation.getBoolean("value"), is(true));
+
+ assertThat(representation.getString("format"), is(nullValue()));
+ }
+
+ @Test
+ public void whenByteWrapper() {
+ val value = Byte.valueOf((byte)123);
+ val representation = representationFor(value);
+
+ 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 whenBytePrimitive() {
+ val value = (byte)123;
+ val representation = representationFor(value);
+
+ assertThat(representation.isIntegralNumber("value"), is(true));
+ assertThat(representation.getByte("value"), is((byte)123));
+
+ assertThat(representation.getString("format"), is("int"));
+ assertThat(representation.getString("extensions.x-isis-format"), is("byte"));
+ }
+
+ @Test
+ public void whenShortWrapper() {
+ val value = Short.valueOf((short)12345);
+ val representation = representationFor(value);
+
+ 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 whenShortPrimitive() {
+ val value = (short)12345;
+ val representation = representationFor(value);
- //representation.getString("value");
+ assertThat(representation.isIntegralNumber("value"), is(true));
+ assertThat(representation.getShort("value"), is((short)12345));
- //representation.isMap("value");
+ assertThat(representation.getString("format"), is("int"));
+ assertThat(representation.getString("extensions.x-isis-format"), is("short"));
+ }
+
+ @Test
+ public void whenIntWrapper() {
+ val value = Integer.valueOf(12345678);
+ val representation = representationFor(value);
+
+ 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 whenIntPrimitive() {
+ val value = 12345678;
+ val representation = representationFor(value);
+
+ 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 whenLongWrapper() {
+ val value = Long.valueOf(12345678901234L);
+ val representation = representationFor(value);
+
+ assertThat(representation.isIntegralNumber("value"), is(true));
+ assertThat(representation.getLong("value"), is(Long.valueOf(12345678901234L)));
+
+ assertThat(representation.getString("format"), is("int"));
+ assertThat(representation.getString("extensions.x-isis-format"), is("long"));
+ }
+
+ @Test
+ public void whenLongPrimitive() {
+ val value = 12345678901234L;
+ val representation = representationFor(value);
- //assertTrue(representation.isString("value"));
+ assertThat(representation.isIntegralNumber("value"), is(true));
+ assertThat(representation.getLong("value"), is(12345678901234L));
- //assertEquals(new BigDecimal("12345678901234567890.1234"), representation.getBigDecimal("value"));
- //assertEquals("big-decimal(27,4)", representation.getString("format"));
- //assertEquals("javamathbigdecimal", representation.getString("extensions.x-isis-format"));
+ assertThat(representation.getString("format"), is("int"));
+ assertThat(representation.getString("extensions.x-isis-format"), is("long"));
+ }
+
+ @Test
+ public void whenFloatWrapper() {
+ val value = Float.valueOf((float)123.45);
+ val representation = representationFor(value);
+
+ 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 whenFloatPrimitive() {
+ val value = (float)123.45;
+ val representation = representationFor(value);
+
+ 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 whenDoubleWrapper() {
+ val value = Double.valueOf(12345.6789);
+ val representation = representationFor(value);
+
+ assertThat(representation.isDecimal("value"), is(true));
+ assertThat(representation.getDouble("value"), is(Double.valueOf(12345.6789)));
+
+ assertThat(representation.getString("format"), is("decimal"));
+ assertThat(representation.getString("extensions.x-isis-format"), is("double"));
+ }
+
+ @Test
+ public void whenDoublePrimitive() {
+ val value = 12345.6789;
+ val representation = representationFor(value);
+
+ assertThat(representation.isDecimal("value"), is(true));
+ assertThat(representation.getDouble("value"), is(12345.6789));
+
+ assertThat(representation.getString("format"), is("decimal"));
+ assertThat(representation.getString("extensions.x-isis-format"), is("double"));
+ }
+
+ @Test
+ public void whenCharWrapper() {
+ val value = Character.valueOf('a');
+ val representation = representationFor(value);
+
+ 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 whenCharPrimitive() {
+ val value = 'a';
+ val representation = representationFor(value);
+
+ 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 whenJavaSqlTimestamp() {
+
+ val epochMilli = defaultInstant().toEpochMilli();
+ val value = new java.sql.Timestamp(epochMilli);
+ val representation = representationFor(value);
+
+ assertThat(representation.isLong("value"), is(true));
+ assertThat(representation.getLong("value"), is(epochMilli));
+
+ assertThat(representation.getString("format"), is("utc-millisec"));
+ assertThat(representation.getString("extensions.x-isis-format"), is("javasqltimestamp"));
+ }
+
+ @Test
+ public void whenBigInteger() {
+ val value = new BigInteger("12345678901234567890");
+ //"big-integer(22)"
+ val representation = representationFor(value, Context.forTesting(22, 0));
+
+ 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() {
+ val value = new BigDecimal("12345678901234567890.1234");
+ //"big-decimal(27,4)"
+ val representation = representationFor(value, Context.forTesting(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"));
+ }
+
+ // -- TEMPORALS
+
+ @Test
+ public void whenJavaUtilDate() {
+ val value = java.util.Date.from(defaultInstant());
+ val representation = representationFor(value);
+
+ 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() {
+ val value = new java.sql.Date(defaultInstant().toEpochMilli());
+ val representation = representationFor(value);
+
+ 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 whenJodaDateTime() {
+ val value = new org.joda.time.DateTime(defaultInstant().toEpochMilli());
+ val representation = representationFor(value);
+
+ 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("jodadatetime"));
+ }
+
+ @Test
+ public void whenJodaLocalDateTime() {
+ val value = new org.joda.time.LocalDateTime(defaultInstant().toEpochMilli());
+ val representation = representationFor(value);
+
+ 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("jodalocaldatetime"));
+ }
+
+ @Test
+ public void whenJodaLocalDate() {
+ val value = new org.joda.time.LocalDate(2014,4,25);
+ val representation = representationFor(value);
+
+ 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
+ void whenBlob() {
+ val value = Blob.of("a Blob", CommonMimeType.BIN, new byte[] {1, 2, 3});
+ val representation = representationFor(value);
+
+ System.err.printf("representation %s%n", representation);
+ //TODO convert repr. to map like
+ }
+
+ private JsonRepresentation representationFor(final Object value) {
+ return representationFor(value, defaultContext());
+ }
+
+ private JsonRepresentation representationFor(final Object value, final Context context) {
+ val valueAdapter = mmc.getObjectManager().adapt(value);
+ val jsonValueEncoder = JsonValueEncoder.forTesting(mmc.getSpecificationLoader());
+ val representation = JsonRepresentation.newMap();
+ jsonValueEncoder.appendValueAndFormat(valueAdapter, representation, context);
+ return representation;
}
private Context defaultContext() {
- // TODO Auto-generated method stub
- return null;
+ return Context.forTesting(null, null);
+ }
+
+ private Instant defaultInstant() {
+ return _Temporals.parseIsoDateTime("2014-04-25T12:34:45Z")
+ .toInstant();
}
}
diff --git a/viewers/restfulobjects/rendering/src/main/java/org/apache/isis/viewer/restfulobjects/rendering/domainobjects/JsonValueConverters.java b/viewers/restfulobjects/rendering/src/main/java/org/apache/isis/viewer/restfulobjects/rendering/domainobjects/JsonValueConverters.java
index 1c22952e2f..e3c2d59913 100644
--- a/viewers/restfulobjects/rendering/src/main/java/org/apache/isis/viewer/restfulobjects/rendering/domainobjects/JsonValueConverters.java
+++ b/viewers/restfulobjects/rendering/src/main/java/org/apache/isis/viewer/restfulobjects/rendering/domainobjects/JsonValueConverters.java
@@ -30,6 +30,7 @@ import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;
+import org.apache.isis.applib.value.semantics.ValueSemanticsProvider;
import org.apache.isis.commons.internal.collections._Lists;
import org.apache.isis.core.metamodel.spec.ManagedObject;
import org.apache.isis.viewer.restfulobjects.applib.JsonRepresentation;
@@ -37,10 +38,11 @@ import org.apache.isis.viewer.restfulobjects.applib.JsonRepresentation;
import lombok.val;
/**
- * Similar to Isis' value encoding, but with additional support for JSON
- * primitives.
+ * Has support for JSON primitives.
+ * @deprecated should be covered 100% per {@link ValueSemanticsProvider}
*/
-public final class JsonValueConverters {
+@Deprecated
+final class JsonValueConverters {
public List<JsonValueConverter> asList() {
diff --git a/viewers/restfulobjects/rendering/src/test/java/org/apache/isis/viewer/restfulobjects/rendering/domainobjects/JsonValueEncoderTest_appendValueAndFormat.java b/viewers/restfulobjects/rendering/src/test/java/org/apache/isis/viewer/restfulobjects/rendering/domainobjects/JsonValueEncoderTest_appendValueAndFormat.java
deleted file mode 100644
index ab7c5c312b..0000000000
--- a/viewers/restfulobjects/rendering/src/test/java/org/apache/isis/viewer/restfulobjects/rendering/domainobjects/JsonValueEncoderTest_appendValueAndFormat.java
+++ /dev/null
@@ -1,439 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.isis.viewer.restfulobjects.rendering.domainobjects;
-
-import java.math.BigDecimal;
-import java.math.BigInteger;
-import java.sql.Timestamp;
-import java.util.Date;
-
-import org.jmock.Expectations;
-import org.jmock.auto.Mock;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Rule;
-import org.junit.Test;
-
-import org.apache.isis.applib.id.LogicalType;
-import org.apache.isis.core.internaltestsupport.jmocking.JUnitRuleMockery2;
-import org.apache.isis.core.metamodel.facets.object.value.ValueFacet;
-import org.apache.isis.core.metamodel.spec.ManagedObject;
-import org.apache.isis.core.metamodel.spec.ObjectSpecification;
-import org.apache.isis.core.metamodel.specloader.SpecificationLoader;
-import org.apache.isis.viewer.restfulobjects.applib.JsonRepresentation;
-import org.apache.isis.viewer.restfulobjects.rendering.domainobjects.JsonValueConverter.Context;
-
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.is;
-import static org.hamcrest.Matchers.nullValue;
-
-public class JsonValueEncoderTest_appendValueAndFormat {
-
- @Rule public JUnitRuleMockery2 context =
- JUnitRuleMockery2.createFor(JUnitRuleMockery2.Mode.INTERFACES_AND_CLASSES);
-
- @Mock private ObjectSpecification mockObjectSpec;
- @Mock private ValueFacet mockValueFacet;
- @Mock private ManagedObject mockObjectAdapter;
- @Mock private SpecificationLoader specLoader;
-
- private JsonRepresentation representation;
- private JsonValueEncoder jsonValueEncoder;
-
- @Before
- public void setUp() {
-
- jsonValueEncoder = JsonValueEncoder.forTesting(specLoader);
-
- representation = JsonRepresentation.newMap();
- }
-
- @After
- public void tearDown() {
-
- }
-
- @Test
- public void whenString() {
- allowingLogicalTypeReturnObjectTypeFor(String.class);
- allowingObjectAdapterToReturn("aString");
-
- jsonValueEncoder.appendValueAndFormat(mockObjectAdapter, representation, defaultContext());
- 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 whenBooleanWrapper() {
- allowingLogicalTypeReturnObjectTypeFor(Boolean.class);
- allowingObjectAdapterToReturn(Boolean.TRUE);
-
- jsonValueEncoder.appendValueAndFormat(mockObjectAdapter, representation, defaultContext());
- assertThat(representation.isBoolean("value"), is(true));
- assertThat(representation.getBoolean("value"), is(Boolean.TRUE));
-
- assertThat(representation.getString("format"), is(nullValue()));
- }
-
- @Test
- public void whenBooleanPrimitive() {
- allowingLogicalTypeReturnObjectTypeFor(boolean.class);
- allowingObjectAdapterToReturn(true);
-
- jsonValueEncoder.appendValueAndFormat(mockObjectAdapter, representation, defaultContext());
- assertThat(representation.isBoolean("value"), is(true));
- assertThat(representation.getBoolean("value"), is(true));
-
- assertThat(representation.getString("format"), is(nullValue()));
- }
-
- @Test
- public void whenByteWrapper() {
- allowingLogicalTypeReturnObjectTypeFor(Byte.class);
- allowingObjectAdapterToReturn(Byte.valueOf((byte)123));
-
- jsonValueEncoder.appendValueAndFormat(mockObjectAdapter, representation, defaultContext());
- 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 whenBytePrimitive() {
- allowingLogicalTypeReturnObjectTypeFor(byte.class);
- allowingObjectAdapterToReturn((byte)123);
-
- jsonValueEncoder.appendValueAndFormat(mockObjectAdapter, representation, defaultContext());
- assertThat(representation.isIntegralNumber("value"), is(true));
- assertThat(representation.getByte("value"), is((byte)123));
-
- assertThat(representation.getString("format"), is("int"));
- assertThat(representation.getString("extensions.x-isis-format"), is("byte"));
- }
-
- @Test
- public void whenShortWrapper() {
- allowingLogicalTypeReturnObjectTypeFor(Short.class);
- allowingObjectAdapterToReturn(Short.valueOf((short)12345));
-
- jsonValueEncoder.appendValueAndFormat(mockObjectAdapter, representation, defaultContext());
- 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 whenShortPrimitive() {
- allowingLogicalTypeReturnObjectTypeFor(short.class);
- allowingObjectAdapterToReturn((short)12345);
-
- jsonValueEncoder.appendValueAndFormat(mockObjectAdapter, representation, defaultContext());
- 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"));
- }
-
- @Test
- public void whenIntWrapper() {
- allowingLogicalTypeReturnObjectTypeFor(Integer.class);
- allowingObjectAdapterToReturn(Integer.valueOf(12345678));
-
- jsonValueEncoder.appendValueAndFormat(mockObjectAdapter, representation, defaultContext());
- 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 whenIntPrimitive() {
- allowingLogicalTypeReturnObjectTypeFor(int.class);
- allowingObjectAdapterToReturn(12345678);
-
- jsonValueEncoder.appendValueAndFormat(mockObjectAdapter, representation, defaultContext());
- 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 whenLongWrapper() {
- allowingLogicalTypeReturnObjectTypeFor(Long.class);
- allowingObjectAdapterToReturn(Long.valueOf(12345678901234L));
-
- jsonValueEncoder.appendValueAndFormat(mockObjectAdapter, representation, defaultContext());
- assertThat(representation.isIntegralNumber("value"), is(true));
- assertThat(representation.getLong("value"), is(Long.valueOf(12345678901234L)));
-
- assertThat(representation.getString("format"), is("int"));
- assertThat(representation.getString("extensions.x-isis-format"), is("long"));
- }
-
- @Test
- public void whenLongPrimitive() {
- allowingLogicalTypeReturnObjectTypeFor(long.class);
- allowingObjectAdapterToReturn(12345678901234L);
-
- jsonValueEncoder.appendValueAndFormat(mockObjectAdapter, representation, defaultContext());
- assertThat(representation.isIntegralNumber("value"), is(true));
- assertThat(representation.getLong("value"), is(12345678901234L));
-
- assertThat(representation.getString("format"), is("int"));
- assertThat(representation.getString("extensions.x-isis-format"), is("long"));
- }
-
- @Test
- public void whenFloatWrapper() {
- allowingLogicalTypeReturnObjectTypeFor(Float.class);
- allowingObjectAdapterToReturn(Float.valueOf((float)123.45));
-
- jsonValueEncoder.appendValueAndFormat(mockObjectAdapter, representation, defaultContext());
- 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 whenFloatPrimitive() {
- allowingLogicalTypeReturnObjectTypeFor(Float.class);
- allowingObjectAdapterToReturn((float)123.45);
-
- jsonValueEncoder.appendValueAndFormat(mockObjectAdapter, representation, defaultContext());
- 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 whenDoubleWrapper() {
- allowingLogicalTypeReturnObjectTypeFor(Double.class);
- allowingObjectAdapterToReturn(Double.valueOf(12345.6789));
-
- jsonValueEncoder.appendValueAndFormat(mockObjectAdapter, representation, defaultContext());
- assertThat(representation.isDecimal("value"), is(true));
- assertThat(representation.getDouble("value"), is(Double.valueOf(12345.6789)));
-
- assertThat(representation.getString("format"), is("decimal"));
- assertThat(representation.getString("extensions.x-isis-format"), is("double"));
- }
-
- @Test
- public void whenDoublePrimitive() {
- allowingLogicalTypeReturnObjectTypeFor(double.class);
- allowingObjectAdapterToReturn(12345.6789);
-
- jsonValueEncoder.appendValueAndFormat(mockObjectAdapter, representation, defaultContext());
- assertThat(representation.isDecimal("value"), is(true));
- assertThat(representation.getDouble("value"), is(12345.6789));
-
- assertThat(representation.getString("format"), is("decimal"));
- assertThat(representation.getString("extensions.x-isis-format"), is("double"));
- }
-
- @Test
- public void whenCharWrapper() {
- allowingLogicalTypeReturnObjectTypeFor(Character.class);
- allowingObjectAdapterToReturn(Character.valueOf('a'));
-
- jsonValueEncoder.appendValueAndFormat(mockObjectAdapter, representation, defaultContext());
- 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 whenCharPrimitive() {
- allowingLogicalTypeReturnObjectTypeFor(char.class);
- allowingObjectAdapterToReturn('a');
-
- jsonValueEncoder.appendValueAndFormat(mockObjectAdapter, representation, defaultContext());
- 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() {
- allowingLogicalTypeReturnObjectTypeFor(java.util.Date.class);
- allowingObjectAdapterToReturn(asDateTime("2014-04-25T12:34:45Z"));
-
- jsonValueEncoder.appendValueAndFormat(mockObjectAdapter, representation, defaultContext());
- 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() {
- allowingLogicalTypeReturnObjectTypeFor(java.sql.Date.class);
- allowingObjectAdapterToReturn(asSqlDate("2014-04-25"));
-
- jsonValueEncoder.appendValueAndFormat(mockObjectAdapter, representation, defaultContext());
- 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 whenJodaDateTime() {
- allowingLogicalTypeReturnObjectTypeFor(org.joda.time.DateTime.class);
- allowingObjectAdapterToReturn(new org.joda.time.DateTime(asDateTime("2014-04-25T12:34:45Z")));
-
- jsonValueEncoder.appendValueAndFormat(mockObjectAdapter, representation, defaultContext());
- 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("jodadatetime"));
- }
-
- @Test
- public void whenJodaLocalDateTime() {
- allowingLogicalTypeReturnObjectTypeFor(org.joda.time.LocalDateTime.class);
- allowingObjectAdapterToReturn(new org.joda.time.LocalDateTime(asDateTime("2014-04-25T12:34:45Z")));
-
- jsonValueEncoder.appendValueAndFormat(mockObjectAdapter, representation, defaultContext());
- 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("jodalocaldatetime"));
- }
-
- @Test
- public void whenJodaLocalDate() {
- allowingLogicalTypeReturnObjectTypeFor(org.joda.time.LocalDate.class);
- allowingObjectAdapterToReturn(new org.joda.time.LocalDate(2014,4,25));
-
- jsonValueEncoder.appendValueAndFormat(mockObjectAdapter, representation, defaultContext());
- 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 whenJavaSqlTimestamp() {
- allowingLogicalTypeReturnObjectTypeFor(java.sql.Timestamp.class);
- final long time = asDateTime("2014-04-25T12:34:45Z").getTime();
- allowingObjectAdapterToReturn(new Timestamp(time));
-
- jsonValueEncoder.appendValueAndFormat(mockObjectAdapter, representation, defaultContext());
- 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 whenBigInteger() {
- allowingLogicalTypeReturnObjectTypeFor(BigInteger.class);
- allowingObjectAdapterToReturn(new BigInteger("12345678901234567890"));
-
- //"big-integer(22)"
- jsonValueEncoder.appendValueAndFormat(mockObjectAdapter, representation, Context.forTesting(22, 0));
- 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() {
- allowingLogicalTypeReturnObjectTypeFor(BigDecimal.class);
- allowingObjectAdapterToReturn(new BigDecimal("12345678901234567890.1234"));
-
- //"big-decimal(27,4)"
- jsonValueEncoder.appendValueAndFormat(mockObjectAdapter, representation, Context.forTesting(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"));
- }
-
- private Context defaultContext() {
- return Context.forTesting(null, null);
- }
-
- private void allowingLogicalTypeReturnObjectTypeFor(final Class<?> cls) {
- context.checking(new Expectations() {
- {
- allowing(mockObjectSpec).getCorrespondingClass();
- will(returnValue(cls));
-
- allowing(mockObjectSpec).getLogicalType();
- will(returnValue(LogicalType.fqcn(cls)));
-
- allowing(mockObjectSpec).getFacet(ValueFacet.class);
- will(returnValue(null));
- }
- });
- }
-
- private void allowingObjectAdapterToReturn(final Object pojo) {
- context.checking(new Expectations() {
- {
- oneOf(mockObjectAdapter).getPojo();
- will(returnValue(pojo));
-
- allowing(mockObjectAdapter).getSpecification();
- will(returnValue(mockObjectSpec));
- }
- });
- }
-
- private static java.sql.Date asSqlDate(final String text) {
- return new java.sql.Date(JsonRepresentation.yyyyMMdd.parseDateTime(text).getMillis());
- }
-
- private static Date asDateTime(final String text) {
- return new java.util.Date(JsonRepresentation.yyyyMMddTHHmmssZ.parseDateTime(text).getMillis());
- }
-
-}