aboutsummaryrefslogtreecommitdiffstats
path: root/appc-dispatcher/appc-command-executor/appc-command-executor-core/src/test/java/org/onap/appc/executor/impl/TestCommandExecutor.java
blob: 3385b84899c2c57df02f041eb0a967e8e36fe490 (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
/*-
 * ============LICENSE_START=======================================================
 * ONAP : APPC
 * ================================================================================
 * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Copyright (C) 2017 Amdocs
 * ================================================================================
 * Modifications Copyright (C) 2019 Ericsson
 * =============================================================================
 * 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.appc.executor.impl;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.onap.appc.domainmodel.lcm.*;
import org.onap.appc.exceptions.APPCException;
import org.onap.appc.executionqueue.ExecutionQueueService;
import org.onap.appc.executor.impl.objects.CommandRequest;
import org.onap.appc.executor.objects.CommandExecutorInput;
import org.onap.appc.requesthandler.RequestHandler;
import org.onap.appc.workflow.WorkFlowManager;
import org.powermock.api.mockito.PowerMockito;

import java.util.Date;
import java.util.concurrent.TimeUnit;


public class TestCommandExecutor {

    private static final String API_VERSION = "2.0.0";
    private static final String ORIGINATOR_ID = "1";

    private CommandExecutorImpl commandExecutor;

    private RequestHandler requestHandler;
    private WorkFlowManager workflowManager;
    private ExecutionQueueService executionQueueService;

    private Date timeStamp = new Date();
    private String requestId = "1";
    private CommandExecutorInput commandExecutorInputConfigure = populateCommandExecutorInput("FIREWALL", 30000, "1.0",
            timeStamp, API_VERSION, requestId, ORIGINATOR_ID, "2", VNFOperation.Configure, "15", "");
    private CommandExecutorInput commandExecutorInputSync = populateCommandExecutorInput("FIREWALL", 30, "1.0",
            timeStamp, API_VERSION, requestId, ORIGINATOR_ID, "2", VNFOperation.Sync, "15", "");
    private CommandTask commandTask;

    @Before
    public void init() throws Exception {
        requestHandler = Mockito.mock(RequestHandler.class);
        workflowManager = Mockito.mock(WorkFlowManager.class);

        executionQueueService = Mockito.mock(ExecutionQueueService.class);

        commandExecutor = Mockito.spy(new CommandExecutorImpl());
        commandExecutor.setExecutionQueueService(executionQueueService);
        commandExecutor.setRequestHandler(requestHandler);
        commandExecutor.setWorkflowManager(workflowManager);
        commandExecutor.initialize();
        commandTask = Mockito.mock(CommandTask.class);
        Mockito.when(commandTask.getCommandRequest()).thenReturn(new CommandRequest(commandExecutorInputConfigure));
        PowerMockito.whenNew(CommandTask.class)
                .withParameterTypes(RequestHandler.class, WorkFlowManager.class)
                .withArguments(requestHandler, workflowManager)
                .thenReturn(commandTask);
    }

    @Test
    public void testPositiveFlow_LCM() throws Exception {
        try {
            Mockito.doReturn(commandTask).when(commandExecutor).getCommandTask(Mockito.any(), Mockito.any());
            commandExecutor.executeCommand(commandExecutorInputConfigure);
        } catch (APPCException e) {
            Assert.fail(e.toString());
        }
    }

    @Test(expected = APPCException.class)
    public void testNegativeFlow_LCM() throws APPCException {
            Mockito.doThrow(new APPCException("Failed to enqueue request"))
                    .when(executionQueueService)
                    .putMessage((Runnable) Mockito.anyObject(), Mockito.anyLong(), (TimeUnit) Mockito.anyObject());
            commandExecutor.executeCommand(commandExecutorInputSync);
    }

    private CommandExecutorInput populateCommandExecutorInput(String vnfType, int ttl, String vnfVersion,
            Date timeStamp, String apiVersion, String requestId, String originatorID, String subRequestID,
            VNFOperation action, String vnfId, String payload) {
        CommandExecutorInput commandExecutorInput = createCommandExecutorInputWithSubObjects();
        RuntimeContext runtimeContext = commandExecutorInput.getRuntimeContext();
        RequestContext requestContext = runtimeContext.getRequestContext();
        CommonHeader commonHeader = requestContext.getCommonHeader();
        commonHeader.getFlags().setTtl(ttl);
        commonHeader.setApiVer(apiVersion);
        commonHeader.setTimestamp(timeStamp);
        commonHeader.setRequestId(requestId);
        commonHeader.setSubRequestId(subRequestID);
        commonHeader.setOriginatorId(originatorID);
        requestContext.setAction(action);
        requestContext.setPayload(payload);
        requestContext.getActionIdentifiers().setVnfId(vnfId);
        VNFContext vnfContext = runtimeContext.getVnfContext();
        vnfContext.setType(vnfType);
        vnfContext.setId(vnfId);
        vnfContext.setVersion(vnfVersion);
        return commandExecutorInput;
    }

    private CommandExecutorInput createCommandExecutorInputWithSubObjects() {
        CommandExecutorInput commandExecutorInput = new CommandExecutorInput();
        RuntimeContext runtimeContext = new RuntimeContext();
        commandExecutorInput.setRuntimeContext(runtimeContext);
        RequestContext requestContext = new RequestContext();
        runtimeContext.setRequestContext(requestContext);
        CommonHeader commonHeader = new CommonHeader();
        requestContext.setCommonHeader(commonHeader);
        Flags flags = new Flags();
        commonHeader.setFlags(flags);
        ActionIdentifiers actionIdentifiers = new ActionIdentifiers();
        requestContext.setActionIdentifiers(actionIdentifiers);
        VNFContext vnfContext = new VNFContext();
        runtimeContext.setVnfContext(vnfContext);
        return commandExecutorInput;
    }
}