Back to Repositories

Testing Protocol Buffer Generation Implementation in Conductor OSS

This test suite validates the ProtoGen utility for generating Protocol Buffer definitions and mapper files from Java source code. It ensures proper file generation, package management, and content validation in the Conductor OSS framework.

Test Coverage Overview

The test suite covers the core functionality of the ProtoGen class, focusing on protocol buffer file generation and package management.

  • Validates successful proto file generation
  • Tests package naming and directory structure creation
  • Verifies file content against expected output
  • Checks proper mapper file generation

Implementation Analysis

The implementation uses JUnit’s TemporaryFolder rule for isolated file system testing. The test follows a builder pattern to configure and execute the proto generation process, with explicit verification of file existence and content matching.

The approach uses Google Common IO utilities for file operations and implements comprehensive assertions for validation.

Technical Details

  • JUnit 4 testing framework
  • TemporaryFolder Rule for file system isolation
  • Google Common IO for file operations
  • StandardCharsets for character encoding
  • Resource comparison for validation

Best Practices Demonstrated

The test demonstrates several testing best practices for file generation validation.

  • Isolated test environment using TemporaryFolder
  • Explicit setup and cleanup of test resources
  • Content validation using resource comparison
  • Clear separation of configuration and execution
  • Proper error handling and resource management

conductor-oss/conductor

annotations-processor/src/test/java/com/netflix/conductor/annotationsprocessor/protogen/ProtoGenTest.java

            
/*
 * Copyright 2022 Conductor Authors.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */
package com.netflix.conductor.annotationsprocessor.protogen;

import java.io.File;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import com.google.common.collect.Lists;
import com.google.common.io.Files;
import com.google.common.io.Resources;

import static org.junit.Assert.*;

public class ProtoGenTest {
    private static final Charset charset = StandardCharsets.UTF_8;

    @Rule public TemporaryFolder folder = new TemporaryFolder();

    @Test
    public void happyPath() throws Exception {
        File rootDir = folder.getRoot();
        String protoPackage = "protoPackage";
        String javaPackage = "abc.protogen.example";
        String goPackage = "goPackage";
        String sourcePackage = "com.example";
        String mapperPackage = "mapperPackage";

        File jarFile = new File("./build/libs/example.jar");
        assertTrue(jarFile.exists());

        File mapperDir = new File(rootDir, "mapperDir");
        mapperDir.mkdirs();

        File protosDir = new File(rootDir, "protosDir");
        protosDir.mkdirs();

        File modelDir = new File(protosDir, "model");
        modelDir.mkdirs();

        ProtoGen generator = new ProtoGen(protoPackage, javaPackage, goPackage);
        generator.processPackage(jarFile, sourcePackage);
        generator.writeMapper(mapperDir, mapperPackage);
        generator.writeProtos(protosDir);

        List<File> models = Lists.newArrayList(modelDir.listFiles());
        assertEquals(1, models.size());
        File exampleProtoFile =
                models.stream().filter(f -> f.getName().equals("example.proto")).findFirst().get();
        assertTrue(exampleProtoFile.length() > 0);
        assertEquals(
                Resources.asCharSource(Resources.getResource("example.proto.txt"), charset).read(),
                Files.asCharSource(exampleProtoFile, charset).read());
    }
}