/*
 * 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.testing.server.entity;

import com.google.common.truth.BooleanSubject;
import com.google.common.truth.FailureMetadata;
import com.google.common.truth.Subject;
import com.google.common.truth.extensions.proto.ProtoSubject;
import com.google.common.truth.extensions.proto.ProtoTruth;
import com.google.protobuf.Message;
import io.spine.server.entity.EntityWithLifecycle;
import org.checkerframework.checker.nullness.compatqual.NullableDecl;

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

/**
 * Assertions for entities.
 *
 * @param <T> the type of entity subject for covariance
 * @param <S> the type of the entity state message
 * @param <E> the type of the entity
 */
public class EntitySubject<T extends EntitySubject<T, S, E>,
                           S extends Message,
                           E extends EntityWithLifecycle<?, S>>
        extends Subject<T, E> {

    protected EntitySubject(FailureMetadata metadata, @NullableDecl E actual) {
        super(metadata, actual);
    }

    public void exists() {
        isNotNull();
    }

    public BooleanSubject archivedFlag() {
        exists();
        return assertThat(actual().getLifecycleFlags()
                                  .getArchived());

    }
    public BooleanSubject deletedFlag() {
        exists();
        return assertThat(actual().getLifecycleFlags()
                                  .getDeleted());
    }

    /**
     * Obtains subject for the state of the process manager.
     */
    public ProtoSubject<?, Message> hasStateThat() {
        exists();
        return ProtoTruth.assertThat(actual().getState());
    }
}

@@ -1391,16 +1391,62 @@
 ecycle;%0A
+import io.spine.server.entity.LifecycleFlags;%0A
 import o
@@ -2064,32 +2064,86 @@
 actual);%0A    %7D%0A%0A
+    /**%0A     * Verifies if the entity exists.%0A     */%0A
     public void 
@@ -2177,24 +2177,98 @@
 l();%0A    %7D%0A%0A
+    /**%0A     * Obtains the subject for the %7B@code archived%7D flag.%0A     */%0A
     public B
@@ -2346,94 +2346,112 @@
 hat(
-actual().getLifecycleFlags()%0A            
+flags().getArchived());%0A%0A
     
+%7D%0A%0A
     
+/**%0A
      
-         .getArchived());%0A%0A
+* Obtains the subject for the %7B@code deleted%7D flag.%0A 
     
-%7D
+*/
 %0A   
@@ -2537,84 +2537,117 @@
 hat(
-actual().getLifecycleFlags()%0A                                  .getDeleted()
+flags().getDeleted());%0A    %7D%0A%0A    private LifecycleFlags flags() %7B%0A        return actual().getLifecycleFlags(
 );%0A 
@@ -2675,16 +2675,20 @@
 Obtains 
+the 
 subject 
@@ -2712,23 +2712,14 @@
 the 
-process manager
+entity
 .%0A  

Add Javadocs