/*
 * Copyright 2016, 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.server.stand;

import com.google.protobuf.Any;
import org.junit.Test;
import org.spine3.base.Queries;
import org.spine3.client.Subscription;
import org.spine3.client.Target;
import org.spine3.protobuf.AnyPacker;
import org.spine3.protobuf.TypeUrl;
import org.spine3.test.aggregate.Project;
import org.spine3.test.aggregate.ProjectId;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

/**
 * @author Dmytro Dashenkov
 */
public class SubscriptionRecordShould {

    @Test
    public void match_record_to_given_parameters() {
        final SubscriptionRecord matchingRecord = new SubscriptionRecord(Given.subscription(),
                                                                         Given.target(),
                                                                         Given.TYPE);

        final Project entityState = Project.getDefaultInstance();
        final Any wrappedState = AnyPacker.pack(entityState);
        final ProjectId redundantId = ProjectId.getDefaultInstance();

        final boolean matchResult = matchingRecord.matches(Given.TYPE, redundantId, wrappedState);

        assertTrue(matchResult);
    }

    @Test
    public void be_equal_if_has_same_subscription() {
        final Subscription oneSubscription = Given.subscription();
        final Subscription otherSubscription = Subscription.newBuilder()
                                                           .setId("breaking-id")
                                                           .build();

        @SuppressWarnings("QuestionableName")
        final SubscriptionRecord one = new SubscriptionRecord(oneSubscription,
                                                              Given.target(),
                                                              Given.TYPE);

        final SubscriptionRecord similar = new SubscriptionRecord(otherSubscription,
                                                                  Given.target(),
                                                                  Given.TYPE);

        final SubscriptionRecord same = new SubscriptionRecord(oneSubscription,
                                                               Given.target(),
                                                               Given.TYPE);

        assertFalse(one.equals(similar));
        assertTrue(one.equals(same));
    }

    @SuppressWarnings("UtilityClass")
    private static class Given {

        private static final TypeUrl TYPE = TypeUrl.of(Project.class);

        private static Target target() {
            final Target target = Queries.Targets.allOf(Project.class);

            return target;
        }

        private static Subscription subscription() {
            final Subscription subscription = Subscription.getDefaultInstance();

            return subscription;
        }
    }
}

@@ -1076,16 +1076,52 @@
 uf.Any;%0A
+import com.google.protobuf.Message;%0A
 import o
@@ -1398,16 +1398,104 @@
 ojectId;
+%0Aimport org.spine3.test.commandservice.customer.Customer;%0A%0Aimport java.util.Collections;
 %0A%0Aimport
@@ -2322,24 +2322,1603 @@
 lt);%0A    %7D%0A%0A
+    @Test%0A    public void fail_to_match_improper_type() %7B%0A        final SubscriptionRecord notMatchingRecord = new SubscriptionRecord(Given.subscription(),%0A                                                                            Given.target(),%0A                                                                            Given.TYPE);%0A%0A        final Project entityState = Project.getDefaultInstance();%0A        final Any wrappedState = AnyPacker.pack(entityState);%0A        final ProjectId redundantId = ProjectId.getDefaultInstance();%0A%0A        final boolean matchResult = notMatchingRecord.matches(Given.OTHER_TYPE, redundantId, wrappedState);%0A%0A        assertFalse(matchResult);%0A    %7D%0A%0A    @Test%0A    public void fail_to_match_improper_target() %7B%0A        final ProjectId nonExisitngId = ProjectId.newBuilder()%0A                                                 .setId(%22never-existed%22)%0A                                                 .build();%0A%0A        final SubscriptionRecord notMatchingRecord = new SubscriptionRecord(Given.subscription(),%0A                                                                            Given.target(nonExisitngId),%0A                                                                            Given.TYPE);%0A%0A        final Project entityState = Project.getDefaultInstance();%0A        final Any wrappedState = AnyPacker.pack(entityState);%0A        final ProjectId redundantId = ProjectId.getDefaultInstance();%0A%0A        final boolean matchResult = notMatchingRecord.matches(Given.TYPE, redundantId, wrappedState);%0A%0A        assertFalse(matchResult);%0A    %7D%0A%0A
     @Test%0A  
@@ -5243,32 +5243,110 @@
 (Project.class);
+%0A        private static final TypeUrl OTHER_TYPE = TypeUrl.of(Customer.class);
 %0A%0A        privat
@@ -5479,24 +5479,226 @@
 %0A        %7D%0A%0A
+        private static Target target(Message targetId) %7B%0A            final Target target = Queries.Targets.someOf(Project.class, Collections.singleton(targetId));%0A%0A            return target;%0A        %7D%0A%0A
         priv

Add dismatch tests for subscription record.