001/* 002 * Copyright © 2025 CUI-OpenSource-Software (info@cuioss.de) 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package de.cuioss.test.generator.domain; 017 018import de.cuioss.test.generator.TypedGenerator; 019 020import java.util.ArrayList; 021import java.util.List; 022 023import static de.cuioss.test.generator.Generators.fixedValues; 024import static de.cuioss.test.generator.Generators.integers; 025 026/** 027 * Generates LDAP Distinguished Names (DN) for testing purposes. 028 * The generated DNs follow the standard format of comma-separated name-value pairs. 029 * 030 * <p>Components:</p> 031 * <ul> 032 * <li>Prefixes: ou (Organizational Unit), o (Organization), dc (Domain Component)</li> 033 * <li>Values: Common LDAP terms like proxies, accounts, groups, roles, etc.</li> 034 * </ul> 035 * 036 * <p>Generation rules:</p> 037 * <ul> 038 * <li>Each DN contains 2-12 components</li> 039 * <li>Components are in the format prefix=value</li> 040 * <li>Components are joined with commas</li> 041 * </ul> 042 * 043 * <p><em>Example outputs:</em></p> 044 * <pre> 045 * "ou=proxies,dc=accounts,o=services" 046 * "dc=groups,ou=roles,dc=ID" 047 * </pre> 048 * 049 * <p><em>Example usage:</em></p> 050 * <pre> 051 * var generator = new DistinguishedNamesGenerator(); 052 * String dn = generator.next(); // Returns a valid LDAP DN string 053 * </pre> 054 * 055 * @author Oliver Wolff 056 */ 057public class DistinguishedNamesGenerator implements TypedGenerator<String> { 058 059 private final TypedGenerator<String> prefixes = fixedValues("ou", "o", "dc"); 060 private final TypedGenerator<String> values = fixedValues("proxies", "ID", "dc", "accounts", "groups", "roles", 061 "services"); 062 063 @Override 064 public String next() { 065 List<String> elements = new ArrayList<>(); 066 int count = integers(2, 12).next(); 067 for (var i = 0; i < count; i++) { 068 elements.add(prefixes.next() + "=" + values.next()); 069 } 070 return String.join(",", elements); 071 } 072 073 @Override 074 public Class<String> getType() { 075 return String.class; 076 } 077}