aboutsummaryrefslogtreecommitdiffstats
path: root/cadi/core/src/test/java/org/onap/ccsdk/apps/cadi/lur/test/JU_EpiLur.java
blob: 37183080f3b3a1df43cd86196af47e1feee6dcf9 (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
/**
 *
 * ============LICENSE_START====================================================
 * org.onap.ccsdk
 * ===========================================================================
 * Copyright (c) 2023 AT&T Intellectual Property. All rights reserved.
 * ===========================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END====================================================
 *
 */

package org.onap.ccsdk.apps.cadi.lur.test;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.when;

import java.security.Principal;
import java.util.ArrayList;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.onap.ccsdk.apps.cadi.CachingLur;
import org.onap.ccsdk.apps.cadi.CadiException;
import org.onap.ccsdk.apps.cadi.CredVal;
import org.onap.ccsdk.apps.cadi.Lur;
import org.onap.ccsdk.apps.cadi.Permission;
import org.onap.ccsdk.apps.cadi.lur.EpiLur;

public class JU_EpiLur {

    private ArrayList<Permission> perms;
    private CredValStub lurMock3;

    @Mock private Lur lurMock1;
    @Mock private CachingLur<?> lurMock2;
    @Mock private Principal princMock;
    @Mock private Permission permMock;

    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);

        perms = new ArrayList<>();
        perms.add(permMock);

        lurMock3 = new CredValStub();
    }

    @Test
    public void test() throws CadiException {
        EpiLur lur;
        try {
            lur = new EpiLur();
        } catch (CadiException e) {
            assertThat(e.getMessage(), is("Need at least one Lur implementation in constructor"));
        }
        lur = new EpiLur(lurMock1, lurMock2, lurMock3);
        assertThat(lur.fish(null,  null), is(false));

        assertThat(lur.fish(princMock, permMock), is(false));

        when(lurMock2.handlesExclusively(permMock)).thenReturn(true);
        assertThat(lur.fish(princMock, permMock), is(false));

        when(lurMock2.fish(princMock, permMock)).thenReturn(true);
        assertThat(lur.fish(princMock, permMock), is(true));

        lur.fishAll(princMock, perms);

        assertThat(lur.handlesExclusively(permMock), is(false));

        assertThat(lur.get(-1), is(nullValue()));
        assertThat(lur.get(0), is(lurMock1));
        assertThat(lur.get(1), is((Lur)lurMock2));
        assertThat(lur.get(2), is((Lur)lurMock3));
        assertThat(lur.get(3), is(nullValue()));

        assertThat(lur.handles(princMock), is(false));
        when(lurMock2.handles(princMock)).thenReturn(true);
        assertThat(lur.handles(princMock), is(true));

        lur.remove("id");

        lur.clear(princMock, null);

        assertThat(lur.createPerm("perm"), is(not(nullValue())));

        lur.getUserPassImpl();
        assertThat(lur.getUserPassImpl(), is((CredVal)lurMock3));

        lur.toString();
        lur.destroy();

        lur = new EpiLur(lurMock1, lurMock2);
        assertThat(lur.getUserPassImpl(), is(nullValue()));

        assertThat(lur.subLur(Lur.class), is(nullValue()));
    }

    private class CredValStub implements Lur, CredVal {
        @Override public boolean validate(String user, Type type, byte[] cred, Object state) { return false; }
        @Override public Permission createPerm(String p) { return null; }
        @Override public boolean fish(Principal bait, Permission ... pond) { return false; }
        @Override public void fishAll(Principal bait, List<Permission> permissions) { }
        @Override public void destroy() { }
        @Override public boolean handlesExclusively(Permission ... pond) { return false; }
        @Override public boolean handles(Principal principal) { return false; }
        @Override public void clear(Principal p, StringBuilder report) { }
    }

}