001/* 002 * Copyright 2019-2024 Ping Identity Corporation 003 * All Rights Reserved. 004 */ 005/* 006 * Copyright 2019-2024 Ping Identity Corporation 007 * 008 * Licensed under the Apache License, Version 2.0 (the "License"); 009 * you may not use this file except in compliance with the License. 010 * You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, software 015 * distributed under the License is distributed on an "AS IS" BASIS, 016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 017 * See the License for the specific language governing permissions and 018 * limitations under the License. 019 */ 020/* 021 * Copyright (C) 2019-2024 Ping Identity Corporation 022 * 023 * This program is free software; you can redistribute it and/or modify 024 * it under the terms of the GNU General Public License (GPLv2 only) 025 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only) 026 * as published by the Free Software Foundation. 027 * 028 * This program is distributed in the hope that it will be useful, 029 * but WITHOUT ANY WARRANTY; without even the implied warranty of 030 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 031 * GNU General Public License for more details. 032 * 033 * You should have received a copy of the GNU General Public License 034 * along with this program; if not, see <http://www.gnu.org/licenses>. 035 */ 036package com.unboundid.ldap.sdk; 037 038 039 040import java.util.Collection; 041import java.util.List; 042 043import com.unboundid.asn1.ASN1OctetString; 044import com.unboundid.ldap.sdk.schema.Schema; 045import com.unboundid.ldif.LDIFException; 046import com.unboundid.util.Extensible; 047import com.unboundid.util.NotNull; 048import com.unboundid.util.Nullable; 049import com.unboundid.util.ThreadSafety; 050import com.unboundid.util.ThreadSafetyLevel; 051import com.unboundid.util.Validator; 052 053 054 055/** 056 * This class provides an implementation of an {@link FullLDAPInterface} that 057 * provides a basic means of mocking an {@link LDAPConnection} (which itself is 058 * not easily mockable because it is final, as a commonly recognized best 059 * practice for APIs). 060 */ 061@Extensible() 062@ThreadSafety(level= ThreadSafetyLevel.MOSTLY_THREADSAFE) 063public class MockableLDAPConnection 064 implements FullLDAPInterface 065{ 066 // The wrapped connection. 067 @NotNull private final LDAPConnection connection; 068 069 070 071 /** 072 * Creates a new mockable LDAP connection from the provided connection. All 073 * non-overridden methods will simply be delegated to the provided connection. 074 * 075 * @param connection The connection to which all non-overridden method calls 076 * will be delegated. 077 */ 078 public MockableLDAPConnection(@NotNull final LDAPConnection connection) 079 { 080 Validator.ensureNotNullWithMessage(connection, 081 "MockableLDAPConnection.connection must not be null."); 082 083 this.connection = connection; 084 } 085 086 087 088 /** 089 * Retrieves the connection that has been wrapped by this mockable LDAP 090 * connection, and to which all non-overridden method calls will be delegated. 091 * 092 * @return The connection that has been wrapped by this mockable LDAP 093 * connection, and to which all non-overridden method calls will be 094 * delegated. 095 */ 096 @NotNull() 097 public final LDAPConnection getWrappedConnection() 098 { 099 return connection; 100 } 101 102 103 104 /** 105 * {@inheritDoc} 106 */ 107 @Override() 108 public void close() 109 { 110 connection.close(); 111 } 112 113 114 115 /** 116 * {@inheritDoc} 117 */ 118 @Override() 119 @Nullable() 120 public RootDSE getRootDSE() 121 throws LDAPException 122 { 123 return connection.getRootDSE(); 124 } 125 126 127 128 /** 129 * {@inheritDoc} 130 */ 131 @Override() 132 @Nullable() 133 public Schema getSchema() 134 throws LDAPException 135 { 136 return connection.getSchema(); 137 } 138 139 140 141 /** 142 * {@inheritDoc} 143 */ 144 @Override() 145 @Nullable() 146 public Schema getSchema(@Nullable final String entryDN) 147 throws LDAPException 148 { 149 return connection.getSchema(entryDN); 150 } 151 152 153 154 /** 155 * {@inheritDoc} 156 */ 157 @Override() 158 @Nullable() 159 public SearchResultEntry getEntry(@NotNull final String dn) 160 throws LDAPException 161 { 162 return connection.getEntry(dn); 163 } 164 165 166 167 /** 168 * {@inheritDoc} 169 */ 170 @Override() 171 @Nullable() 172 public SearchResultEntry getEntry(@NotNull final String dn, 173 @Nullable final String... attributes) 174 throws LDAPException 175 { 176 return connection.getEntry(dn, attributes); 177 } 178 179 180 181 /** 182 * {@inheritDoc} 183 */ 184 @Override() 185 @NotNull() 186 public LDAPResult add(@NotNull final String dn, 187 @NotNull final Attribute... attributes) 188 throws LDAPException 189 { 190 return connection.add(dn, attributes); 191 } 192 193 194 195 /** 196 * {@inheritDoc} 197 */ 198 @Override() 199 @NotNull() 200 public LDAPResult add(@NotNull final String dn, 201 @NotNull final Collection<Attribute> attributes) 202 throws LDAPException 203 { 204 return connection.add(dn, attributes); 205 } 206 207 208 209 /** 210 * {@inheritDoc} 211 */ 212 @Override() 213 @NotNull() 214 public LDAPResult add(@NotNull final Entry entry) 215 throws LDAPException 216 { 217 return connection.add(entry); 218 } 219 220 221 222 /** 223 * {@inheritDoc} 224 */ 225 @Override() 226 @NotNull() 227 public LDAPResult add(@NotNull final String... ldifLines) 228 throws LDIFException, LDAPException 229 { 230 return connection.add(ldifLines); 231 } 232 233 234 235 /** 236 * {@inheritDoc} 237 */ 238 @Override() 239 @NotNull() 240 public LDAPResult add(@NotNull final AddRequest addRequest) 241 throws LDAPException 242 { 243 return connection.add(addRequest); 244 } 245 246 247 248 /** 249 * {@inheritDoc} 250 */ 251 @Override() 252 @NotNull() 253 public LDAPResult add(@NotNull final ReadOnlyAddRequest addRequest) 254 throws LDAPException 255 { 256 return connection.add(addRequest); 257 } 258 259 260 261 /** 262 * {@inheritDoc} 263 */ 264 @Override() 265 @NotNull() 266 public BindResult bind(@Nullable final String bindDN, 267 @Nullable final String password) 268 throws LDAPException 269 { 270 return connection.bind(bindDN, password); 271 } 272 273 274 275 /** 276 * {@inheritDoc} 277 */ 278 @Override() 279 @NotNull() 280 public BindResult bind(@NotNull final BindRequest bindRequest) 281 throws LDAPException 282 { 283 return connection.bind(bindRequest); 284 } 285 286 287 288 /** 289 * {@inheritDoc} 290 */ 291 @Override() 292 @NotNull() 293 public CompareResult compare(@NotNull final String dn, 294 @NotNull final String attributeName, 295 @NotNull final String assertionValue) 296 throws LDAPException 297 { 298 return connection.compare(dn, attributeName, assertionValue); 299 } 300 301 302 303 /** 304 * {@inheritDoc} 305 */ 306 @Override() 307 @NotNull() 308 public CompareResult compare(@NotNull final CompareRequest compareRequest) 309 throws LDAPException 310 { 311 return connection.compare(compareRequest); 312 } 313 314 315 316 /** 317 * {@inheritDoc} 318 */ 319 @Override() 320 @NotNull() 321 public CompareResult compare( 322 @NotNull final ReadOnlyCompareRequest compareRequest) 323 throws LDAPException 324 { 325 return connection.compare(compareRequest); 326 } 327 328 329 330 /** 331 * {@inheritDoc} 332 */ 333 @Override() 334 @NotNull() 335 public LDAPResult delete(@NotNull final String dn) 336 throws LDAPException 337 { 338 return connection.delete(dn); 339 } 340 341 342 343 /** 344 * {@inheritDoc} 345 */ 346 @Override() 347 @NotNull() 348 public LDAPResult delete(@NotNull final DeleteRequest deleteRequest) 349 throws LDAPException 350 { 351 return connection.delete(deleteRequest); 352 } 353 354 355 356 /** 357 * {@inheritDoc} 358 */ 359 @Override() 360 @NotNull() 361 public LDAPResult delete(@NotNull final ReadOnlyDeleteRequest deleteRequest) 362 throws LDAPException 363 { 364 return connection.delete(deleteRequest); 365 } 366 367 368 369 /** 370 * {@inheritDoc} 371 */ 372 @Override() 373 @NotNull() 374 public ExtendedResult processExtendedOperation( 375 @NotNull final String requestOID) 376 throws LDAPException 377 { 378 return connection.processExtendedOperation(requestOID); 379 } 380 381 382 383 /** 384 * {@inheritDoc} 385 */ 386 @Override() 387 @NotNull() 388 public ExtendedResult processExtendedOperation( 389 @NotNull final String requestOID, 390 @Nullable final ASN1OctetString requestValue) 391 throws LDAPException 392 { 393 return connection.processExtendedOperation(requestOID, requestValue); 394 } 395 396 397 398 /** 399 * {@inheritDoc} 400 */ 401 @Override() 402 @NotNull() 403 public ExtendedResult processExtendedOperation( 404 @NotNull final ExtendedRequest extendedRequest) 405 throws LDAPException 406 { 407 return connection.processExtendedOperation(extendedRequest); 408 } 409 410 411 412 /** 413 * {@inheritDoc} 414 */ 415 @Override() 416 @NotNull() 417 public LDAPResult modify(@NotNull final String dn, 418 @NotNull final Modification mod) 419 throws LDAPException 420 { 421 return connection.modify(dn, mod); 422 } 423 424 425 426 /** 427 * {@inheritDoc} 428 */ 429 @Override() 430 @NotNull() 431 public LDAPResult modify(@NotNull final String dn, 432 @NotNull final Modification... mods) 433 throws LDAPException 434 { 435 return connection.modify(dn, mods); 436 } 437 438 439 440 /** 441 * {@inheritDoc} 442 */ 443 @Override() 444 @NotNull() 445 public LDAPResult modify(@NotNull final String dn, 446 @NotNull final List<Modification> mods) 447 throws LDAPException 448 { 449 return connection.modify(dn, mods); 450 } 451 452 453 454 /** 455 * {@inheritDoc} 456 */ 457 @Override() 458 @NotNull() 459 public LDAPResult modify(@NotNull final String... ldifModificationLines) 460 throws LDIFException, LDAPException 461 { 462 return connection.modify(ldifModificationLines); 463 } 464 465 466 467 /** 468 * {@inheritDoc} 469 */ 470 @Override() 471 @NotNull() 472 public LDAPResult modify(@NotNull final ModifyRequest modifyRequest) 473 throws LDAPException 474 { 475 return connection.modify(modifyRequest); 476 } 477 478 479 480 /** 481 * {@inheritDoc} 482 */ 483 @Override() 484 @NotNull() 485 public LDAPResult modify(@NotNull final ReadOnlyModifyRequest modifyRequest) 486 throws LDAPException 487 { 488 return connection.modify(modifyRequest); 489 } 490 491 492 493 /** 494 * {@inheritDoc} 495 */ 496 @Override() 497 @NotNull() 498 public LDAPResult modifyDN(@NotNull final String dn, 499 @NotNull final String newRDN, 500 final boolean deleteOldRDN) 501 throws LDAPException 502 { 503 return connection.modifyDN(dn, newRDN, deleteOldRDN); 504 } 505 506 507 508 /** 509 * {@inheritDoc} 510 */ 511 @Override() 512 @NotNull() 513 public LDAPResult modifyDN(@NotNull final String dn, 514 @NotNull final String newRDN, 515 final boolean deleteOldRDN, 516 @Nullable final String newSuperiorDN) 517 throws LDAPException 518 { 519 return connection.modifyDN(dn, newRDN, deleteOldRDN, newSuperiorDN); 520 } 521 522 523 524 /** 525 * {@inheritDoc} 526 */ 527 @Override() 528 @NotNull() 529 public LDAPResult modifyDN(@NotNull final ModifyDNRequest modifyDNRequest) 530 throws LDAPException 531 { 532 return connection.modifyDN(modifyDNRequest); 533 } 534 535 536 537 /** 538 * {@inheritDoc} 539 */ 540 @Override() 541 @NotNull() 542 public LDAPResult modifyDN( 543 @NotNull final ReadOnlyModifyDNRequest modifyDNRequest) 544 throws LDAPException 545 { 546 return connection.modifyDN(modifyDNRequest); 547 } 548 549 550 551 /** 552 * {@inheritDoc} 553 */ 554 @Override() 555 @NotNull() 556 public SearchResult search(@NotNull final String baseDN, 557 @NotNull final SearchScope scope, 558 @NotNull final String filter, 559 @Nullable final String... attributes) 560 throws LDAPSearchException 561 { 562 return connection.search(baseDN, scope, filter, attributes); 563 } 564 565 566 567 /** 568 * {@inheritDoc} 569 */ 570 @Override() 571 @NotNull() 572 public SearchResult search(@NotNull final String baseDN, 573 @NotNull final SearchScope scope, 574 @NotNull final Filter filter, 575 @Nullable final String... attributes) 576 throws LDAPSearchException 577 { 578 return connection.search(baseDN, scope, filter, attributes); 579 } 580 581 582 583 /** 584 * {@inheritDoc} 585 */ 586 @Override() 587 @NotNull() 588 public SearchResult search( 589 @Nullable final SearchResultListener searchResultListener, 590 @NotNull final String baseDN, 591 @NotNull final SearchScope scope, 592 @NotNull final String filter, 593 @Nullable final String... attributes) 594 throws LDAPSearchException 595 { 596 return connection.search(searchResultListener, baseDN, scope, filter, 597 attributes); 598 } 599 600 601 602 /** 603 * {@inheritDoc} 604 */ 605 @Override() 606 @NotNull() 607 public SearchResult search( 608 @Nullable final SearchResultListener searchResultListener, 609 @NotNull final String baseDN, 610 @NotNull final SearchScope scope, 611 @NotNull final Filter filter, 612 @Nullable final String... attributes) 613 throws LDAPSearchException 614 { 615 return connection.search(searchResultListener, baseDN, scope, filter, 616 attributes); 617 } 618 619 620 621 /** 622 * {@inheritDoc} 623 */ 624 @Override() 625 @NotNull() 626 public SearchResult search(@NotNull final String baseDN, 627 @NotNull final SearchScope scope, 628 @NotNull final DereferencePolicy derefPolicy, 629 final int sizeLimit, final int timeLimit, 630 final boolean typesOnly, 631 @NotNull final String filter, 632 @Nullable final String... attributes) 633 throws LDAPSearchException 634 { 635 return connection.search(baseDN, scope, derefPolicy, sizeLimit, timeLimit, 636 typesOnly, filter, attributes); 637 } 638 639 640 641 /** 642 * {@inheritDoc} 643 */ 644 @Override() 645 @NotNull() 646 public SearchResult search(@NotNull final String baseDN, 647 @NotNull final SearchScope scope, 648 @NotNull final DereferencePolicy derefPolicy, 649 final int sizeLimit, final int timeLimit, 650 final boolean typesOnly, 651 @NotNull final Filter filter, 652 @Nullable final String... attributes) 653 throws LDAPSearchException 654 { 655 return connection.search(baseDN, scope, derefPolicy, sizeLimit, timeLimit, 656 typesOnly, filter, attributes); 657 } 658 659 660 661 /** 662 * {@inheritDoc} 663 */ 664 @Override() 665 @NotNull() 666 public SearchResult search( 667 @Nullable final SearchResultListener searchResultListener, 668 @NotNull final String baseDN, @NotNull final SearchScope scope, 669 @NotNull final DereferencePolicy derefPolicy, final int sizeLimit, 670 final int timeLimit, final boolean typesOnly, 671 @NotNull final String filter, 672 @Nullable final String... attributes) 673 throws LDAPSearchException 674 { 675 return connection.search(searchResultListener, baseDN, scope, derefPolicy, 676 sizeLimit, timeLimit, typesOnly, filter, attributes); 677 } 678 679 680 681 /** 682 * {@inheritDoc} 683 */ 684 @Override() 685 @NotNull() 686 public SearchResult search( 687 @Nullable final SearchResultListener searchResultListener, 688 @NotNull final String baseDN, @NotNull final SearchScope scope, 689 @NotNull final DereferencePolicy derefPolicy, final int sizeLimit, 690 final int timeLimit, final boolean typesOnly, 691 @NotNull final Filter filter, 692 @Nullable final String... attributes) 693 throws LDAPSearchException 694 { 695 return connection.search(searchResultListener, baseDN, scope, derefPolicy, 696 sizeLimit, timeLimit, typesOnly, filter, attributes); 697 } 698 699 700 701 /** 702 * {@inheritDoc} 703 */ 704 @Override() 705 @NotNull() 706 public SearchResult search(@NotNull final SearchRequest searchRequest) 707 throws LDAPSearchException 708 { 709 return connection.search(searchRequest); 710 } 711 712 713 714 /** 715 * {@inheritDoc} 716 */ 717 @Override() 718 @NotNull() 719 public SearchResult search(@NotNull final ReadOnlySearchRequest searchRequest) 720 throws LDAPSearchException 721 { 722 return connection.search(searchRequest); 723 } 724 725 726 727 /** 728 * {@inheritDoc} 729 */ 730 @Override() 731 @Nullable() 732 public SearchResultEntry searchForEntry(@NotNull final String baseDN, 733 @NotNull final SearchScope scope, 734 @NotNull final String filter, 735 @Nullable final String... attributes) 736 throws LDAPSearchException 737 { 738 return connection.searchForEntry(baseDN, scope, filter, attributes); 739 } 740 741 742 743 /** 744 * {@inheritDoc} 745 */ 746 @Override() 747 @Nullable() 748 public SearchResultEntry searchForEntry(@NotNull final String baseDN, 749 @NotNull final SearchScope scope, 750 @NotNull final Filter filter, 751 @Nullable final String... attributes) 752 throws LDAPSearchException 753 { 754 return connection.searchForEntry(baseDN, scope, filter, attributes); 755 } 756 757 758 759 /** 760 * {@inheritDoc} 761 */ 762 @Override() 763 @Nullable() 764 public SearchResultEntry searchForEntry(@NotNull final String baseDN, 765 @NotNull final SearchScope scope, 766 @NotNull final DereferencePolicy derefPolicy, 767 final int timeLimit, final boolean typesOnly, 768 @NotNull final String filter, 769 @Nullable final String... attributes) 770 throws LDAPSearchException 771 { 772 return connection.searchForEntry(baseDN, scope, derefPolicy, timeLimit, 773 typesOnly, filter, attributes); 774 } 775 776 777 778 /** 779 * {@inheritDoc} 780 */ 781 @Override() 782 @Nullable() 783 public SearchResultEntry searchForEntry(@NotNull final String baseDN, 784 @NotNull final SearchScope scope, 785 @NotNull final DereferencePolicy derefPolicy, 786 final int timeLimit, final boolean typesOnly, 787 @NotNull final Filter filter, 788 @Nullable final String... attributes) 789 throws LDAPSearchException 790 { 791 return connection.searchForEntry(baseDN, scope, derefPolicy, timeLimit, 792 typesOnly, filter, attributes); 793 } 794 795 796 797 /** 798 * {@inheritDoc} 799 */ 800 @Override() 801 @Nullable() 802 public SearchResultEntry searchForEntry( 803 @NotNull final SearchRequest searchRequest) 804 throws LDAPSearchException 805 { 806 return connection.searchForEntry(searchRequest); 807 } 808 809 810 811 /** 812 * {@inheritDoc} 813 */ 814 @Override() 815 @Nullable() 816 public SearchResultEntry searchForEntry( 817 @NotNull final ReadOnlySearchRequest searchRequest) 818 throws LDAPSearchException 819 { 820 return connection.searchForEntry(searchRequest); 821 } 822}