View Javadoc

1   package com.atlassian.marketplace.client.impl;
2   
3   import java.io.ByteArrayInputStream;
4   import java.io.ByteArrayOutputStream;
5   
6   import com.atlassian.fugue.Option;
7   import com.atlassian.marketplace.client.MpacException;
8   import com.atlassian.marketplace.client.encoding.SchemaViolation;
9   import com.atlassian.utt.matchers.NamedFunction;
10  import com.atlassian.utt.reflect.ReflectionFunctions;
11  
12  import org.hamcrest.Matcher;
13  
14  import static org.hamcrest.MatcherAssert.assertThat;
15  import static org.hamcrest.Matchers.allOf;
16  import static org.hamcrest.Matchers.contains;
17  import static org.hamcrest.Matchers.isA;
18  
19  abstract class BaseJsonTests
20  {
21      protected JsonEntityEncoding encoding = new JsonEntityEncoding();
22      
23      protected <T> T decode(String json, Class<T> type) throws Exception
24      {
25          return encoding.decode(new ByteArrayInputStream(json.getBytes()), type);
26      }
27      
28      protected <T> String encode(T t) throws Exception
29      {
30          return encode(t, false);
31      }
32  
33      protected <T> String encode(T t, boolean includeReadOnly) throws Exception
34      {
35          ByteArrayOutputStream os = new ByteArrayOutputStream();
36          encoding.encode(os, t, includeReadOnly);
37          return new String(os.toByteArray());
38      }
39      
40      @SuppressWarnings("unchecked")
41      protected void causeShouldBe(MpacException.InvalidResponseError e, Class<? extends SchemaViolation> cause,
42          Option<String> message)
43      {
44          Matcher<SchemaViolation> classCond = isA((Class<SchemaViolation>)cause);
45          Matcher<SchemaViolation> cond = classCond;
46          for (String s: message)
47          {
48              cond = allOf(classCond, svMessage.is(s));
49          }
50          assertThat(e.getSchemaViolations(), contains(cond));
51      }
52  
53      private static NamedFunction<SchemaViolation, String> svMessage =
54          ReflectionFunctions.accessor(SchemaViolation.class, String.class, "message");
55  }