// Copyright 2018 The Bazel Authors. All rights reserved.
//
// 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 build.buildfarm.common;

import static com.google.common.truth.Truth.assertThat;

import build.buildfarm.common.DigestUtil;
import com.google.devtools.remoteexecution.v1test.Digest;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class DigestUtilTest {
    @Rule
    public ExpectedException thrown = ExpectedException.none();

    @Test
    public void invalidHashCodeIsDetectedDuringConstruction() {
        DigestUtil util = new DigestUtil(DigestUtil.HashFunction.MD5);
        thrown.expect(NumberFormatException.class);
        thrown.expectMessage("[foo] is not a valid MD5 hash.");
        util.build("foo", 3);
    }

    @Test
    public void canBuildAValidDigest() {
        DigestUtil util = new DigestUtil(DigestUtil.HashFunction.MD5);
        String bazelMd5Hash = "24ef4c36ec66c15ef9f0c96fe27c0e0b";
        long payloadSizeInBytes = 5;
        Digest digest = util.build(bazelMd5Hash, payloadSizeInBytes);
        assertThat(digest.getHash()).isEqualTo(bazelMd5Hash);
        assertThat(digest.getSizeBytes()).isEqualTo(payloadSizeInBytes);
    }
}

@@ -703,50 +703,8 @@
 t;%0A%0A
-import build.buildfarm.common.DigestUtil;%0A
 impo
@@ -969,21 +969,15 @@
 t %7B%0A
-  
   @Rule
-%0A   
  pub
@@ -1034,26 +1034,24 @@
 one();%0A%0A
-  
   @Test%0A
     publ
@@ -1034,34 +1034,32 @@
 one();%0A%0A  @Test%0A
-  
   public void in
@@ -1100,28 +1100,24 @@
 ruction() %7B%0A
-    
     DigestUt
@@ -1171,28 +1171,24 @@
 n.MD5);%0A    
- 
-   
 thrown.expec
@@ -1215,28 +1215,24 @@
 ion.class);%0A
-    
     thrown.e
@@ -1279,20 +1279,16 @@
 ash.%22);%0A
-    
     util
@@ -1309,27 +1309,21 @@
 3);%0A
-  
   %7D%0A%0A  
- 
- 
 @Test%0A
-  
   pu
@@ -1357,20 +1357,16 @@
 est() %7B%0A
-    
     Dige
@@ -1428,20 +1428,16 @@
 5);%0A    
- 
-   
 String b
@@ -1486,20 +1486,16 @@
 c0e0b%22;%0A
-    
     long
@@ -1519,20 +1519,16 @@
 es = 5;%0A
-    
     Dige
@@ -1585,28 +1585,24 @@
 Bytes);%0A    
-   
- 
 assertThat(d
@@ -1643,20 +1643,16 @@
 5Hash);%0A
-    
     asse
@@ -1712,16 +1712,14 @@
 Bytes);%0A
-  
   %7D%0A%7D%0A

Reformat DigestUtilTest using google-java-format. (#106)