/*
 * 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.ranger.usergroupsync;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.List;

import org.junit.Before;
import org.junit.Test;

public class TestRegEx {
	protected String userNameBaseProperty = "ranger.usersync.mapping.username.regex";
    protected String groupNameBaseProperty = "ranger.usersync.mapping.groupname.regex";
    protected RegEx userNameRegEx = null;
    protected RegEx groupNameRegEx = null;
    List<String> userRegexPatterns = null;
    List<String> groupRegexPatterns = null;

	@Before
	public void setUp() throws Exception {
		userNameRegEx = new RegEx();
        //userNameRegEx.init(userNameBaseProperty);
        userRegexPatterns = new ArrayList<String>();
        groupNameRegEx = new RegEx();
        //groupNameRegEx.init(groupNameBaseProperty);
        groupRegexPatterns = new ArrayList<String>();
	}

	@Test
    public void testUserNameTransform() throws Throwable {
            userRegexPatterns.add("s/\\s/_/");
            userNameRegEx.populateReplacementPatterns(userNameBaseProperty, userRegexPatterns);
            assertEquals("test_user", userNameRegEx.transform("test user"));
    }

    @Test
    public void testGroupNameTransform() throws Throwable {
            groupRegexPatterns.add("s/\\s/_/g");
            groupRegexPatterns.add("s/_/\\$/g");
            groupNameRegEx.populateReplacementPatterns(userNameBaseProperty, groupRegexPatterns);
            assertEquals("ldap$grp", groupNameRegEx.transform("ldap grp"));
    }

    @Test
    public void testEmptyTransform() {
            assertEquals("test user", userNameRegEx.transform("test user"));
            assertEquals("ldap grp", groupNameRegEx.transform("ldap grp"));
    }

    @Test
    public void testTransform() throws Throwable {
            userRegexPatterns.add("s/\\s/_/g");
            userNameRegEx.populateReplacementPatterns(userNameBaseProperty, userRegexPatterns);
            assertEquals("test_user", userNameRegEx.transform("test user"));
            assertEquals("ldap grp", groupNameRegEx.transform("ldap grp"));
    }

    @Test
    public void testTransform1() throws Throwable {
            userRegexPatterns.add("s/\\\\/ /g");
            userRegexPatterns.add("s//_/g");
            userNameRegEx.populateReplacementPatterns(userNameBaseProperty, userRegexPatterns);
            groupRegexPatterns.add("s/\\s/\\$/g");
            groupRegexPatterns.add("s/\\s");
            groupRegexPatterns.add("s/\\$//g");
            groupNameRegEx.populateReplacementPatterns(userNameBaseProperty, groupRegexPatterns);
            assertEquals("test user", userNameRegEx.transform("test\\user"));
            assertEquals("ldapgrp", groupNameRegEx.transform("ldap grp"));
    }

}

@@ -2190,36 +2190,37 @@
 acementPatterns(
-user
+group
 NameBaseProperty
@@ -2635,32 +2635,81 @@
 d(%22s/%5C%5Cs/_/g%22);%0A
+            groupRegexPatterns.add(%22s/%5C%5Cs/_/g%22);%0A
             user
@@ -2780,32 +2780,131 @@
 RegexPatterns);%0A
+            groupNameRegEx.populateReplacementPatterns(groupNameBaseProperty, groupRegexPatterns);%0A
             asse
@@ -2986,33 +2986,33 @@
 sertEquals(%22ldap
- 
+_
 grp%22, groupNameR
@@ -3490,36 +3490,37 @@
 acementPatterns(
-user
+group
 NameBaseProperty

Fix issue found during coverity scan in one of the ranger usersync unit tests