package com.atlassian.bitbucket.mesh.transaction;

import com.atlassian.bitbucket.mesh.rpc.v1.RpcTransaction;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;

import static org.apache.commons.lang3.StringUtils.isBlank;

public class RpcTransactionMatcher extends BaseMatcher<RpcTransaction> {

    private final RpcTransaction transaction;

    public RpcTransactionMatcher(RpcTransaction transaction) {
        this.transaction = transaction;
    }

    public static RpcTransactionMatcher tx(RpcTransaction transaction) {
        return new RpcTransactionMatcher(transaction);
    }

    @Override
    public void describeTo(Description description) {
        description.appendText("RpcTransaction matching ").appendValue(transaction);
    }

    @Override
    public boolean matches(Object item) {
        if (item instanceof RpcTransaction) {
            RpcTransaction other = (RpcTransaction) item;
            if (isBlank(transaction.getId())) {
                other = other.toBuilder().setId("").build();
            }
            return transaction.equals(other);
        }
        return false;
    }
}
