#!/usr/share/ucs-test/runner bash
# shellcheck shell=bash
## desc: "Make concurrent changes in UCS"
## exposure: dangerous
## packages:
## - univention-s4-connector
## bugs:
##  - 33621
##  - 33466

# shellcheck source=../../lib/base.sh
. "$TESTLIBPATH/base.sh" || exit 137
# shellcheck source=../../lib/udm.sh
. "$TESTLIBPATH/udm.sh" || exit 137
# shellcheck source=../../lib/random.sh
. "$TESTLIBPATH/random.sh" || exit 137

if [ "$(command -v univention-ad-connector)" ]; then
	echo "SKIP if AD-Connector installed: Flaky if AD-Connector is also installed"
	exit 138
fi

. "s4connector.sh" || exit 137
test -n "$connector_s4_ldap_host" || exit 137
connector_running_on_this_host || exit 137

for((i=0;i<10;i++)); do

	echo "********************************************"
	echo "* Round $((i+1)) of 10"
	echo "********************************************"

	UDM_users_user_username="$(random_chars)"
	UDM_users_user_lastname="$(random_chars)"
	UDM_users_user_password="univention"
	udm_create "users/user"; fail_bool 0 110

	ad_wait_for_synchronization; fail_bool 0 110

	udm_modify "users/user" "" "" "" "" \
		--set description="univention" || fail_test 110

	sleep $i
	udm_modify "users/user" "" "" "" "" \
		--set homeTelephoneNumber="1" || fail_test 110

	sleep $i
	udm_modify "users/user" "" "" "" "" \
		--set phone="2"|| fail_test 110

	sleep $i
	udm_modify "users/user" "" "" "" "" \
		--set organisation="Test"|| fail_test 110

	sleep $i
	udm_modify "users/user" "" "" "" "" \
		--set street="Street"|| fail_test 110

	sleep $i
	udm_modify "users/user" "" "" "" "" \
		--set city="Bremen"|| fail_test 110

	sleep $i
	udm_modify "users/user" "" "" "" "" \
		--set postcode="28359"|| fail_test 110

	sleep $i
	udm_modify "users/user" "" "" "" "" \
		--set mobileTelephoneNumber="3"|| fail_test 110

	sleep $i
	udm_modify "users/user" "" "" "" "" \
		--set pagerTelephoneNumber="4"|| fail_test 110

	sleep $i
	udm_modify "users/user" "" "" "" "" \
		--set sambaUserWorkstations="pc01"|| fail_test 110

	ad_wait_for_synchronization; fail_bool 0 110

	udm_verify_multi_value_udm_attribute_contains "description" "univention" "users/user"; fail_bool 0 110
	udm_verify_multi_value_udm_attribute_contains "homeTelephoneNumber" "1" "users/user"; fail_bool 0 110
	udm_verify_multi_value_udm_attribute_contains "phone" "2" "users/user"; fail_bool 0 110
	udm_verify_multi_value_udm_attribute_contains "organisation" "Test" "users/user"; fail_bool 0 110
	udm_verify_multi_value_udm_attribute_contains "street" "Street" "users/user"; fail_bool 0 110
	udm_verify_multi_value_udm_attribute_contains "city" "Bremen" "users/user"; fail_bool 0 110
	udm_verify_multi_value_udm_attribute_contains "postcode" "28359" "users/user"; fail_bool 0 110
	udm_verify_multi_value_udm_attribute_contains "mobileTelephoneNumber" "3" "users/user"; fail_bool 0 110
	udm_verify_multi_value_udm_attribute_contains "pagerTelephoneNumber" "4" "users/user"; fail_bool 0 110
	udm_verify_multi_value_udm_attribute_contains "sambaUserWorkstations" "pc01" "users/user"; fail_bool 0 110

	udm_remove "users/user" || fail_test 110

	if [ "$RETVAL" != 100 ]; then
		break
	fi
done

exit $RETVAL
