private static String toJson(DataResponse<?> builder) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); try (JsonGenerator generator = new JacksonService().getJsonFactory().createJsonGenerator(out, JsonEncoding.UTF8)) { builder.getEncoder().encode(null, builder.getObjects(), generator); } return new String(out.toByteArray(), "UTF-8"); }
@Test public void testEncoder_FilterNoMatch() throws IOException { filters.add( new EncoderFilter() { @Override public boolean matches(ClientEntity<?> clientEntity) { return false; } @Override public boolean encode( String propertyName, Object object, JsonGenerator out, Encoder delegate) throws IOException { fail("Non matching filter was not supposed to be invoked"); return false; } @Override public boolean willEncode(String propertyName, Object object, Encoder delegate) { fail("Non matching filter was not supposed to be invoked"); return false; } }); ClientEntity<E2> descriptor = getClientEntity(E2.class); descriptor.setIdIncluded(true); DataResponse<E2> builder = DataResponse.forType(E2.class).withClientEntity(descriptor); encoderService.makeEncoder(builder); ObjectContext context = cayenneService.newContext(); E2 e21 = new E2(); e21.setObjectId(new ObjectId("E2", E2.ID_PK_COLUMN, 7)); e21.setName("XYZ"); e21.setAddress("bla bla street"); context.registerNewObject(e21); builder.getEncoder().encode(null, Collections.singletonList(e21), mock(JsonGenerator.class)); }
private String responseContents(DataResponse<?> response, PushPopVisitor visitor) { response.getEncoder().visitEntities(response.getObjects(), visitor); return visitor.ids.stream().collect(joining(";")); }