aboutsummaryrefslogtreecommitdiffstats
path: root/upstream/odl-aaa-moon/aaa/aaa-authn-mdsal-store/aaa-authn-mdsal-store-impl/src/test/java/org/opendaylight/aaa/authn/mdsal/store/DataBrokerReadMocker.java
blob: f821cf166643cf98690d826c2b84e6baebcb0a3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */

package org.opendaylight.aaa.authn.mdsal.store;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DataBrokerReadMocker implements InvocationHandler {
    private Map<Method, List<StubContainer>> stubs = new HashMap<Method, List<StubContainer>>();
    private Class<?> mokingClass = null;

    @Override
    public Object invoke(Object arg0, Method arg1, Object[] arg2) throws Throwable {
        List<StubContainer> stList = stubs.get(arg1);
        if (stList != null) {
            for (StubContainer sc : stList) {
                if (sc.fitGeneric(arg2)) {
                    return sc.returnObject;
                }
            }
        }
        return null;
    }

    public DataBrokerReadMocker(Class<?> cls) {
        this.mokingClass = cls;
    }

    public static Object addMock(Class<?> cls) {
        return Proxy.newProxyInstance(cls.getClassLoader(), new Class[] { cls },
                new DataBrokerReadMocker(cls));
    }

    public static DataBrokerReadMocker getMocker(Object o) {
        return (DataBrokerReadMocker) Proxy.getInvocationHandler(o);
    }

    public static Method findMethod(Class<?> cls, String name, Object args[]) {
        Method methods[] = cls.getMethods();
        for (Method m : methods) {
            if (m.getName().equals(name)) {
                if ((m.getParameterTypes() == null || m.getParameterTypes().length == 0)
                        && args == null) {
                    return m;
                }
                boolean match = true;
                for (int i = 0; i < m.getParameterTypes().length; i++) {
                    if (!m.getParameterTypes()[i].isAssignableFrom(args[i].getClass())) {
                        match = false;
                    }
                }
                if (match)
                    return m;
            }
        }
        return null;
    }

    public void addWhen(String methodName, Object[] args, Object returnThis)
            throws NoSuchMethodException, SecurityException {
        Method m = findMethod(this.mokingClass, methodName, args);
        if (m == null)
            throw new IllegalArgumentException("Unable to find method");
        StubContainer sc = new StubContainer(args, returnThis);
        List<StubContainer> lst = stubs.get(m);
        if (lst == null) {
            lst = new ArrayList<>();
        }
        lst.add(sc);
        stubs.put(m, lst);
    }

    private class StubContainer {
        private Class<?>[] parameters = null;
        private Class<?>[] generics = null;
        private Object args[] = null;
        private Object returnObject;

        public StubContainer(Object[] _args, Object ret) {
            this.args = _args;
            this.returnObject = ret;
        }

        public boolean fitGeneric(Object _args[]) {
            if (args == null && _args != null)
                return false;
            if (args != null && _args == null)
                return false;
            if (args == null && _args == null)
                return true;
            if (args.length != _args.length)
                return false;
            for (int i = 0; i < args.length; i++) {
                if (!args[i].equals(_args[i])) {
                    return false;
                }
            }
            return true;
        }
    }
}