summaryrefslogtreecommitdiffstats
path: root/holmes-actions/src/test/java/org/onap/holmes/common/aai/AaiQuery4CcvpnTest.java
blob: 0df99b5827536c80347a3afd906172ec693145b8 (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
/**
 * Copyright 2018-2023 ZTE Corporation.
 * <p>
 * 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
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * 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.
 */

package org.onap.holmes.common.aai;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.easymock.EasyMock;
import org.junit.*;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.onap.holmes.common.aai.config.AaiConfig;
import org.onap.holmes.common.exception.CorrelationException;
import org.onap.holmes.common.utils.JerseyClient;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

import static org.easymock.EasyMock.anyObject;
import static org.easymock.EasyMock.anyString;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;

@RunWith(PowerMockRunner.class)
@PrepareForTest(JerseyClient.class)
@SuppressStaticInitializationFor("org.onap.holmes.common.utils.JerseyClient")
public class AaiQuery4CcvpnTest {

    @Rule
    public ExpectedException thrown = ExpectedException.none();

    private static JsonObject data;

    private static AaiQuery4Ccvpn aai = AaiQuery4Ccvpn.newInstance();

    private static Map<String, Object> headers = new HashMap<>();

    private static JerseyClient client;

    @BeforeClass
    static public void beforeClass() {
        File file = new File(AaiQuery4CcvpnTest.class.getClassLoader().getResource("./ccvpn.data.json").getFile());
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(file));
            StringBuilder sb = new StringBuilder();
            reader.lines().forEach(l -> sb.append(l));
            data = JsonParser.parseString(sb.toString()).getAsJsonObject();
        } catch (FileNotFoundException e) {
            // Do nothing
        } catch (IOException e) {
            // Do nothing
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    // Do nothing
                }
            }
        }

        headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
        headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
        headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
        headers.put("Accept", "application/json");
        headers.put("Content-Type", "application/json");
        Whitebox.setInternalState(aai, "headers", headers);
    }

    @Before
    public void before() {
        PowerMock.mockStatic(JerseyClient.class);
        client = PowerMock.createMock(JerseyClient.class);
        EasyMock.expect(JerseyClient.newInstance()).andReturn(client).anyTimes();
    }

    @After
    public void after() {
        PowerMock.resetAll();
    }

    @Test
    public void test_getPath() throws Exception {
        String path = "/aai/v14/business/customers/customer/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances?service-instance-id={servId}";
        String ret = Whitebox.invokeMethod(aai, "getPath", path);
        assertThat(ret, equalTo("/api/aai-business/v14/customers/customer/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances?service-instance-id={servId}"));
    }

    @Test
    public void test_getLogicLink() {
        mockGetMethod(data.get("logic-link").toString());

        PowerMock.replayAll();

        String linkId = aai.getLogicLink("network-1", "pnf-1", "interface-1", "DOWN");

        PowerMock.verifyAll();

        assertThat(linkId, equalTo("logic-link-1"));

    }


    @Test
    public void test_getServiceInstance() {
        mockGetMethod(data.get("vpn-binding").toString());
        mockGetMethod(data.get("connectivity").toString());
        mockGetMethod(data.get("service-instance-by-connectivity").toString());
        mockGetMethod(data.get("service-instances-by-service-type").toString());

        PowerMock.replayAll();

        JsonObject instance = aai.getServiceInstance("network-1", "pnf-1", "interface-1", "DOWN");

        PowerMock.verifyAll();

        assertThat(instance.get("service-instance-id").getAsString(), equalTo("some id 1"));
        assertThat(instance.get("globalSubscriberId").getAsString(), equalTo("e151059a-d924-4629-845f-264db19e50b4"));
        assertThat(instance.get("serviceType").getAsString(), equalTo("volte"));
    }

    @Test
    public void test_updateTerminalPointStatus() throws CorrelationException {
        mockGetMethod(data.toString());
        mockPutMethod("ok");

        PowerMock.replayAll();

        aai.updateTerminalPointStatus("network-1", "pnf-1", "if-1", new HashMap<>());

        PowerMock.verifyAll();
    }


    @Test
    public void test_updateLogicLinkStatus() {
        mockGetMethod(data.toString());
        mockPutMethod("ok");

        PowerMock.replayAll();

        aai.updateLogicLinkStatus("link-1", new HashMap<>());

        PowerMock.verifyAll();
    }

    private void mockGetMethod(String ret) {
        EasyMock.expect(client.path(anyString())).andReturn(client);
        EasyMock.expect(client.headers(anyObject())).andReturn(client);
        EasyMock.expect(client.get(anyString())).andReturn(ret);
    }

    private void mockPutMethod(String ok) {
        EasyMock.expect(client.path(anyString())).andReturn(client);
        EasyMock.expect(client.headers(anyObject())).andReturn(client);
        EasyMock.expect(client.put(anyString(), anyObject())).andReturn(ok);
    }
}