@Test public void validateDiffWithChangingComponentIdAndAdditionalElements() throws Exception { FlowSnippetDTO snippet = new FlowSnippetDTO(); Set<ProcessorDTO> procs = new HashSet<>(); for (int i = 4; i > 0; i--) { ProcessorDTO procDTO = new ProcessorDTO(); procDTO.setType("Processor" + i + ".class"); procDTO.setId(ComponentIdGenerator.generateId().toString()); procs.add(procDTO); } snippet.setProcessors(procs); TemplateDTO origTemplate = new TemplateDTO(); origTemplate.setDescription("MyTemplate"); origTemplate.setId("MyTemplate"); origTemplate.setSnippet(snippet); byte[] serTemplate = TemplateSerializer.serialize(origTemplate); ByteArrayInputStream in = new ByteArrayInputStream(serTemplate); JAXBContext context = JAXBContext.newInstance(TemplateDTO.class); Unmarshaller unmarshaller = context.createUnmarshaller(); JAXBElement<TemplateDTO> templateElement = unmarshaller.unmarshal(new StreamSource(in), TemplateDTO.class); TemplateDTO deserTemplate = templateElement.getValue(); FlowSnippetDTO deserSnippet = deserTemplate.getSnippet(); Set<ProcessorDTO> deserProcs = deserSnippet.getProcessors(); int c = 0; for (ProcessorDTO processorDTO : deserProcs) { if (c % 2 == 0) { processorDTO.setName("Hello-" + c); } c++; } ProcessorDTO procDTO = new ProcessorDTO(); procDTO.setType("ProcessorNew" + ".class"); procDTO.setId(ComponentIdGenerator.generateId().toString()); deserProcs.add(procDTO); byte[] serTemplate2 = TemplateSerializer.serialize(deserTemplate); RawText rt1 = new RawText(serTemplate); RawText rt2 = new RawText(serTemplate2); EditList diffList = new EditList(); diffList.addAll(new HistogramDiff().diff(RawTextComparator.DEFAULT, rt1, rt2)); ByteArrayOutputStream out = new ByteArrayOutputStream(); try (DiffFormatter diff = new DiffFormatter(out);) { diff.format(diffList, rt1, rt2); BufferedReader reader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(out.toByteArray()), StandardCharsets.UTF_8)); List<String> changes = reader.lines().peek(System.out::println) .filter(line -> line.startsWith("+") || line.startsWith("-")).collect(Collectors.toList()); assertEquals("+ <name>Hello-0</name>", changes.get(0)); assertEquals("+ <name>Hello-2</name>", changes.get(1)); assertEquals("+ <processors>", changes.get(2)); assertEquals("+ <type>ProcessorNew.class</type>", changes.get(4)); assertEquals("+ </processors>", changes.get(5)); } }
public static byte[] serialize(final TemplateDTO dto) { try { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final BufferedOutputStream bos = new BufferedOutputStream(baos); JAXBContext context = JAXBContext.newInstance(TemplateDTO.class); Marshaller marshaller = context.createMarshaller(); XMLOutputFactory xmlof = XMLOutputFactory.newInstance(); XMLStreamWriter writer = new IndentingXMLStreamWriter(xmlof.createXMLStreamWriter(bos)); marshaller.marshal(dto, writer); bos.flush(); return baos.toByteArray(); } catch (final IOException | JAXBException | XMLStreamException e) { throw new FlowSerializationException(e); } }
TemplateSerializer { public static byte[] serialize(final TemplateDTO dto) { try { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final BufferedOutputStream bos = new BufferedOutputStream(baos); JAXBContext context = JAXBContext.newInstance(TemplateDTO.class); Marshaller marshaller = context.createMarshaller(); XMLOutputFactory xmlof = XMLOutputFactory.newInstance(); XMLStreamWriter writer = new IndentingXMLStreamWriter(xmlof.createXMLStreamWriter(bos)); marshaller.marshal(dto, writer); bos.flush(); return baos.toByteArray(); } catch (final IOException | JAXBException | XMLStreamException e) { throw new FlowSerializationException(e); } } }
TemplateSerializer { public static byte[] serialize(final TemplateDTO dto) { try { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final BufferedOutputStream bos = new BufferedOutputStream(baos); JAXBContext context = JAXBContext.newInstance(TemplateDTO.class); Marshaller marshaller = context.createMarshaller(); XMLOutputFactory xmlof = XMLOutputFactory.newInstance(); XMLStreamWriter writer = new IndentingXMLStreamWriter(xmlof.createXMLStreamWriter(bos)); marshaller.marshal(dto, writer); bos.flush(); return baos.toByteArray(); } catch (final IOException | JAXBException | XMLStreamException e) { throw new FlowSerializationException(e); } }  }
TemplateSerializer { public static byte[] serialize(final TemplateDTO dto) { try { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final BufferedOutputStream bos = new BufferedOutputStream(baos); JAXBContext context = JAXBContext.newInstance(TemplateDTO.class); Marshaller marshaller = context.createMarshaller(); XMLOutputFactory xmlof = XMLOutputFactory.newInstance(); XMLStreamWriter writer = new IndentingXMLStreamWriter(xmlof.createXMLStreamWriter(bos)); marshaller.marshal(dto, writer); bos.flush(); return baos.toByteArray(); } catch (final IOException | JAXBException | XMLStreamException e) { throw new FlowSerializationException(e); } }  static byte[] serialize(final TemplateDTO dto); }
TemplateSerializer { public static byte[] serialize(final TemplateDTO dto) { try { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final BufferedOutputStream bos = new BufferedOutputStream(baos); JAXBContext context = JAXBContext.newInstance(TemplateDTO.class); Marshaller marshaller = context.createMarshaller(); XMLOutputFactory xmlof = XMLOutputFactory.newInstance(); XMLStreamWriter writer = new IndentingXMLStreamWriter(xmlof.createXMLStreamWriter(bos)); marshaller.marshal(dto, writer); bos.flush(); return baos.toByteArray(); } catch (final IOException | JAXBException | XMLStreamException e) { throw new FlowSerializationException(e); } }  static byte[] serialize(final TemplateDTO dto);  }