package uk.ac.ebi.quickgo.annotation.controller;

import org.springframework.test.web.servlet.ResultMatcher;

import static java.lang.String.format;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;

/**
 * Helper class containing several methods that verifies the response from the stats endpoint.
 *
 * @author Ricardo Antunes
 */
final class StatsResponseVerifier {
    private static final String GROUP_NAME_TAG = "groupName";
    private static final String TYPE_NAME_TAG = "type";
    private static final String VALUE_NAME_TAG = "values";
    private static final String KEY_NAME_TAG = "key";
    private static final String TOTAL_GROUP_HITS_TAG = "totalHits";

    private StatsResponseVerifier() {}

    static ResultMatcher totalHitsInGroup(String groupName, int hits) {
        String hitsInGroupText = "%s[?(@.%s=='%s')].%s";

        return jsonPath(
                format(hitsInGroupText, ResponseVerifier.RESULTS, GROUP_NAME_TAG, groupName, TOTAL_GROUP_HITS_TAG)
        ).value(hits);
    }

    /**
     * Checks that all of the keys defined in the {@param keys} array are present in the {@param
     * type} within the given {@param group}.
     *
     * @param group the stats group to check in
     * @param type the stats type defined within in the {@param group}
     * @param keys the keys to check for
     * @return a {@link ResultMatcher} that checks for the given inputs
     */
    static ResultMatcher keysInTypeWithinGroup(String group, String type, String[] keys) {
        String valuesInTypeForGroup = "%s[?(@.%s=='%s')]..[?(@.%s=='%s')]..%s";
        return jsonPath(
                format(valuesInTypeForGroup,
                        ResponseVerifier.RESULTS,
                        GROUP_NAME_TAG, group,
                        TYPE_NAME_TAG, type,
                        KEY_NAME_TAG),
                containsInAnyOrder(keys)
        );
    }
}
@@ -580,67 +580,8 @@
 e%22;%0A
-    private static final String VALUE_NAME_TAG = %22values%22;%0A
     
@@ -630,16 +630,16 @@
  %22key%22;%0A
+
     priv
@@ -1039,16 +1039,890 @@
 %0A    %7D%0A%0A
+    /**%0A     * Checks that the value specified in the %7B@param value%7D is present in the %7B@param%0A     * type%7D within the given %7B@param group%7D.%0A     *%0A     * @param group the stats group to check in%0A     * @param type the stats type defined within in the %7B@param group%7D%0A     * @param value the value to check for%0A     * @return a %7B@link ResultMatcher%7D that checks for the given inputs%0A     */%0A    static ResultMatcher numericValueForGroup(String group, String type, String field, int value) %7B%0A        String valuesInTypeForGroup = %22%25s%5B?(@.%25s=='%25s')%5D..%5B?(@.%25s=='%25s')%5D..%25s%22;%0A        return jsonPath(%0A                format(valuesInTypeForGroup,%0A                        ResponseVerifier.RESULTS,%0A                        GROUP_NAME_TAG, group,%0A                        TYPE_NAME_TAG, type,%0A                        field),%0A                containsInAnyOrder(value)%0A        );%0A    %7D%0A%0A
     /**%0A
@@ -2791,9 +2791,10 @@
 ;%0A    %7D%0A
-
 %7D
+%0A

Add method numericValueForGroup() to check for values at group level.