/*
 * Copyright 2019 Red Hat, Inc. and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 *
 *      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.kie.server.integrationtests.dmn;

import java.math.BigDecimal;
import java.util.Map;

import org.assertj.core.api.Assertions;
import org.junit.Assume;
import org.junit.BeforeClass;
import org.junit.Test;
import org.kie.api.KieServices;
import org.kie.dmn.api.core.DMNContext;
import org.kie.dmn.api.core.DMNResult;
import org.kie.server.api.marshalling.MarshallingFormat;
import org.kie.server.api.model.KieServiceResponse;
import org.kie.server.api.model.ReleaseId;
import org.kie.server.api.model.ServiceResponse;
import org.kie.server.integrationtests.shared.KieServerDeployer;

public class DMNPMMLIntegrationTest extends DMNKieServerBaseIntegrationTest {

    private static final ReleaseId kjar1 = new ReleaseId(
            "org.kie.server.testing", "dmn-pmml-kjar",
            "1.0.0.Final" );

    private static final String CONTAINER_1_ID  = "dmn-pmml";
    private static final String CONTAINER_1_ALIAS  = "pmml";

    private static final String SCORECARD_MODEL_NAMESPACE
            = "http://www.trisotech.com/definitions/_ca466dbe-20b4-4e88-a43f-4ce3aff26e4f";
    private static final String SCORECARD_MODEL_NAME = "KiePMMLScoreCard";
    private static final String SCORECARD_DECISION_NAME = "my decision";
    private static final String SCORECARD_DECISION_RESULT = "calculatedScore";

    private static final String REGRESSION_MODEL_NAMESPACE
            = "https://kiegroup.org/dmn/_51A1FD67-8A67-4332-9889-B718BE8B7456";
    private static final String REGRESSION_MODEL_NAME = "TestRegressionDMN";
    private static final String REGRESSION_DECISION_NAME = "Decision";
    private static final String REGRESSION_DECISION_RESULT = "result";



    @BeforeClass
    public static void deployArtifacts() {
        KieServerDeployer.buildAndDeployCommonMavenParent();
        KieServerDeployer.buildAndDeployMavenProjectFromResource("/kjars-sources/dmn-pmml");

        kieContainer = KieServices.Factory.get().newKieContainer(kjar1);
        createContainer(CONTAINER_1_ID, kjar1, CONTAINER_1_ALIAS);
    }

    @Test
    public void testDMNwithPMMLScorecard() {
        Assume.assumeFalse(marshallingFormat == MarshallingFormat.JSON); // RHDM-1050
        final DMNContext dmnContext = dmnClient.newContext();
        final ServiceResponse<DMNResult> serviceResponse = dmnClient.evaluateDecisionByName(
                CONTAINER_1_ID,
                SCORECARD_MODEL_NAMESPACE,
                SCORECARD_MODEL_NAME,
                SCORECARD_DECISION_NAME,
                dmnContext);

        Assertions.assertThat(serviceResponse.getType()).isEqualTo(KieServiceResponse.ResponseType.SUCCESS);

        final DMNResult dmnResult = serviceResponse.getResult();
        Assertions.assertThat(dmnResult).isNotNull();
        Assertions.assertThat(dmnResult.hasErrors()).isFalse();
        final BigDecimal result = (BigDecimal) ((Map) dmnResult.getDecisionResultByName(SCORECARD_DECISION_NAME)
                .getResult()).get(SCORECARD_DECISION_RESULT);
        Assertions.assertThat(result).isEqualTo(new BigDecimal("41.345"));
    }

    @Test
    public void testDMNWithPMMLRegression() {
        final DMNContext dmnContext = dmnClient.newContext();
        dmnContext.set("fld1", 3);
        dmnContext.set("fld2", 2);
        dmnContext.set("fld3", "y");
        final ServiceResponse<DMNResult> serviceResponse = dmnClient.evaluateDecisionByName(
                CONTAINER_1_ID,
                REGRESSION_MODEL_NAMESPACE,
                REGRESSION_MODEL_NAME,
                REGRESSION_DECISION_NAME,
                dmnContext);

        Assertions.assertThat(serviceResponse.getType()).isEqualTo(KieServiceResponse.ResponseType.SUCCESS);

        final DMNResult dmnResult = serviceResponse.getResult();
        Assertions.assertThat(dmnResult).isNotNull();
        Assertions.assertThat(dmnResult.hasErrors()).isFalse();
        final BigDecimal result = (BigDecimal) ((Map) dmnResult.getDecisionResultByName(REGRESSION_DECISION_NAME)
                .getResult()).get(REGRESSION_DECISION_RESULT);
        Assertions.assertThat(result).isEqualTo(new BigDecimal("52.5"));
    }
}

@@ -957,16 +957,70 @@
 Format;%0A
+import org.kie.server.api.model.KieContainerResource;%0A
 import o
@@ -1055,32 +1055,32 @@
 erviceResponse;%0A
-
 import org.kie.s
@@ -1147,32 +1147,143 @@
 erviceResponse;%0A
+import org.kie.server.client.KieServicesClient;%0Aimport org.kie.server.integrationtests.shared.KieServerAssert;%0A
 import org.kie.s
@@ -1619,69 +1619,8 @@
 ml%22;
-%0A    private static final String CONTAINER_1_ALIAS  = %22pmml%22;
 %0A%0A  
@@ -2354,17 +2354,74 @@
 sult%22;%0A%0A
+    private static final long EXTENDED_TIMEOUT = 300000L;
 %0A
-
 %0A    @Be
@@ -2701,24 +2701,25 @@
 kjar1);%0A
+%0A
         
 createCo
@@ -2714,64 +2714,330 @@
     
-createContainer(CONTAINER_1_ID, kjar1, CONTAINER_1_ALIAS
+// Having timeout issues due to pmml -%3E raised timeout.%0A        KieServicesClient client = createDefaultStaticClient(EXTENDED_TIMEOUT);%0A        ServiceResponse%3CKieContainerResource%3E reply = client.createContainer(CONTAINER_1_ID, new KieContainerResource(CONTAINER_1_ID, kjar1));%0A        KieServerAssert.assertSuccess(reply
 );%0A 

Extend container creation timeout for DMNPMMLIntegrationTest (#1886)