Cleanup mockito-configuration
[yangtools.git] / common / mockito-configuration / src / main / java / org / opendaylight / mockito / ArgumentsExtractorVerifier.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.mockito;
9
10 import org.mockito.exceptions.base.MockitoException;
11 import org.mockito.internal.invocation.InvocationsFinder;
12 import org.mockito.internal.verification.VerificationModeFactory;
13 import org.mockito.internal.verification.api.VerificationData;
14 import org.mockito.verification.VerificationMode;
15
16 /**
17  * Verifier that extracts arguments from actual invocation. Useful when deeper validation of arguments is needed.
18  */
19 public final class ArgumentsExtractorVerifier implements VerificationMode {
20     private Object[] arguments = null;
21
22     @Override
23     public void verify(final VerificationData data) {
24         final var actualInvocations = InvocationsFinder.findInvocations(data.getAllInvocations(), data.getTarget());
25         final var size = actualInvocations.size();
26         switch (size) {
27             case 1 -> {
28                 final var invocation = actualInvocations.get(0);
29                 arguments = invocation.getArguments();
30                 invocation.markVerified();
31             }
32             default -> throw new MockitoException("This verifier can only be used with 1 invocation, got " + size);
33         }
34     }
35
36     @Override
37     public VerificationMode description(final String description) {
38         return VerificationModeFactory.description(this, description);
39     }
40
41     /**
42      * Return verified invocation arguments, or {@code null} if not verified.
43      *
44      * @return verified invocation arguments, or {@code null} if not verified
45      */
46     public Object[] getArguments() {
47         return arguments == null ? null : arguments.clone();
48     }
49 }