/*
 * Copyright The OpenTelemetry Authors
 * SPDX-License-Identifier: Apache-2.0
 */

package io.opentelemetry.javaagent.instrumentation.hibernate.v4_0;

import static io.opentelemetry.api.common.AttributeKey.stringKey;
import static io.opentelemetry.api.trace.SpanKind.CLIENT;
import static io.opentelemetry.api.trace.SpanKind.INTERNAL;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.satisfies;

import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.semconv.incubating.DbIncubatingAttributes;
import java.util.function.Consumer;
import java.util.stream.Stream;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

class CriteriaTest extends AbstractHibernateTest {

  @ParameterizedTest
  @MethodSource("provideArguments")
  @SuppressWarnings("deprecation") // createCriteria(Class) has been deprecated in v5
  void testCriteria(String methodName, Consumer<Criteria> interaction) {

    testing.runWithSpan(
        "parent",
        () -> {
          Session session = sessionFactory.openSession();
          session.beginTransaction();
          Criteria criteria =
              session
                  .createCriteria(Value.class)
                  .add(Restrictions.like("name", "Hello"))
                  .addOrder(Order.desc("name"));
          interaction.accept(criteria);
          session.getTransaction().commit();
          session.close();
        });

    testing.waitAndAssertTraces(
        trace ->
            trace.hasSpansSatisfyingExactly(
                span ->
                    span.hasName("parent")
                        .hasKind(INTERNAL)
                        .hasNoParent()
                        .hasAttributes(Attributes.empty()),
                span ->
                    span.hasName("Criteria." + methodName + " " + Value.class.getName())
                        .hasKind(INTERNAL)
                        .hasParent(trace.getSpan(0))
                        .hasAttributesSatisfyingExactly(
                            satisfies(
                                stringKey("hibernate.session_id"),
                                val -> val.isInstanceOf(String.class))),
                span ->
                    span.hasName("SELECT db1.Value")
                        .hasKind(CLIENT)
                        .hasParent(trace.getSpan(1))
                        .hasAttributesSatisfyingExactly(
                            equalTo(DbIncubatingAttributes.DB_SYSTEM, "h2"),
                            equalTo(DbIncubatingAttributes.DB_NAME, "db1"),
                            equalTo(DbIncubatingAttributes.DB_USER, "sa"),
                            equalTo(DbIncubatingAttributes.DB_CONNECTION_STRING, "h2:mem:"),
                            satisfies(
                                DbIncubatingAttributes.DB_STATEMENT,
                                stringAssert -> stringAssert.startsWith("select")),
                            equalTo(DbIncubatingAttributes.DB_OPERATION, "SELECT"),
                            equalTo(DbIncubatingAttributes.DB_SQL_TABLE, "Value")),
                span ->
                    span.hasName("Transaction.commit")
                        .hasKind(INTERNAL)
                        .hasParent(trace.getSpan(0))
                        .hasAttributesSatisfyingExactly(
                            equalTo(
                                stringKey("hibernate.session_id"),
                                trace
                                    .getSpan(1)
                                    .getAttributes()
                                    .get(stringKey("hibernate.session_id"))))));
  }

  private static Stream<Arguments> provideArguments() {
    return Stream.of(
        Arguments.of("list", (Consumer<Criteria>) Criteria::list),
        Arguments.of("uniqueResult", (Consumer<Criteria>) Criteria::uniqueResult),
        Arguments.of("scroll", (Consumer<Criteria>) Criteria::scroll));
  }
}
