/*
 * Copyright 2019, TeamDev. All rights reserved.
 *
 * Redistribution and use in source and/or binary forms, with or without
 * modification, must retain the above copyright notice and the following
 * disclaimer.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package io.spine.test.tools.validate;

import io.spine.protobuf.MessageWithConstraints;
import io.spine.validate.ConstraintViolation;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.List;

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

@DisplayName("Number boundaries constraints should be compiled so that")
class NumberConstraintTest {

    @Test
    @DisplayName("min value is checked")
    void min() {
        assertViolation(
                InterestRate
                        .newBuilder()
                        .setPercent(-3)
                        .buildPartial(),
                "greater than 0.0"
        );
        assertValid(
                InterestRate
                        .newBuilder()
                        .setPercent(117.3F)
                        .build()
        );
    }

    @Test
    @DisplayName("min and max values are checked")
    void minMax() {
        assertViolation(
                Year
                        .newBuilder()
                        .setDayCount(42)
                        .buildPartial(),
                "greater than or equal to 365"
        );
        assertViolation(
                Year
                        .newBuilder()
                        .setDayCount(420)
                        .buildPartial(),
                "less than or equal to 366"
        );
        assertValid(
                Year
                        .newBuilder()
                        .setDayCount(365)
                        .buildPartial()
        );
        assertValid(
                Year
                        .newBuilder()
                        .setDayCount(366)
                        .buildPartial()
        );
    }

    @Test
    @DisplayName("numerical range is checked")
    void range() {
        assertViolation(
                Probability
                        .newBuilder()
                        .setValue(1.1)
                        .buildPartial(),
                "less than or equal to 1.0"
        );
        assertViolation(
                Probability
                        .newBuilder()
                        .setValue(-0.1)
                        .buildPartial(),
                "greater than or equal to 0.0"
        );
        assertValid(
                Probability
                        .newBuilder()
                        .setValue(0.0)
                        .buildPartial()
        );
        assertValid(
                Probability
                        .newBuilder()
                        .setValue(1.0)
                        .buildPartial()
        );
    }

    private static void assertViolation(MessageWithConstraints message, String error) {
        List<ConstraintViolation> violations = message.validate();
        assertThat(violations)
                .hasSize(1);
        assertThat(violations.get(0).getMsgFormat())
                .contains(error);
    }

    private static void assertValid(MessageWithConstraints message) {
        List<ConstraintViolation> violations = message.validate();
        assertThat(violations)
                .isEmpty();
    }
}

@@ -1527,33 +1527,8 @@
 Rate
-%0A                        
 .new
@@ -1529,32 +1529,36 @@
 te.newBuilder()%0A
+    
                 
@@ -1581,16 +1581,20 @@
 ent(-3)%0A
+    
         
@@ -1725,33 +1725,8 @@
 Rate
-%0A                        
 .new
@@ -1727,32 +1727,36 @@
 te.newBuilder()%0A
+    
                 
@@ -1783,16 +1783,20 @@
 117.3F)%0A
+    
         
@@ -1968,33 +1968,8 @@
 Year
-%0A                        
 .new
@@ -1970,36 +1970,32 @@
 ar.newBuilder()%0A
-    
                 
@@ -2015,20 +2015,16 @@
 unt(42)%0A
-    
         
@@ -2159,33 +2159,8 @@
 Year
-%0A                        
 .new
@@ -2161,36 +2161,32 @@
 ar.newBuilder()%0A
-    
                 
@@ -2207,20 +2207,16 @@
 nt(420)%0A
-    
         
@@ -2344,33 +2344,8 @@
 Year
-%0A                        
 .new
@@ -2346,36 +2346,32 @@
 ar.newBuilder()%0A
-    
                 
@@ -2404,36 +2404,32 @@
                 
-    
 .buildPartial()%0A
@@ -2484,33 +2484,8 @@
 Year
-%0A                        
 .new
@@ -2486,36 +2486,32 @@
 ar.newBuilder()%0A
-    
                 
@@ -2532,20 +2532,16 @@
 nt(366)%0A
-    
         
@@ -2718,33 +2718,8 @@
 lity
-%0A                        
 .new
@@ -2720,32 +2720,35 @@
 ty.newBuilder()%0A
+   
                 
@@ -2766,24 +2766,27 @@
 tValue(1.1)%0A
+   
             
@@ -2925,33 +2925,8 @@
 lity
-%0A                        
 .new
@@ -2927,32 +2927,35 @@
 ty.newBuilder()%0A
+   
                 
@@ -2974,24 +2974,27 @@
 Value(-0.1)%0A
+   
             
@@ -3132,33 +3132,8 @@
 lity
-%0A                        
 .new
@@ -3134,32 +3134,35 @@
 ty.newBuilder()%0A
+   
                 
@@ -3180,24 +3180,27 @@
 tValue(0.0)%0A
+   
             
@@ -3290,33 +3290,8 @@
 lity
-%0A                        
 .new
@@ -3292,32 +3292,35 @@
 ty.newBuilder()%0A
+   
                 
@@ -3338,24 +3338,27 @@
 tValue(1.0)%0A
+   
             
@@ -3654,16 +3654,46 @@
 s.get(0)
+%0A                             
 .getMsgF

Fix code alignment