aboutsummaryrefslogtreecommitdiffstats
path: root/odl-aaa-moon/aaa/aaa-authn-mdsal-store/aaa-authn-mdsal-store-impl/src/test/java/org/opendaylight/aaa/authn/mdsal/store/IDMStoreTestUtil.java
blob: 39eeadb4fa0192df317af291dd10533584d3f224 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
 * 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 static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import com.google.common.base.Optional;
import com.google.common.util.concurrent.CheckedFuture;
import java.util.concurrent.ExecutionException;
import org.opendaylight.aaa.api.IDMStoreUtil;
import org.opendaylight.controller.md.sal.binding.api.DataBroker;
import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
import org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.Authentication;
import org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.Domain;
import org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.DomainBuilder;
import org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.DomainKey;
import org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.Grant;
import org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.GrantBuilder;
import org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.GrantKey;
import org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.Role;
import org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.RoleBuilder;
import org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.RoleKey;
import org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.User;
import org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.UserBuilder;
import org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.UserKey;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;

public class IDMStoreTestUtil {
    /* DataBroker mocked with Mokito */
    protected static DataBroker dataBroker = mock(DataBroker.class);
    protected static WriteTransaction wrt = mock(WriteTransaction.class);
    protected static ReadOnlyTransaction rot = null;

    static {
        rot = (ReadOnlyTransaction) DataBrokerReadMocker.addMock(ReadOnlyTransaction.class);
        when(dataBroker.newReadOnlyTransaction()).thenReturn(rot);
        when(dataBroker.newWriteOnlyTransaction()).thenReturn(wrt);
    }

    /* Domain Data Object Instance */
    public Domain domain = createdomain();

    /* Domain create Method */
    public Domain createdomain() {
        /* Start of Domain builder */
        DomainBuilder domainbuilder = new DomainBuilder();
        domainbuilder.setName("SETNAME");
        domainbuilder.setDomainid("SETNAME");
        domainbuilder.setKey(new DomainKey("SETNAME"));
        domainbuilder.setDescription("SETDESCRIPTION");
        domainbuilder.setEnabled(true);
        /* End of Domain builder */
        return domainbuilder.build();
    }

    /* Role Data Object Instance */
    public Role role = createrole();

    /* Role create Method */
    public Role createrole() {
        /* Start of Role builder */
        RoleBuilder rolebuilder = new RoleBuilder();
        rolebuilder.setRoleid("SETNAME@SETNAME");
        rolebuilder.setName("SETNAME");
        rolebuilder.setKey(new RoleKey(rolebuilder.getRoleid()));
        rolebuilder.setDomainid(createdomain().getDomainid());
        rolebuilder.setDescription("SETDESCRIPTION");
        /* End of Role builder */
        return rolebuilder.build();
    }

    /* User Data Object Instance */
    public User user = createuser();

    /* User create Method */
    public User createuser() {
        /* Start of User builder */
        UserBuilder userbuilder = new UserBuilder();
        userbuilder.setUserid("SETNAME@SETNAME");
        userbuilder.setName("SETNAME");
        userbuilder.setKey(new UserKey(userbuilder.getUserid()));
        userbuilder.setDomainid(createdomain().getDomainid());
        userbuilder.setEmail("SETEMAIL");
        userbuilder.setPassword("SETPASSWORD");
        userbuilder.setSalt("SETSALT");
        userbuilder.setEnabled(true);
        userbuilder.setDescription("SETDESCRIPTION");
        /* End of User builder */
        return userbuilder.build();
    }

    /* Grant Data Object Instance */
    public Grant grant = creategrant();

    /* Grant create Method */
    public Grant creategrant() {
        /* Start of Grant builder */
        GrantBuilder grantbuilder = new GrantBuilder();
        grantbuilder.setDomainid(createdomain().getDomainid());
        grantbuilder.setRoleid(createrole().getRoleid());
        grantbuilder.setUserid(createuser().getUserid());
        grantbuilder.setGrantid(IDMStoreUtil.createGrantid(grantbuilder.getUserid(),
                grantbuilder.getDomainid(), grantbuilder.getRoleid()));
        grantbuilder.setKey(new GrantKey(grantbuilder.getGrantid()));
        /* End of Grant builder */
        return grantbuilder.build();
    }

    /* InstanceIdentifier for Grant instance grant */
    public InstanceIdentifier<Grant> grantID = InstanceIdentifier.create(Authentication.class)
                                                                 .child(Grant.class,
                                                                         creategrant().getKey());

    /* Mokito DataBroker method for grant Data Object */
    public void addMokitoForgrant() throws NoSuchMethodException, SecurityException, InterruptedException, ExecutionException {
        CheckedFuture<Optional<Grant>, ReadFailedException> read = mock(CheckedFuture.class);
        DataBrokerReadMocker.getMocker(rot).addWhen("read",
                new Object[] { LogicalDatastoreType.CONFIGURATION, grantID }, read);
        Optional<Grant> optional = mock(Optional.class);
        when(read.get()).thenReturn(optional);
        when(optional.get()).thenReturn(grant);
        when(optional.isPresent()).thenReturn(true);
    }

    /* InstanceIdentifier for Domain instance domain */
    public InstanceIdentifier<Domain> domainID = InstanceIdentifier.create(Authentication.class)
                                                                   .child(Domain.class,
                                                                           new DomainKey(
                                                                                   new String(
                                                                                           "SETNAME")));

    /* Mokito DataBroker method for domain Data Object */
    public void addMokitoFordomain() throws NoSuchMethodException, SecurityException, InterruptedException, ExecutionException {
        CheckedFuture<Optional<Domain>, ReadFailedException> read = mock(CheckedFuture.class);
        DataBrokerReadMocker.getMocker(rot).addWhen("read",
                new Object[] { LogicalDatastoreType.CONFIGURATION, domainID }, read);
        Optional<Domain> optional = mock(Optional.class);
        when(read.get()).thenReturn(optional);
        when(optional.get()).thenReturn(domain);
        when(optional.isPresent()).thenReturn(true);
    }

    /* InstanceIdentifier for Role instance role */
    public InstanceIdentifier<Role> roleID = InstanceIdentifier.create(Authentication.class).child(
            Role.class, createrole().getKey());

    /* Mokito DataBroker method for role Data Object */
    public void addMokitoForrole() throws NoSuchMethodException, SecurityException, InterruptedException, ExecutionException {
        CheckedFuture<Optional<Role>, ReadFailedException> read = mock(CheckedFuture.class);
        DataBrokerReadMocker.getMocker(rot).addWhen("read",
                new Object[] { LogicalDatastoreType.CONFIGURATION, roleID }, read);
        Optional<Role> optional = mock(Optional.class);
        when(read.get()).thenReturn(optional);
        when(optional.get()).thenReturn(role);
        when(optional.isPresent()).thenReturn(true);
    }

    /* InstanceIdentifier for User instance user */
    public InstanceIdentifier<User> userID = InstanceIdentifier.create(Authentication.class).child(
            User.class, createuser().getKey());

    /* Mokito DataBroker method for user Data Object */
    public void addMokitoForuser() throws NoSuchMethodException, SecurityException, InterruptedException, ExecutionException {
        CheckedFuture<Optional<User>, ReadFailedException> read = mock(CheckedFuture.class);
        DataBrokerReadMocker.getMocker(rot).addWhen("read",
                new Object[] { LogicalDatastoreType.CONFIGURATION, userID }, read);
        Optional<User> optional = mock(Optional.class);
        when(read.get()).thenReturn(optional);
        when(optional.get()).thenReturn(user);
        when(optional.isPresent()).thenReturn(true);
    }
}