|
| 1 | +package com.microsoft.windowsazure.services.media.implementation.templates.widevine; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertNotNull; |
| 5 | +import static org.junit.Assert.assertNull; |
| 6 | +import java.io.IOException; |
| 7 | +import java.net.URISyntaxException; |
| 8 | +import javax.xml.bind.JAXBException; |
| 9 | + |
| 10 | +import org.junit.Test; |
| 11 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 12 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 13 | + |
| 14 | +public class WidevineMessageSerializerTests { |
| 15 | + |
| 16 | + @Test |
| 17 | + public void RoundTripTest() throws JAXBException, URISyntaxException, IOException { |
| 18 | + ObjectMapper mapper = new ObjectMapper(); |
| 19 | + WidevineMessage message = new WidevineMessage(); |
| 20 | + message.allowed_track_types = AllowedTrackTypes.SD_HD; |
| 21 | + ContentKeySpecs ckspecs = new ContentKeySpecs(); |
| 22 | + message.content_key_specs = new ContentKeySpecs[] { ckspecs }; |
| 23 | + ckspecs.required_output_protection = new RequiredOutputProtection(); |
| 24 | + ckspecs.required_output_protection.hdcp = Hdcp.HDCP_NONE; |
| 25 | + ckspecs.security_level = 1; |
| 26 | + ckspecs.track_type = "SD"; |
| 27 | + message.policy_overrides = new Object() { |
| 28 | + public boolean can_play = true; |
| 29 | + public boolean can_persist = true; |
| 30 | + public boolean can_renew = false; |
| 31 | + }; |
| 32 | + |
| 33 | + String json = mapper.writeValueAsString(message); |
| 34 | + |
| 35 | + WidevineMessage result = mapper.readValue(json, WidevineMessage.class); |
| 36 | + |
| 37 | + assertEqualsWidevineMessage(message, result); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void FromJsonTest() throws JAXBException, URISyntaxException, JsonProcessingException { |
| 42 | + String expected = "{\"allowed_track_types\":\"SD_HD\",\"content_key_specs\":[{\"track_type\":\"SD\",\"key_id\":null,\"security_level\":1,\"required_output_protection\":{\"hdcp\":\"HDCP_NONE\"}}],\"policy_overrides\":{\"can_play\":true,\"can_persist\":true,\"can_renew\":false}}"; |
| 43 | + ObjectMapper mapper = new ObjectMapper(); |
| 44 | + WidevineMessage message = new WidevineMessage(); |
| 45 | + message.allowed_track_types = AllowedTrackTypes.SD_HD; |
| 46 | + ContentKeySpecs ckspecs = new ContentKeySpecs(); |
| 47 | + message.content_key_specs = new ContentKeySpecs[] { ckspecs }; |
| 48 | + ckspecs.required_output_protection = new RequiredOutputProtection(); |
| 49 | + ckspecs.required_output_protection.hdcp = Hdcp.HDCP_NONE; |
| 50 | + ckspecs.security_level = 1; |
| 51 | + ckspecs.track_type = "SD"; |
| 52 | + message.policy_overrides = new Object() { |
| 53 | + public boolean can_play = true; |
| 54 | + public boolean can_persist = true; |
| 55 | + public boolean can_renew = false; |
| 56 | + }; |
| 57 | + |
| 58 | + String json = mapper.writeValueAsString(message); |
| 59 | + |
| 60 | + assertEquals(expected, json); |
| 61 | + } |
| 62 | + |
| 63 | + private static void assertEqualsWidevineMessage(WidevineMessage expected, WidevineMessage actual) { |
| 64 | + assertEquals(expected.allowed_track_types, actual.allowed_track_types); |
| 65 | + if (expected.content_key_specs == null) { |
| 66 | + assertNull(actual.content_key_specs); |
| 67 | + } else { |
| 68 | + assertNotNull(actual.content_key_specs); |
| 69 | + assertEquals(expected.content_key_specs.length, actual.content_key_specs.length); |
| 70 | + for(int i = 0; i < expected.content_key_specs.length; i++) { |
| 71 | + ContentKeySpecs expectedCks = expected.content_key_specs[i]; |
| 72 | + ContentKeySpecs actualCks = actual.content_key_specs[i]; |
| 73 | + assertEquals(expectedCks.key_id, actualCks.key_id); |
| 74 | + assertEquals(expectedCks.security_level, actualCks.security_level); |
| 75 | + assertEquals(expectedCks.track_type, actualCks.track_type); |
| 76 | + if (expectedCks.required_output_protection != null) { |
| 77 | + assertNotNull(actualCks.required_output_protection); |
| 78 | + assertEquals(expectedCks.required_output_protection.hdcp, actualCks.required_output_protection.hdcp); |
| 79 | + } else { |
| 80 | + assertNull(actualCks.required_output_protection); |
| 81 | + } |
| 82 | + assertEquals(expectedCks.key_id, actualCks.key_id); |
| 83 | + } |
| 84 | + } |
| 85 | + if (expected.policy_overrides == null) { |
| 86 | + assertNull(actual.policy_overrides); |
| 87 | + } else { |
| 88 | + assertNotNull(actual.policy_overrides); |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments