aboutsummaryrefslogtreecommitdiffstats
path: root/appc-dg/appc-dg-shared/appc-dg-netconf/src/test/java/org/openecomp/appc/dg/netconf/impl/NetconfDBPluginImplTest.java
blob: a591648c42f240648967582269f0f6f80e9b4102 (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*-
 * ============LICENSE_START=======================================================
 * openECOMP : APP-C
 * ================================================================================
 * Copyright (C) 2017 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.openecomp.appc.dg.netconf.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.*;
import org.openecomp.appc.adapter.netconf.ConnectionDetails;
import org.openecomp.appc.adapter.netconf.NetconfConnectionDetails;
import org.openecomp.appc.adapter.netconf.NetconfDataAccessService;
import org.openecomp.appc.adapter.netconf.exception.DataAccessException;
import org.openecomp.appc.dg.netconf.impl.NetconfDBPluginImpl;
import org.openecomp.appc.exceptions.APPCException;
import org.openecomp.sdnc.sli.SvcLogicContext;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import static org.powermock.api.support.SuppressCode.suppressConstructor;

public class NetconfDBPluginImplTest {
    private NetconfDBPluginImpl netconfDBPlugin;
    private NetconfDataAccessService daoService;
    private DAOServiceMock daoMock;
    private Map<String, String> params;
    private static final String DG_OUTPUT_STATUS_MESSAGE = "output.status.message";
    String host = "http://www.test.com";
    String host1 = "http://www.test1.com";
    int port = 8080;
    String username = "test";
    String password = "test";
    String configContent = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
            "<rpc message-id=\"101\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
            "\t<get-config>\n" +
            "\t\t<source>\n" +
            "\t\t\t<running/>\n" +
            "\t\t </source>\n" +
            "\t</get-config>\n" +
            "</rpc>'";


    @Test
    public void testRetrieveDSConfiguration() throws Exception {
        init();
        params = new HashMap<>();
        params.put("org.openecomp.appc.vftype", "VNF");
        params.put("configuration-file-name", "VnfGetRunningConfig");
        SvcLogicContext ctx = new SvcLogicContext();
        netconfDBPlugin.retrieveDSConfiguration(params, ctx);

        Assert.assertEquals("lack of success of status", "success", ctx.getStatus());
        Assert.assertEquals("wrong config file content", configContent, ctx.getAttribute("file-content"));
    }


    @Test
    public void testRetrieveDSConfigurationNegativeErrorFieldNameDaoException() throws Exception {
        init();
        SvcLogicContext ctx = new SvcLogicContext();
        params = new HashMap<>();
        params.put("configuration-file-name", "wrong");

        try {
            netconfDBPlugin.retrieveDSConfiguration(params, ctx);
        } catch (DataAccessException e) {
            //Assert.assertNotNull(ctx.getAttribute("org.openecomp.appc.dg.error"));
            Assert.assertNull(ctx.getAttribute("file-content"));
        }


    }

    @Test
    public void testRetrieveVMDSConfiguration() throws Exception {
        init();
        params = new HashMap<>();
        params.put("resourceKey", "VNF");
        SvcLogicContext ctx = new SvcLogicContext();
        netconfDBPlugin.retrieveVMDSConfiguration(params, ctx);

        Assert.assertEquals("lack of success of retrieveVMDSConfiguration_Result", "success", ctx.getAttribute("retrieveVMDSConfiguration_Result"));
        Assert.assertEquals("wrong entity", "VNF", ctx.getAttribute("entity"));
        assertConnectionDetails(ctx, host);
    }

    @Test
    public void testRetrieveVMDSConfigurationNegativeMissingConfiguration() throws Exception {
        init();
        SvcLogicContext ctx = new SvcLogicContext();
        params = new HashMap<>();
        params.put("resourceKey", "MOCK");

        try {
            netconfDBPlugin.retrieveVMDSConfiguration(params, ctx);
            Assert.assertTrue(false);
        } catch (APPCException e) {

            Assert.assertEquals("failure", ctx.getAttribute("retrieveVMDSConfiguration_Result"));
        }
    }


    @Test
    public void testRetrieveVMDSConfigurationNegativeJsonProcessingException() throws Exception {

        SvcLogicContext ctx = new SvcLogicContext();
        params = new HashMap<>();
        params.put("resourceKey", "VNF");

        init();
        substituteMapper(true);
        try {
            netconfDBPlugin.retrieveVMDSConfiguration(params, ctx);
            substituteMapper(false);
            Assert.assertTrue(false);

        } catch (APPCException e) {
            substituteMapper(false);
            Assert.assertNotNull(ctx.getAttribute(DG_OUTPUT_STATUS_MESSAGE));

        }

    }

    @Test
    public void testRetrieveConfigFile() throws Exception {
        init();
        SvcLogicContext ctx = new SvcLogicContext();
        params = new HashMap<>();
        params.put("configuration-file-name", "VnfGetRunningConfig");
        netconfDBPlugin.retrieveConfigFile(params, ctx);

        Assert.assertEquals("lack of success of status", "success", ctx.getStatus());
        Assert.assertEquals("wrong config file content", configContent, ctx.getAttribute("file-content"));
    }

    @Test
    public void testRetrieveConnectionDetails() throws Exception {
        init();
        params = new HashMap<>();
        params.put("org.openecomp.appc.vftype", "VNF");
        params.put("vnf-host-ip-address", host1);
        SvcLogicContext ctx = new SvcLogicContext();
        netconfDBPlugin.retrieveConnectionDetails(params, ctx);

        assertConnectionDetails(ctx, host1);
    }

    @Test
    public void testRetrieveConnectionDetailsNegativeJsonProcessingException() throws Exception {
        init();
        params = new HashMap<>();
        params.put("org.openecomp.appc.vftype", "MOCK");
        params.put("vnf-host-ip-address", host1);
        SvcLogicContext ctx = new SvcLogicContext();

        try {
            netconfDBPlugin.retrieveConnectionDetails(params, ctx);
            Assert.assertTrue(false);
        } catch (APPCException e) {
            Assert.assertNull(ctx.getAttribute("connection-details"));
            Assert.assertNotNull(ctx.getAttribute(DG_OUTPUT_STATUS_MESSAGE));
        }

    }


    @Test
    public void testRetrieveConnectionDetailsNegativeMissingConfiguration() throws Exception {
        init();
        params = new HashMap<>();
        params.put("org.openecomp.appc.vftype", "VNF");
        params.put("vnf-host-ip-address", host1);
        SvcLogicContext ctx = new SvcLogicContext();
        substituteMapper(true);

        try {
            netconfDBPlugin.retrieveConnectionDetails(params, ctx);
            substituteMapper(false);
            Assert.assertTrue(false);
        } catch (APPCException e) {
            substituteMapper(false);
            Assert.assertNull(ctx.getAttribute("connection-details"));
            Assert.assertNotNull(ctx.getAttribute(DG_OUTPUT_STATUS_MESSAGE));
        }

    }

    private void assertConnectionDetails(SvcLogicContext ctx, String host) throws IOException {
        String sConnectionDetails = ctx.getAttribute("connection-details");
        NetconfConnectionDetails connectionDetails = new ObjectMapper().readValue(sConnectionDetails, NetconfConnectionDetails.class);
        Assert.assertEquals(host, connectionDetails.getHost());
        Assert.assertEquals(port, connectionDetails.getPort());
        Assert.assertEquals(username, connectionDetails.getUsername());
        Assert.assertEquals(password, connectionDetails.getPassword());
        Assert.assertNull(connectionDetails.getCapabilities());
        Assert.assertNull(connectionDetails.getAdditionalProperties());
    }

    private void init() {
        netconfDBPlugin = new NetconfDBPluginImpl();
        daoService = new DAOServiceMock();
        netconfDBPlugin.setDaoService(daoService);
        daoMock = (DAOServiceMock) daoService;
        daoMock.setConfigFile(configContent);
        daoMock.setConnection(getConnectionDetails());

    }

    private ConnectionDetails getConnectionDetails() {
        ConnectionDetails connectionDetails = new ConnectionDetails();
        connectionDetails.setHost(host);
        connectionDetails.setUsername(username);
        connectionDetails.setPort(port);
        connectionDetails.setPassword(password);
        return connectionDetails;
    }

    private void substituteMapper(boolean command) throws NoSuchFieldException, IllegalAccessException {
        ObjectMapper mapper = new ObjectMapperMock();
        ObjectMapper mapper2 = new ObjectMapper();
        Field field = NetconfDBPluginImpl.class.getDeclaredField("mapper");
        field.setAccessible(true);
        if (command) {
            field.set(netconfDBPlugin, mapper);
        } else {
            field.set(netconfDBPlugin, mapper2);
        }
    }
}