/*
 * Copyright 2016 Google LLC
 *
 * 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
 *
 *       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 com.google.cloud.bigquery;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

import com.google.cloud.bigquery.TimePartitioning.Type;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

public class TimePartitioningTest {

  private static final Type TYPE = Type.DAY;
  private static final long EXPIRATION_MS = 42;
  private static final boolean REQUIRE_PARTITION_FILTER = false;
  private static final String FIELD = "field";
  private static final TimePartitioning TIME_PARTITIONING =
      TimePartitioning.newBuilder(TYPE)
          .setExpirationMs(EXPIRATION_MS)
          .setRequirePartitionFilter(REQUIRE_PARTITION_FILTER)
          .setField(FIELD)
          .build();

  @Rule public ExpectedException thrown = ExpectedException.none();

  @Test
  public void testOf() {
    assertEquals(TYPE, TIME_PARTITIONING.getType());
    assertEquals(EXPIRATION_MS, TIME_PARTITIONING.getExpirationMs().longValue());
    assertEquals(REQUIRE_PARTITION_FILTER, TIME_PARTITIONING.getRequirePartitionFilter());
    assertEquals(FIELD, TIME_PARTITIONING.getField());
    TimePartitioning partitioning = TimePartitioning.of(TYPE);
    assertEquals(TYPE, partitioning.getType());
    assertNull(partitioning.getExpirationMs());
  }

  @Test
  public void testBuilder() {
    TimePartitioning partitioning = TimePartitioning.newBuilder(TYPE).build();
    assertEquals(TYPE, partitioning.getType());
    assertNull(partitioning.getExpirationMs());
    assertNull(partitioning.getRequirePartitionFilter());
    assertNull(partitioning.getField());
    partitioning = TimePartitioning.newBuilder(TYPE).setExpirationMs(100L).build();
    assertEquals(TYPE, partitioning.getType());
    assertEquals(100, (long) partitioning.getExpirationMs());
    assertNull(partitioning.getRequirePartitionFilter());
    assertNull(partitioning.getField());
  }

  @Test
  public void testTypeOf_Npe() {
    thrown.expect(NullPointerException.class);
    TimePartitioning.of(null);
  }

  @Test
  public void testTypeAndExpirationOf_Npe() {
    thrown.expect(NullPointerException.class);
    TimePartitioning.of(null, EXPIRATION_MS);
  }

  @Test
  public void testToAndFromPb() {
    compareTimePartitioning(TIME_PARTITIONING, TimePartitioning.fromPb(TIME_PARTITIONING.toPb()));
    TimePartitioning partitioning = TimePartitioning.of(TYPE);
    compareTimePartitioning(partitioning, TimePartitioning.fromPb(partitioning.toPb()));
  }

  private void compareTimePartitioning(TimePartitioning expected, TimePartitioning value) {
    assertEquals(expected, value);
    assertEquals(expected.getType(), value.getType());
    assertEquals(expected.getExpirationMs(), value.getExpirationMs());
    assertEquals(expected.getRequirePartitionFilter(), value.getRequirePartitionFilter());
    assertEquals(expected.getField(), value.getField());
    assertEquals(expected.hashCode(), value.hashCode());
    assertEquals(expected.toString(), value.toString());
  }
}

@@ -671,16 +671,62 @@
 Equals;%0A
+import static org.junit.Assert.assertNotNull;%0A
 import s
@@ -838,77 +838,37 @@
 nit.
-Rule;%0Aimport org.junit.Test;%0Aimport org.junit.rules.ExpectedException
+Assert;%0Aimport org.junit.Test
 ;%0A%0Ap
@@ -1365,77 +1365,8 @@
 );%0A%0A
-  @Rule public ExpectedException thrown = ExpectedException.none();%0A%0A
   @T
@@ -2499,80 +2499,150 @@
    t
-hrown.expect(NullPointerException.class);%0A    TimePartitioning.of(null);
+ry %7B%0A      TimePartitioning.of(null);%0A      Assert.fail();%0A    %7D catch (NullPointerException ex) %7B%0A      assertNotNull(ex.getMessage());%0A    %7D
 %0A  %7D
@@ -2706,50 +2706,15 @@
    t
-hrown.expect(NullPointerException.class);%0A
+ry %7B%0A  
     
@@ -2755,16 +2755,121 @@
 ON_MS);%0A
+      Assert.fail();%0A    %7D catch (NullPointerException ex) %7B%0A      assertNotNull(ex.getMessage());%0A    %7D%0A
   %7D%0A%0A  @

remove deprecated method (#78)