<commit_before>defmodule OpenAperture.Manager.Controllers.SystemComponentTest do
  use ExUnit.Case, async: false
  use Phoenix.ConnTest

  alias OpenAperture.Manager.DB.Models.MessagingExchange
  alias OpenAperture.Manager.DB.Models.SystemComponent
  alias OpenAperture.Manager.Repo
  alias OpenAperture.Manager.Plugs.Authentication

  alias OpenAperture.OverseerApi.Publisher, as: OverseerPublisher

  require Repo
  import Ecto.Query  

  setup_all do
    exchange = Repo.insert(MessagingExchange.new(%{name: "test exchange"}))

    :meck.new(Authentication, [:passthrough])
    :meck.expect(Authentication, :authenticate_user, fn conn, _opts -> conn end)

    on_exit fn ->
      :meck.unload

      Repo.delete_all(SystemComponent)
      Repo.delete_all(MessagingExchange)
    end

    {:ok, [exchange: exchange]}
  end

  setup do
    Repo.delete_all(SystemComponent)
    :ok
  end

  @endpoint OpenAperture.Manager.Endpoint

  # ==================================
  # index tests

  test "index - no components" do
    conn = get conn(), "/system_components"
    assert conn.status == 200

    body = Poison.decode!(conn.resp_body)

    assert length(body) == 0
  end

  test "index - components", context do
    Repo.insert(SystemComponent.new(%{messaging_exchange_id: context[:exchange].id, type: "test", source_repo: "https://github.com/test/test.git", source_repo_git_ref: "123abc", upgrade_strategy: "manual", deployment_repo: "https://github.com/test/test.git", deployment_repo_git_ref: "123abc"}))
    Repo.insert(SystemComponent.new(%{messaging_exchange_id: context[:exchange].id, type: "test", source_repo: "https://github.com/test/test.git", source_repo_git_ref: "123abc", upgrade_strategy: "manual", deployment_repo: "https://github.com/test/test.git", deployment_repo_git_ref: "123abc"}))

    conn = get conn(), "/system_components"
    assert conn.status == 200

    body = Poison.decode!(conn.resp_body)

    assert length(body) == 2
  end

  # ==================================
  # show tests

  test "show - invalid component" do
    conn = get conn(), "/system_components/1234567890"
    assert conn.status == 404
  end

  test "show - valid component", context do
    component = Repo.insert(SystemComponent.new(%{messaging_exchange_id: context[:exchange].id, type: "test", source_repo: "https://github.com/test/test.git", source_repo_git_ref: "123abc", upgrade_strategy: "manual", deployment_repo: "https://github.com/test/test.git", deployment_repo_git_ref: "123abc", status: to_string(:available), upgrade_status: Poison.encode!(%{workflow_ids: []})}))

    conn = get conn(), "/system_components/#{component.id}"
    assert conn.status == 200

    returned_component = Poison.decode!(conn.resp_body)
    assert returned_component != nil
    assert returned_component["type"] == component.type
    assert returned_component["source_repo"] == component.source_repo
    assert returned_component["source_repo_git_ref"] == component.source_repo_git_ref
    assert returned_component["deployment_repo"] == component.deployment_repo
    assert returned_component["deployment_repo_git_ref"] == component.deployment_repo_git_ref    
    assert returned_component["upgrade_strategy"] == component.upgrade_strategy
  end  

  # ==================================
  # create tests

  test "create - conflict", context do<commit_msg>added test for system component type search<commit_after>defmodule OpenAperture.Manager.Controllers.SystemComponentTest do
  use ExUnit.Case, async: false
  use Phoenix.ConnTest

  alias OpenAperture.Manager.DB.Models.MessagingExchange
  alias OpenAperture.Manager.DB.Models.SystemComponent
  alias OpenAperture.Manager.Repo
  alias OpenAperture.Manager.Plugs.Authentication

  alias OpenAperture.OverseerApi.Publisher, as: OverseerPublisher

  require Repo
  import Ecto.Query  

  setup_all do
    exchange = Repo.insert(MessagingExchange.new(%{name: "test exchange"}))

    :meck.new(Authentication, [:passthrough])
    :meck.expect(Authentication, :authenticate_user, fn conn, _opts -> conn end)

    on_exit fn ->
      :meck.unload

      Repo.delete_all(SystemComponent)
      Repo.delete_all(MessagingExchange)
    end

    {:ok, [exchange: exchange]}
  end

  setup do
    Repo.delete_all(SystemComponent)
    :ok
  end

  @endpoint OpenAperture.Manager.Endpoint

  # ==================================
  # index tests

  test "index - no components" do
    conn = get conn(), "/system_components"
    assert conn.status == 200

    body = Poison.decode!(conn.resp_body)

    assert length(body) == 0
  end

  test "index - components", context do
    Repo.insert(SystemComponent.new(%{messaging_exchange_id: context[:exchange].id, type: "test", source_repo: "https://github.com/test/test.git", source_repo_git_ref: "123abc", upgrade_strategy: "manual", deployment_repo: "https://github.com/test/test.git", deployment_repo_git_ref: "123abc"}))
    Repo.insert(SystemComponent.new(%{messaging_exchange_id: context[:exchange].id, type: "test", source_repo: "https://github.com/test/test.git", source_repo_git_ref: "123abc", upgrade_strategy: "manual", deployment_repo: "https://github.com/test/test.git", deployment_repo_git_ref: "123abc"}))

    conn = get conn(), "/system_components"
    assert conn.status == 200

    body = Poison.decode!(conn.resp_body)

    assert length(body) == 2
  end

  test "index - components w/ type search", context do
    Repo.insert(SystemComponent.new(%{messaging_exchange_id: context[:exchange].id, type: "manager", source_repo: "https://github.com/test/test.git", source_repo_git_ref: "123abc", upgrade_strategy: "manual", deployment_repo: "https://github.com/test/test.git", deployment_repo_git_ref: "123abc"}))
    Repo.insert(SystemComponent.new(%{messaging_exchange_id: context[:exchange].id, type: "builder", source_repo: "https://github.com/test/test.git", source_repo_git_ref: "123abc", upgrade_strategy: "manual", deployment_repo: "https://github.com/test/test.git", deployment_repo_git_ref: "123abc"}))

    conn = get conn(), "/system_components?type=manager"
    assert conn.status == 200

    body = Poison.decode!(conn.resp_body)

    assert length(body) == 1
  end

  # ==================================
  # show tests

  test "show - invalid component" do
    conn = get conn(), "/system_components/1234567890"
    assert conn.status == 404
  end

  test "show - valid component", context do
    component = Repo.insert(SystemComponent.new(%{messaging_exchange_id: context[:exchange].id, type: "test", source_repo: "https://github.com/test/test.git", source_repo_git_ref: "123abc", upgrade_strategy: "manual", deployment_repo: "https://github.com/test/test.git", deployment_repo_git_ref: "123abc", status: to_string(:available), upgrade_status: Poison.encode!(%{workflow_ids: []})}))

    conn = get conn(), "/system_components/#{component.id}"
    assert conn.status == 200

    returned_component = Poison.decode!(conn.resp_body)
    assert returned_component != nil
    assert returned_component["type"] == component.type
    assert returned_component["source_repo"] == component.source_repo
    assert returned_component["source_repo_git_ref"] == component.source_repo_git_ref
    assert returned_component["deployment_repo"] == component.deployment_repo
    assert returned_component["deployment_repo_git_ref"] == component.deployment_repo_git_ref    
    assert returned_component["upgrade_strategy"] == component.upgrade_strategy
  end  

  # ==================================
  # create tests

  test "create - conflict", context do
{'fraction_non_alphanumeric': 0.1494770289951013, 'fraction_numerical': 0.013239772275916854, 'mean_word_length': 4.869463869463869, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 16, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6337962', 'n_tokens_mistral': 2466, 'n_tokens_neox': 2267, 'n_words': 456}