/*
 * Copyright 2017, TeamDev Ltd. 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 org.spine3.examples.todolist.c.aggregate.failures;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.spine3.base.CommandContext;
import org.spine3.examples.todolist.FailedTaskCommandDetails;
import org.spine3.examples.todolist.LabelId;
import org.spine3.examples.todolist.TaskId;
import org.spine3.examples.todolist.c.commands.AssignLabelToTask;
import org.spine3.examples.todolist.c.commands.RemoveLabelFromTask;
import org.spine3.examples.todolist.c.failures.CannotAssignLabelToTask;
import org.spine3.examples.todolist.c.failures.CannotRemoveLabelFromTask;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.spine3.examples.todolist.c.aggregate.failures.TaskLabelsPartFailures.throwCannotAssignLabelToTaskFailure;
import static org.spine3.examples.todolist.c.aggregate.failures.TaskLabelsPartFailures.throwCannotRemoveLabelFromTaskFailure;
import static org.spine3.test.Tests.assertHasPrivateParameterlessCtor;

/**
 * @author Illia Shepilov
 */
@DisplayName("TaskLabelsPartFailures should")
public class TaskLabelsPartFailuresTest {

    private final TaskId taskId = TaskId.getDefaultInstance();
    private final LabelId labelId = LabelId.getDefaultInstance();

    @Test
    @DisplayName("have the private constructor")
    public void havePrivateConstructor() {
        assertHasPrivateParameterlessCtor(TaskLabelsPartFailures.class);
    }

    @Test
    @DisplayName("throw CannotRemoveLabelFromTask failure")
    public void throwCannotRemoveLabelFromTask() {
        final RemoveLabelFromTask cmd = RemoveLabelFromTask.newBuilder()
                                                           .setId(taskId)
                                                           .setLabelId(labelId)
                                                           .build();
        final CommandContext ctx = CommandContext.getDefaultInstance();
        try {
            throwCannotRemoveLabelFromTaskFailure(cmd, ctx);
        } catch (CannotRemoveLabelFromTask ex) {
            final TaskId actual = ex.getFailureMessage()
                                    .getRemoveLabelFailed()
                                    .getFailureDetails()
                                    .getTaskId();
            assertEquals(taskId, actual);
        }
    }

    @Test
    @DisplayName("throw CannotAssignLabelToTask failure")
    public void throwCannotAssignLabelToTask() {
        final AssignLabelToTask cmd = AssignLabelToTask.newBuilder()
                                                       .setLabelId(labelId)
                                                       .setId(taskId)
                                                       .build();
        final CommandContext ctx = CommandContext.getDefaultInstance();
        try {
            throwCannotAssignLabelToTaskFailure(cmd, ctx);
        } catch (CannotAssignLabelToTask ex) {
            final FailedTaskCommandDetails failedCommand = ex.getFailureMessage()
                                                             .getAssignLabelFailed()
                                                             .getFailureDetails();
            final TaskId actualId = failedCommand.getTaskId();
            assertEquals(taskId, actualId);
        }
    }
}

@@ -1675,24 +1675,85 @@
 sertEquals;%0A
+import static org.junit.jupiter.api.Assertions.assertThrows;%0A
 import stati
@@ -1856,32 +1856,32 @@
 lToTaskFailure;%0A
-
 import static or
@@ -2142,23 +2142,16 @@
 hould%22)%0A
-public 
 class Ta
@@ -2177,16 +2177,16 @@
 sTest %7B%0A
+
 %0A    pri
@@ -2367,31 +2367,24 @@
 uctor%22)%0A    
-public 
 void havePri
@@ -2549,39 +2549,32 @@
 k failure%22)%0A    
-public 
 void throwCannot
@@ -2961,32 +2961,33 @@
 Instance();%0A
+%0A
         
 try %7B%0A      
@@ -2966,37 +2966,73 @@
 nce();%0A%0A        
-try %7B
+final CannotRemoveLabelFromTask failure =
 %0A            thr
@@ -3024,29 +3024,41 @@
             
-t
+    assertT
 hrow
+s(
 CannotRemove
@@ -3074,44 +3074,56 @@
 Task
-Failure(cmd, ctx);%0A        %7D catch (
+.class,%0A                             () -%3E throw
 Cann
@@ -3139,35 +3139,44 @@
 abelFromTask
- ex) %7B%0A    
+Failure(cmd, ctx));%0A
         fina
@@ -3193,18 +3193,23 @@
 ctual = 
+failur
 e
-x
 .getFail
@@ -3213,32 +3213,33 @@
 ailureMessage()%0A
+ 
                 
@@ -3310,32 +3310,33 @@
                 
+ 
 .getFailureDetai
@@ -3340,16 +3340,17 @@
 tails()%0A
+ 
         
@@ -3391,36 +3391,32 @@
 skId();%0A        
-    
 assertEquals(tas
@@ -3429,26 +3429,16 @@
 ctual);%0A
-        %7D%0A
     %7D%0A%0A 
@@ -3512,15 +3512,8 @@
     
-public 
 void
@@ -3898,24 +3898,25 @@
 ance();%0A
+%0A
         
 try %7B%0A  
@@ -3911,14 +3911,49 @@
     
-try %7B%0A
+final CannotAssignLabelToTask failure =%0A 
     
@@ -3960,21 +3960,32 @@
         
-t
+   assertT
 hrow
+s(
 CannotAs
@@ -4003,44 +4003,56 @@
 Task
-Failure(cmd, ctx);%0A        %7D catch (
+.class,%0A                             () -%3E throw
 Cann
@@ -4074,19 +4074,28 @@
 Task
- ex) %7B%0A    
+Failure(cmd, ctx));%0A
     
@@ -4145,18 +4145,23 @@
 mmand = 
+failur
 e
-x
 .getFail
@@ -4165,32 +4165,33 @@
 ailureMessage()%0A
+ 
                 
@@ -4312,32 +4312,33 @@
                 
+ 
 .getFailureDetai
@@ -4339,28 +4339,24 @@
 eDetails();%0A
-    
         fina
@@ -4406,28 +4406,24 @@
 ();%0A        
-    
 assertEquals
@@ -4446,18 +4446,8 @@
 d);%0A
-        %7D%0A
     

Fix excessive test visibility and rewrite `TaskLabelsPartFailuresTest` using `assertThrows`