/*
 * Copyright 2010 Proofpoint, Inc.
 *
 * Licensed 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 com.proofpoint.platform.sample;

import com.google.common.base.Charsets;
import com.google.common.io.Resources;
import com.proofpoint.json.JsonCodec;
import org.testng.annotations.Test;

import static com.proofpoint.json.JsonCodec.jsonCodec;
import static org.testng.Assert.assertEquals;

public class TestPersonRepresentation
{
    private final JsonCodec<PersonRepresentation> codec = jsonCodec(PersonRepresentation.class);

    // TODO: add equivalence test

    @Test
    public void testJsonRoundTrip()
    {
        PersonRepresentation expected = new PersonRepresentation("alice@example.com", "Alice", null);
        String json = codec.toJson(expected);
        PersonRepresentation actual = codec.fromJson(json);
        assertEquals(actual, expected);
    }

    @Test
    public void testJsonDecode()
            throws Exception
    {
        PersonRepresentation expected = new PersonRepresentation("foo@example.com", "Mr Foo", null);

        String json = Resources.toString(Resources.getResource("single.json"), Charsets.UTF_8);
        PersonRepresentation actual = codec.fromJson(json);

        assertEquals(actual, expected);
    }
}

@@ -663,21 +663,28 @@
 mon.
-base.Charsets
+collect.ImmutableMap
 ;%0Aim
@@ -710,19 +710,19 @@
 mon.
-io.Resource
+collect.Map
 s;%0Ai
@@ -754,24 +754,68 @@
 .JsonCodec;%0A
+import org.testng.annotations.BeforeMethod;%0A
 import org.t
@@ -850,239 +850,787 @@
 ort 
-static com.proofpoint.json.JsonCodec.jsonCodec;%0Aimport static org.testng.Assert.assertEquals;%0A%0Apublic class TestPersonRepresentation%0A%7B%0A    private final JsonCodec%3CPersonRepresentation%3E codec = jsonCodec(PersonRepresentation.class);
+javax.validation.constraints.NotNull;%0Aimport javax.validation.constraints.Pattern;%0Aimport java.util.Map;%0A%0Aimport static com.proofpoint.json.JsonCodec.jsonCodec;%0Aimport static com.proofpoint.json.testing.JsonTester.decodeJson;%0Aimport static com.proofpoint.testing.ValidationAssertions.assertFailsValidation;%0Aimport static com.proofpoint.testing.ValidationAssertions.assertValidates;%0Aimport static org.testng.Assert.assertEquals;%0A%0Apublic class TestPersonRepresentation%0A%7B%0A    private final JsonCodec%3CPersonRepresentation%3E codec = jsonCodec(PersonRepresentation.class);%0A    private Map%3CString,String%3E map;%0A%0A    @BeforeMethod%0A    public void setup() %7B%0A        map = Maps.newHashMap(ImmutableMap.of(%0A                %22name%22, %22Mr Foo%22,%0A                %22email%22, %22foo@example.com%22));%0A    %7D
 %0A%0A  
@@ -2016,24 +2016,26 @@
 e()%0A    
+%7B%0A
         
 throws E
@@ -2030,93 +2030,185 @@
     
-throws Exception%0A    %7B%0A        PersonRepresentation expected = new PersonRepresentati
+PersonRepresentation personRepresentation = decodeJson(codec, map);%0A        assertValidates(personRepresentation);%0A        assertEquals(personRepresentation.toPerson(), new Pers
 on(%22
@@ -2237,175 +2237,541 @@
 Foo%22
-, null
+)
 );%0A
-%0A
     
+%7D%0A%0A
     
-String json = Resources.toString(Resources.getResource(%22single.json%22), Charsets.UTF_8
+@Test(enabled = false) // todo fails due to bug%0A    public void testNoEmail()%0A    %7B%0A        map.remove(%22email%22);%0A        assertFailsValidation(decodeJson(codec, map), %22email%22, %22is missing%22, NotNull.class);%0A    %7D%0A%0A    @Test%0A    public void testInvalidEmail()%0A    %7B%0A        map.put(%22email%22, %22invalid%22);%0A        assertFailsValidation(decodeJson(codec, map), %22email%22, %22is malformed%22, Pattern.class
 );%0A    
+%7D%0A%0A
     
-PersonRepresentation actual = codec.fromJson(json
+@Test(enabled = false) // todo fails due to bug%0A    public void testNoName()%0A    %7B%0A        map.remove(%22name%22
 );%0A
-%0A
     
@@ -2780,39 +2780,91 @@
   assert
-Equals(actual, expected
+FailsValidation(decodeJson(codec, map), %22name%22, %22is missing%22, NotNull.class
 );%0A    %7D

Use new Representation testing methodology