/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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 org.apache.beam.runners.spark.structuredstreaming.translation.batch;

import java.io.Serializable;
import org.apache.beam.runners.spark.structuredstreaming.SparkPipelineOptions;
import org.apache.beam.runners.spark.structuredstreaming.SparkRunner;
import org.apache.beam.sdk.Pipeline;
import org.apache.beam.sdk.options.PipelineOptions;
import org.apache.beam.sdk.options.PipelineOptionsFactory;
import org.apache.beam.sdk.transforms.Combine;
import org.apache.beam.sdk.transforms.Create;
import org.apache.beam.sdk.values.PCollection;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Test class for beam to spark {@link org.apache.beam.sdk.transforms.Combine} translation. */
@RunWith(JUnit4.class)
public class CombineTest implements Serializable {
  private static Pipeline pipeline;

  @BeforeClass
  public static void beforeClass() {
    PipelineOptions options = PipelineOptionsFactory.create().as(SparkPipelineOptions.class);
    options.setRunner(SparkRunner.class);
    pipeline = Pipeline.create(options);
  }

  @Test
  public void testCombine() {
    PCollection<Integer> input = pipeline.apply(Create.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
    input.apply(
        Combine.globally(
            new Combine.CombineFn<Integer, Integer, Integer>() {

              @Override
              public Integer createAccumulator() {
                return 0;
              }

              @Override
              public Integer addInput(Integer accumulator, Integer input) {
                return accumulator + input;
              }

              @Override
              public Integer mergeAccumulators(Iterable<Integer> accumulators) {
                Integer result = 0;
                for (Integer value : accumulators) {
                  result += value;
                }
                return result;
              }

              @Override
              public Integer extractOutput(Integer accumulator) {
                return accumulator;
              }
            }));
    pipeline.run();
  }
}

@@ -906,16 +906,67 @@
 izable;%0A
+import java.util.ArrayList;%0Aimport java.util.List;%0A
 import o
@@ -1347,16 +1347,54 @@
 Create;%0A
+import org.apache.beam.sdk.values.KV;%0A
 import o
@@ -2026,16 +2026,24 @@
 tCombine
+Globally
 () %7B%0A   
@@ -2176,203 +2176,665 @@
 lly(
-%0A            new Combine.CombineFn%3CInteger, Integer, Integer%3E() %7B%0A%0A              @Override%0A              public Integer createAccumulator() %7B%0A                return 0;%0A              %7D%0A%0A      
+new IntegerCombineFn()));%0A    pipeline.run();%0A  %7D%0A%0A  @Test%0A  public void testCombinePerKey()%7B%0A    List%3CKV%3CInteger, Integer%3E%3E elems = new ArrayList%3C%3E();%0A    elems.add(KV.of(1, 1));%0A    elems.add(KV.of(1, 3));%0A    elems.add(KV.of(1, 5));%0A    elems.add(KV.of(2, 2));%0A    elems.add(KV.of(2, 4));%0A    elems.add(KV.of(2, 6));%0A%0A    PCollection%3CKV%3CInteger, Integer%3E%3E input = pipeline.apply(Create.of(elems));%0A    input.apply(Combine.perKey(new IntegerCombineFn()));%0A    pipeline.run();%0A  %7D%0A%0A  private static class IntegerCombineFn extends Combine.CombineFn%3CInteger, Integer, Integer%3E %7B%0A%0A    @Override%0A    public Integer createAccumulator() %7B%0A      return 0;%0A
     
+%7D%0A%0A
     
@@ -2839,34 +2839,24 @@
   @Override%0A
-          
     public I
@@ -2905,34 +2905,24 @@
 er input) %7B%0A
-          
       return
@@ -2947,39 +2947,19 @@
 ut;%0A    
-          %7D%0A%0A          
+%7D%0A%0A
     @Ove
@@ -2960,34 +2960,24 @@
   @Override%0A
-          
     public I
@@ -3031,34 +3031,24 @@
 mulators) %7B%0A
-          
       Intege
@@ -3061,26 +3061,16 @@
 lt = 0;%0A
-          
       fo
@@ -3100,34 +3100,24 @@
 mulators) %7B%0A
-          
         resu
@@ -3139,102 +3139,52 @@
     
-          %7D%0A                return result;%0A              %7D%0A%0A              @Override%0A          
+%7D%0A      return result;%0A    %7D%0A%0A    @Override%0A
     
@@ -3235,26 +3235,16 @@
 ator) %7B%0A
-          
       re
@@ -3269,56 +3269,9 @@
     
-          %7D%0A            %7D));%0A    pipeline.run();
+%7D
 %0A  %7D

Add combinePerKey and CombineGlobally tests