package com.atlassian.bitbucket.test.rest.comment;

import com.jayway.restassured.builder.ResponseSpecBuilder;
import com.jayway.restassured.specification.ResponseSpecification;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

import static org.hamcrest.Matchers.equalTo;

public class RestCommentLikeUtils {

    public static ResponseSpecification expectLikedBy(String basePath, List<String> userSlugs) {
        return expectLikedBy(basePath, userSlugs, userSlugs.size());
    }

    public static ResponseSpecification expectLikedBy(String basePath, List<String> userSlugs, int totalCount) {
        // append a . if its not zero length and doesn't already end in a .
        if (!StringUtils.isEmpty(basePath) && !StringUtils.endsWith(basePath, ".")) {
            basePath = basePath + '.';
        }

        ResponseSpecBuilder builder = new ResponseSpecBuilder();
        builder.expectBody(basePath + "properties.likedBy.total", equalTo(totalCount));

        for (int i = 0; i < userSlugs.size(); i++) {
            builder.expectBody(basePath + "properties.likedBy.likers[" + i + "].slug", equalTo(userSlugs.get(i)));
        }
        return builder.build();
    }
}
