|
| 1 | +package org.jooby.json; |
| 2 | + |
| 3 | +import static org.easymock.EasyMock.expect; |
| 4 | + |
| 5 | +import org.jooby.MediaType; |
| 6 | +import org.jooby.Parser; |
| 7 | +import org.jooby.Parser.Context; |
| 8 | +import org.jooby.test.MockUnit; |
| 9 | +import org.junit.Test; |
| 10 | + |
| 11 | +import com.fasterxml.jackson.databind.JavaType; |
| 12 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 13 | +import com.google.inject.TypeLiteral; |
| 14 | + |
| 15 | +public class JacksonParserTest { |
| 16 | + |
| 17 | + @Test |
| 18 | + public void parseAny() throws Exception { |
| 19 | + Object value = new Object(); |
| 20 | + new MockUnit(ObjectMapper.class, Parser.Context.class, MediaType.class) |
| 21 | + .expect(unit -> { |
| 22 | + MediaType type = unit.get(MediaType.class); |
| 23 | + expect(type.isAny()).andReturn(true); |
| 24 | + |
| 25 | + Context ctx = unit.get(Parser.Context.class); |
| 26 | + expect(ctx.type()).andReturn(type); |
| 27 | + expect(ctx.next()).andReturn(value); |
| 28 | + }) |
| 29 | + .run(unit -> { |
| 30 | + new JacksonParser(unit.get(ObjectMapper.class), MediaType.json) |
| 31 | + .parse(TypeLiteral.get(JacksonParserTest.class), unit.get(Parser.Context.class)); |
| 32 | + }); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + public void parseSkip() throws Exception { |
| 37 | + Object value = new Object(); |
| 38 | + new MockUnit(ObjectMapper.class, Parser.Context.class, MediaType.class, TypeLiteral.class) |
| 39 | + .expect(unit -> { |
| 40 | + MediaType type = unit.get(MediaType.class); |
| 41 | + expect(type.isAny()).andReturn(false); |
| 42 | + |
| 43 | + Context ctx = unit.get(Parser.Context.class); |
| 44 | + expect(ctx.type()).andReturn(type); |
| 45 | + expect(ctx.next()).andReturn(value); |
| 46 | + |
| 47 | + JavaType javaType = unit.mock(JavaType.class); |
| 48 | + |
| 49 | + ObjectMapper mapper = unit.get(ObjectMapper.class); |
| 50 | + expect(mapper.constructType(null)).andReturn(javaType); |
| 51 | + }) |
| 52 | + .run(unit -> { |
| 53 | + new JacksonParser(unit.get(ObjectMapper.class), MediaType.json) |
| 54 | + .parse(unit.get(TypeLiteral.class), unit.get(Parser.Context.class)); |
| 55 | + }); |
| 56 | + } |
| 57 | +} |
0 commit comments