################################################################################
# Catena-X - Test Suite
#
# Copyright (c) 2026 Catena-X Automotive Network e.V.
# Copyright (c) 2026 Contributors to the Eclipse Foundation
#
# See the NOTICE file(s) distributed with this work for additional
# information regarding copyright ownership.
#
# This program and the accompanying materials are made available under the
# terms of the Apache License, Version 2.0 which is available at
# https://www.apache.org/licenses/LICENSE-2.0.
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
# SPDX-License-Identifier: Apache-2.0
################################################################################
## This code was partially generated using artificial intelligence (AI) (Tool: Copilot, Model: Claude Sonnet 4.6).
## It was reviewed and tested by a human committer.

kind: test
syntax: v1-alpha

namespace: certificate-management-tck-v0.0.1
id: request-certificate

metadata:
  name: "Request Certificate"
  version: "v1.0.0"
  description: >
    Request certificate via CCMAPI endpoint per CX-0135 v3.1.0. Validates
    HTTP 200 response, header message ID format, and payload schema.

execution:
  - id: pull_ccmapi_endpoint
    uses: connector/consumer/pull_data_filtered
    name: Discover CCMAPI offer and obtain dataplane credentials
    with:
      counter_party_address: "${{ env.sut_counter_party_address }}"
      counter_party_id: "${{ env.sut_counter_party_id }}"
      expected_policies: "${{ env.ccm_usage_policy }}"
      filters:
        - operand_left: "https://w3id.org/edc/v0.0.1/ns/type"
          operator: "="
          operand_right: "https://w3id.org/catenax/taxonomy#CCMAPI"
        - operand_left: "http://purl.org/dc/terms/subject"
          operator: "="
          operand_right: "https://w3id.org/catenax/taxonomy#CompanyCertificateManagementNotificationApi"
        - operand_left: "https://w3id.org/catenax/ontology/common#version"
          operator: "="
          operand_right: "3.0"
    returns:
      edr_token:
        type: string
        class: AuthToken
      dataplane_url:
        type: string
    validate:
      - uses: validate/assert
        with: { input: edr_token, operator: not_null }
      - uses: validate/assert
        with: { input: dataplane_url, operator: not_null }

  - id: request_certificate
    uses: connector/dataplane/http_request
    name: POST certificate request to CCMAPI endpoint via dataplane
    with:
      method: POST
      dataplane_url: "${{ execution.pull_ccmapi_endpoint.dataplane_url }}"
      path: "/companycertificate/request"
      edr_token: "${{ execution.pull_ccmapi_endpoint.edr_token }}"
      headers:
        Content-Type: "application/json"
      body: "${{ env.testdata.request_certificate_body }}"
    returns:
      status_code:
        type: integer
      response_body:
        type: object
        class: ResponseBody
    validate:
      - uses: validate/field
        with: { input: status_code, operator: equals, value: 200 }
      - uses: validate/field
        with:
          input: response_body
          path: "header.messageId"
          operator: matches_regex
          value: "^urn:uuid:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$"
      - uses: validate/schema
        with:
          input: response_body
          schema: "${{ env.schemas.certificate_schema }}"
