/*
 * Copyright (c) 2025, WSO2 LLC. (http://www.wso2.com).
 *
 * WSO2 LLC. licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://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.
 */

package org.wso2.identity.integration.test.rest.api.server.workflow.v1;

import io.restassured.response.Response;
import org.apache.http.HttpStatus;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.wso2.carbon.automation.engine.context.TestUserMode;

import java.io.IOException;

/**
 * Test class for Workflow Management REST APIs failure paths.
 */
public class WorkflowFailureTest extends WorkflowBaseTest {

    public WorkflowFailureTest() throws Exception {

        super.init(TestUserMode.SUPER_TENANT_ADMIN);
        this.context = isServer;
        this.authenticatingUserName = context.getContextTenant().getTenantAdmin().getUserName();
        this.authenticatingCredential = context.getContextTenant().getTenantAdmin().getPassword();
        this.tenant = context.getContextTenant().getDomain();
    }

    @Override
    @BeforeClass(alwaysRun = true)
    public void init() throws IOException {

        super.testInit(API_VERSION, swaggerDefinition, tenant);
    }

    @Test
    public void testGetWorkflowWithInvalidId() {

        Response response = getResponseOfGet(WORKFLOW_API_BASE_PATH + PATH_SEPARATOR + "random-id");
        validateErrorResponse(response, HttpStatus.SC_NOT_FOUND, "WF-51001", "random-id");
    }

    @Test
    public void addWorkflowWithInvalidTemplate() throws IOException {

        Response response = getResponseOfPost(WORKFLOW_API_BASE_PATH, readResource("add-workflow-invalid-template" +
                ".json"));
        validateErrorResponse(response, HttpStatus.SC_BAD_REQUEST, "WF-51003", "SingleStepApprovalTemplate");
    }

    @Test
    public void addWorkflowWithInvalidEngine() throws IOException {

        Response response = getResponseOfPost(WORKFLOW_API_BASE_PATH, readResource("add-workflow-invalid-engine" +
                ".json"));
        validateErrorResponse(response, HttpStatus.SC_BAD_REQUEST, "WF-51003", "Sample Engine");
    }

    @Test
    public void updateWorkflowWithInvalidWorkflowId() throws IOException {

        String body = readResource("update-workflow.json");
        Response response =
                getResponseOfPut(WORKFLOW_API_BASE_PATH + PATH_SEPARATOR + "random-id", body);
        validateErrorResponse(response, HttpStatus.SC_BAD_REQUEST, "WF-51004", "random-id");
    }

    @Test
    public void testGetWorkflowAssociationWithInvalidId() {

        Response response = getResponseOfGet(WORKFLOW_ASSOCIATION_API_BASE_PATH + PATH_SEPARATOR + "random-id");
        validateErrorResponse(response, HttpStatus.SC_NOT_FOUND, "WF-51005", "random-id");
    }

    @Test
    public void addWorkflowAssociationWithInvalidWorkflow() throws IOException {

        Response response = getResponseOfPost(WORKFLOW_ASSOCIATION_API_BASE_PATH, readResource("add-association" +
                "-invalid-workflow.json"));
        validateErrorResponse(response, HttpStatus.SC_BAD_REQUEST, "WF-51007", "invalid-id");
    }

    @Test
    public void updateWorkflowAssociationWithInvalidAssociationId() throws IOException {

        String body = readResource("patch-association-name.json");
        Response response =
                getResponseOfPatch(WORKFLOW_ASSOCIATION_API_BASE_PATH + PATH_SEPARATOR + "random-id", body);
        validateErrorResponse(response, HttpStatus.SC_BAD_REQUEST, "WF-51008", "random-id");
    }
}
