<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF 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.apache.syncope.fit.core;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

import java.util.List;
import org.apache.syncope.common.lib.SyncopeConstants;
import org.apache.syncope.common.lib.request.UserCR;
import org.apache.syncope.common.lib.to.AnyTypeClassTO;
import org.apache.syncope.common.lib.to.CamelRouteTO;
import org.apache.syncope.common.lib.to.PlainSchemaTO;
import org.apache.syncope.common.lib.to.UserTO;
import org.apache.syncope.common.lib.types.AnyTypeKind;
import org.apache.syncope.common.lib.types.AttrSchemaType;
import org.apache.syncope.common.lib.types.SchemaType;
import org.apache.syncope.fit.AbstractITCase;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class CamelRouteITCase extends AbstractITCase {

    @BeforeEach
    public void check() {
        assumeTrue(syncopeService.platform().getProvisioningInfo().getUserProvisioningManager().contains("Camel"));
    }

    @Test
    public void userRoutes() {
        List<CamelRouteTO> userRoutes = camelRouteService.list(AnyTypeKind.USER);
        assertNotNull(userRoutes);
        assertEquals(16, userRoutes.size());
        userRoutes.forEach(route -> assertNotNull(route.getContent()));
    }

    @Test
    public void groupRoutes() {
        List<CamelRouteTO> groupRoutes = camelRouteService.list(AnyTypeKind.GROUP);
        assertNotNull(groupRoutes);
        assertEquals(8, groupRoutes.size());
        groupRoutes.forEach(route -> assertNotNull(route.getContent()));
    }

    private CamelRouteTO doUpdate(final AnyTypeKind anyTypeKind, final String key, final String content) {
        CamelRouteTO route = camelRouteService.read(anyTypeKind, key);
        route.setContent(content);
        camelRouteService.update(anyTypeKind, route);
        // getting new route definition
        return camelRouteService.read(anyTypeKind, key);
    }

    @Test
    public void update() {
        CamelRouteTO oldRoute = camelRouteService.read(AnyTypeKind.USER, "createUser");
        assertNotNull(oldRoute);
        String routeContent = "<route id=\"createUser\">\n"
                + "  <from uri=\"direct:createUser\"/>\n"
                + "  <setProperty propertyName=\"actual\">\n"
                + "    <simple>${body}</simple>\n"
                + "  </setProperty>\n"
                + "  <doTry>\n"
                + "    <bean ref=\"uwfAdapter\" method=\"create(${body},${exchangeProperty.disablePwdPolicyCheck},\n"
                + "                             ${exchangeProperty.enabled})\"/>\n"
                + "    <to uri=\"propagate:create?anyTypeKind=USER\"/>\n"
                + "    <to uri=\"direct:createPort\"/>\n"
                + "    <to uri=\"log:myLog\"/>\n"
                + "    <doCatch>        \n"
                + "      <exception>java.lang.RuntimeException</exception>\n"
                + "      <handled>\n"
                + "        <constant>false</constant>\n"
                + "      </handled>\n"
                + "      <to uri=\"direct:createPort\"/>\n"
                + "    </doCatch>\n"
                + "  </doTry>\n"
                + "</route>";
        try {
            CamelRouteTO route = doUpdate(AnyTypeKind.USER, "createUser", routeContent);
            assertEquals(routeContent, route.getContent());
        } finally {
            doUpdate(AnyTypeKind.USER, oldRoute.getKey(), oldRoute.getContent());
        }
    }

    @Test
    public void scriptingUpdate() {
        CamelRouteTO oldRoute = camelRouteService.read(AnyTypeKind.USER, "createUser");
        // updating route content including new attribute management

        String routeContent = ""
                + "  <route id=\"createUser\">\n"
                + "    <from uri=\"direct:createUser\"/>\n"
                + "    <setProperty propertyName=\"actual\">\n"
                + "      <simple>${body}</simple>\n"
                + "    </setProperty>\n"
                + "    <setBody>\n"
                + "     <groovy>\n"
                + "request.body.getPlainAttr(\"camelAttribute\").get().getValues().set(0,\"true\")\n"
                + "       return request.body\n"
                + "     </groovy>\n"
                + "    </setBody>\n"
                + "    <doTry>\n"
                + "      <bean ref=\"uwfAdapter\" method=\"create(${body},${exchangeProperty.disablePwdPolicyCheck},\n"
                + "                                     ${exchangeProperty.enabled})\"/>\n"
                + "      <to uri=\"propagate:create?anyTypeKind=USER\"/>\n"
                + "      <to uri=\"direct:createPort\"/>\n"
                + "      <doCatch>        \n"
                + "        <exception>java.lang.RuntimeException</exception>\n"
                + "        <handled>\n"
                + "          <constant>false</constant>\n"
                + "        </handled>\n"
                + "        <to uri=\"direct:createPort\"/>\n"
                + "      </doCatch>\n"
                + "    </doTry>\n"
                + "  </route> ";
        try {
            doUpdate(AnyTypeKind.USER, "createUser", routeContent);

            // creating new schema attribute for user
            PlainSchemaTO schemaTO = new PlainSchemaTO();
            schemaTO.setKey("camelAttribute");
            schemaTO.setType(AttrSchemaType.String);
            createSchema(SchemaType.PLAIN, schemaTO);

            AnyTypeClassTO typeClass = new AnyTypeClassTO();
            typeClass.setKey("camelAttribute");
            typeClass.getPlainSchemas().add(schemaTO.getKey());
            anyTypeClassService.create(typeClass);

            UserCR userCR = new UserCR();
            userCR.setRealm(SyncopeConstants.ROOT_REALM);
            userCR.getAuxClasses().add(typeClass.getKey());
            String userId = getUUIDString() + "<EMAIL>";
            userCR.setUsername(userId);
            userCR.setPassword("<PASSWORD>");
            userCR.getPlainAttrs().add(attr("userId", userId));
            userCR.getPlainAttrs().add(attr("fullname", userId));
            userCR.getPlainAttrs().add(attr("surname", userId));
            userCR.getPlainAttrs().add(attr("camelAttribute", "false"));

            UserTO userTO = createUser(userCR).getEntity();
            assertNotNull(userTO);
            assertEquals("true", userTO.getPlainAttr("camelAttribute").get().getValues().get(0));
        } finally {
            doUpdate(AnyTypeKind.USER, oldRoute.getKey(), oldRoute.getContent());
        }
    }

    @Test
    public void issueSYNCOPE931() {
        CamelRouteTO oldRoute = camelRouteService.read(AnyTypeKind.USER, "createUser");
        assertNotNull(oldRoute);
        String routeContent = "<route id=\"createUser\">\n"
                + "  <from uri=\"direct:createUser\"/>\n"
                + "  <setProperty propertyName=\"actual\">\n"
                + "    <simple>${body}</simple>\n"
                + "  </setProperty>\n"
                + "  <doTry>\n"
                + "    <bean ref=\"uwfAdapter\" method=\"create(${body},${exchangeProperty.disablePwdPolicyCheck},\n"
                + "                             ${exchangeProperty.enabled})\"/>\n"
                + "    <to uri=\"propagate:create123?anyTypeKind=USER\"/>\n"
                + "    <to uri=\"direct:createPort\"/>\n"
                + "    <to uri=\"log:myLog\"/>\n"<commit_msg>Fix route definition after Camel upgrade (part 2)<commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF 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.apache.syncope.fit.core;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

import java.util.List;
import org.apache.syncope.common.lib.SyncopeConstants;
import org.apache.syncope.common.lib.request.UserCR;
import org.apache.syncope.common.lib.to.AnyTypeClassTO;
import org.apache.syncope.common.lib.to.CamelRouteTO;
import org.apache.syncope.common.lib.to.PlainSchemaTO;
import org.apache.syncope.common.lib.to.UserTO;
import org.apache.syncope.common.lib.types.AnyTypeKind;
import org.apache.syncope.common.lib.types.AttrSchemaType;
import org.apache.syncope.common.lib.types.SchemaType;
import org.apache.syncope.fit.AbstractITCase;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class CamelRouteITCase extends AbstractITCase {

    @BeforeEach
    public void check() {
        assumeTrue(syncopeService.platform().getProvisioningInfo().getUserProvisioningManager().contains("Camel"));
    }

    @Test
    public void userRoutes() {
        List<CamelRouteTO> userRoutes = camelRouteService.list(AnyTypeKind.USER);
        assertNotNull(userRoutes);
        assertEquals(16, userRoutes.size());
        userRoutes.forEach(route -> assertNotNull(route.getContent()));
    }

    @Test
    public void groupRoutes() {
        List<CamelRouteTO> groupRoutes = camelRouteService.list(AnyTypeKind.GROUP);
        assertNotNull(groupRoutes);
        assertEquals(8, groupRoutes.size());
        groupRoutes.forEach(route -> assertNotNull(route.getContent()));
    }

    private CamelRouteTO doUpdate(final AnyTypeKind anyTypeKind, final String key, final String content) {
        CamelRouteTO route = camelRouteService.read(anyTypeKind, key);
        route.setContent(content);
        camelRouteService.update(anyTypeKind, route);
        // getting new route definition
        return camelRouteService.read(anyTypeKind, key);
    }

    @Test
    public void update() {
        CamelRouteTO oldRoute = camelRouteService.read(AnyTypeKind.USER, "createUser");
        assertNotNull(oldRoute);
        String routeContent = "<route id=\"createUser\">\n"
                + "  <from uri=\"direct:createUser\"/>\n"
                + "  <setProperty name=\"actual\">\n"
                + "    <simple>${body}</simple>\n"
                + "  </setProperty>\n"
                + "  <doTry>\n"
                + "    <bean ref=\"uwfAdapter\" method=\"create(${body},${exchangeProperty.disablePwdPolicyCheck},\n"
                + "                             ${exchangeProperty.enabled})\"/>\n"
                + "    <to uri=\"propagate:create?anyTypeKind=USER\"/>\n"
                + "    <to uri=\"direct:createPort\"/>\n"
                + "    <to uri=\"log:myLog\"/>\n"
                + "    <doCatch>        \n"
                + "      <exception>java.lang.RuntimeException</exception>\n"
                + "      <handled>\n"
                + "        <constant>false</constant>\n"
                + "      </handled>\n"
                + "      <to uri=\"direct:createPort\"/>\n"
                + "    </doCatch>\n"
                + "  </doTry>\n"
                + "</route>";
        try {
            CamelRouteTO route = doUpdate(AnyTypeKind.USER, "createUser", routeContent);
            assertEquals(routeContent, route.getContent());
        } finally {
            doUpdate(AnyTypeKind.USER, oldRoute.getKey(), oldRoute.getContent());
        }
    }

    @Test
    public void scriptingUpdate() {
        CamelRouteTO oldRoute = camelRouteService.read(AnyTypeKind.USER, "createUser");
        // updating route content including new attribute management

        String routeContent = ""
                + "  <route id=\"createUser\">\n"
                + "    <from uri=\"direct:createUser\"/>\n"
                + "    <setProperty name=\"actual\">\n"
                + "      <simple>${body}</simple>\n"
                + "    </setProperty>\n"
                + "    <setBody>\n"
                + "     <groovy>\n"
                + "request.body.getPlainAttr(\"camelAttribute\").get().getValues().set(0,\"true\")\n"
                + "       return request.body\n"
                + "     </groovy>\n"
                + "    </setBody>\n"
                + "    <doTry>\n"
                + "      <bean ref=\"uwfAdapter\" method=\"create(${body},${exchangeProperty.disablePwdPolicyCheck},\n"
                + "                                     ${exchangeProperty.enabled})\"/>\n"
                + "      <to uri=\"propagate:create?anyTypeKind=USER\"/>\n"
                + "      <to uri=\"direct:createPort\"/>\n"
                + "      <doCatch>        \n"
                + "        <exception>java.lang.RuntimeException</exception>\n"
                + "        <handled>\n"
                + "          <constant>false</constant>\n"
                + "        </handled>\n"
                + "        <to uri=\"direct:createPort\"/>\n"
                + "      </doCatch>\n"
                + "    </doTry>\n"
                + "  </route> ";
        try {
            doUpdate(AnyTypeKind.USER, "createUser", routeContent);

            // creating new schema attribute for user
            PlainSchemaTO schemaTO = new PlainSchemaTO();
            schemaTO.setKey("camelAttribute");
            schemaTO.setType(AttrSchemaType.String);
            createSchema(SchemaType.PLAIN, schemaTO);

            AnyTypeClassTO typeClass = new AnyTypeClassTO();
            typeClass.setKey("camelAttribute");
            typeClass.getPlainSchemas().add(schemaTO.getKey());
            anyTypeClassService.create(typeClass);

            UserCR userCR = new UserCR();
            userCR.setRealm(SyncopeConstants.ROOT_REALM);
            userCR.getAuxClasses().add(typeClass.getKey());
            String userId = getUUIDString() + "<EMAIL>";
            userCR.setUsername(userId);
            userCR.setPassword("<PASSWORD>");
            userCR.getPlainAttrs().add(attr("userId", userId));
            userCR.getPlainAttrs().add(attr("fullname", userId));
            userCR.getPlainAttrs().add(attr("surname", userId));
            userCR.getPlainAttrs().add(attr("camelAttribute", "false"));

            UserTO userTO = createUser(userCR).getEntity();
            assertNotNull(userTO);
            assertEquals("true", userTO.getPlainAttr("camelAttribute").get().getValues().get(0));
        } finally {
            doUpdate(AnyTypeKind.USER, oldRoute.getKey(), oldRoute.getContent());
        }
    }

    @Test
    public void issueSYNCOPE931() {
        CamelRouteTO oldRoute = camelRouteService.read(AnyTypeKind.USER, "createUser");
        assertNotNull(oldRoute);
        String routeContent = "<route id=\"createUser\">\n"
                + "  <from uri=\"direct:createUser\"/>\n"
                + "  <setProperty name=\"actual\">\n"
                + "    <simple>${body}</simple>\n"
                + "  </setProperty>\n"
                + "  <doTry>\n"
                + "    <bean ref=\"uwfAdapter\" method=\"create(${body},${exchangeProperty.disablePwdPolicyCheck},\n"
                + "                             ${exchangeProperty.enabled})\"/>\n"
                + "    <to uri=\"propagate:create123?anyTypeKind=USER\"/>\n"
                + "    <to uri=\"direct:createPort\"/>\n"
                + "    <to uri=\"log:myLog\"/>\n"
{'fraction_non_alphanumeric': 0.13768029562522352, 'fraction_numerical': 0.0018476576469185838, 'mean_word_length': 2.0775862068965516, 'pattern_counts': {'":': 0, '<': 127, '<?xml version=': 0, '>': 131, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2773375', 'n_tokens_mistral': 4835, 'n_tokens_neox': 4676, 'n_words': 810}