AMImageSerializerImpl implements AMImageSerializer { @Override public AdaptiveMedia<AMImageProcessor> deserialize( String s, Supplier<InputStream> inputStreamSupplier) { try { JSONObject jsonObject = JSONFactoryUtil.createJSONObject(s); Map<String, String> properties = new HashMap<>(); JSONObject attributesJSONObject = jsonObject.getJSONObject( "attributes"); Map<String, AMAttribute<?, ?>> allowedAMAttributes = AMImageAttribute.getAllowedAMAttributes(); allowedAMAttributes.forEach( (name, amAttribute) -> { if (attributesJSONObject.has(name)) { properties.put( name, attributesJSONObject.getString(name)); } }); String uri = jsonObject.getString("uri"); return new AMImage( inputStreamSupplier, AMImageAttributeMapping.fromProperties(properties), URI.create(uri)); } catch (JSONException jsonException) { throw new AMRuntimeException(jsonException); } }  @Override AdaptiveMedia<AMImageProcessor> deserialize(
		String s, Supplier<InputStream> inputStreamSupplier); @Override String serialize(AdaptiveMedia<AMImageProcessor> adaptiveMedia);  }
@Test public void testDeserialize() throws Exception { JSONObject jsonObject = JSONUtil.put("uri", "http: jsonObject.put( "attributes", JSONUtil.put( AMImageAttribute.AM_IMAGE_ATTRIBUTE_HEIGHT.getName(), "200" ).put( AMImageAttribute.AM_IMAGE_ATTRIBUTE_WIDTH.getName(), "300" )); AMImageSerializer amImageSerializer = new AMImageSerializerImpl(); InputStream inputStream = Mockito.mock(InputStream.class); AdaptiveMedia<AMImageProcessor> adaptiveMedia = amImageSerializer.deserialize( jsonObject.toString(), () -> inputStream); Assert.assertEquals( new URI("http: Optional<Integer> heightValueOptional = adaptiveMedia.getValueOptional( AMImageAttribute.AM_IMAGE_ATTRIBUTE_HEIGHT); Assert.assertEquals("200", String.valueOf(heightValueOptional.get())); Optional<Integer> widthValueOptional = adaptiveMedia.getValueOptional( AMImageAttribute.AM_IMAGE_ATTRIBUTE_WIDTH); Assert.assertEquals("300", String.valueOf(widthValueOptional.get())); }
@Test(expected = AMRuntimeException.class) public void testDeserializeInvalidString() throws Exception { String invalidString = RandomTestUtil.randomString(); AMImageSerializer amImageSerializer = new AMImageSerializerImpl(); InputStream inputStream = Mockito.mock(InputStream.class); amImageSerializer.deserialize(invalidString, () -> inputStream); }
@Test public void testDeserializeWithEmptyAttributes() throws Exception { JSONObject jsonObject = JSONUtil.put("uri", "http: JSONObject attributesJSONObject = JSONFactoryUtil.createJSONObject(); jsonObject.put("attributes", attributesJSONObject); AMImageSerializer amImageSerializer = new AMImageSerializerImpl(); InputStream inputStream = Mockito.mock(InputStream.class); AdaptiveMedia<AMImageProcessor> adaptiveMedia = amImageSerializer.deserialize( jsonObject.toString(), () -> inputStream); Assert.assertEquals( new URI("http: Optional<Integer> heightValueOptional = adaptiveMedia.getValueOptional( AMImageAttribute.AM_IMAGE_ATTRIBUTE_HEIGHT); Assert.assertFalse(heightValueOptional.isPresent()); Optional<Integer> widthValueOptional = adaptiveMedia.getValueOptional( AMImageAttribute.AM_IMAGE_ATTRIBUTE_WIDTH); Assert.assertFalse(widthValueOptional.isPresent()); }