SchemaGenerator { protected String buildMessage(Message message) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append("\n"); stringBuilder.append("message"); stringBuilder.append(" "); stringBuilder.append(message.getName()); stringBuilder.append(" {"); stringBuilder.append(NEW_LINE); message.getFields().stream() .sorted(Comparator.comparingInt(Field::getIndex)) .map(this::buildField) .forEachOrdered(field -> { stringBuilder.append(field); stringBuilder.append(NEW_LINE); }); stringBuilder.append("}"); return stringBuilder.toString(); }  String generate(Schema schema);  }
@Test public void testBuildMessageWithSingleField() { Message message = new Message("KObject", Collections.singleton(new Field(ProtobufScope.REQUIRED, "int32", "aField", 1))); String generatedMessage = this.schemaGenerator.buildMessage(message); logger.debug(generatedMessage); assertThat(this.sanitize(generatedMessage)) .isEqualToIgnoringWhitespace(this.read("proto/single-field-message.proto")); }
@Test public void testBuildMessageWithMultipleFields() { Message message = new Message("KObject", new HashSet<>(Arrays.asList(new Field(ProtobufScope.REQUIRED, "int32", "aField", 1), new Field(ProtobufScope.REQUIRED, "string", "anotherField", 2)))); String generatedMessage = this.schemaGenerator.buildMessage(message); assertThat(this.sanitize(generatedMessage)) .isEqualToIgnoringWhitespace(this.read("proto/multi-field-message.proto")); }