Add junit tests to LocalRequestValidatorImpl class
[appc.git] / appc-dispatcher / appc-request-handler / appc-request-handler-core / src / test / java / org / onap / appc / requesthandler / impl / LocalRequestValidatorImplTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.appc.requesthandler.impl;
24
25 // import com.att.eelf.configuration.EELFLogger;
26 // import com.att.eelf.configuration.EELFManager;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.mockito.Mock;
31 import org.mockito.Mockito;
32 import org.mockito.Matchers;
33 import org.mockito.runners.MockitoJUnitRunner;
34 import org.onap.appc.domainmodel.lcm.ActionIdentifiers;
35 import org.onap.appc.domainmodel.lcm.ActionLevel;
36 import org.onap.appc.domainmodel.lcm.RuntimeContext;
37 import org.onap.appc.domainmodel.lcm.VNFOperation;
38 import org.onap.appc.exceptions.InvalidInputException;
39 import org.onap.appc.requesthandler.LCMStateManager;
40 import org.onap.appc.requesthandler.exceptions.DuplicateRequestException;
41 import org.onap.appc.requesthandler.exceptions.LCMOperationsDisabledException;
42 import org.onap.appc.transactionrecorder.TransactionRecorder;
43
44 import static org.powermock.api.mockito.PowerMockito.spy;
45
46 /**
47  * Test class for LocalRequestValidatorImpl
48  */
49 @RunWith(MockitoJUnitRunner.class)
50 public class LocalRequestValidatorImplTest implements LocalRequestHanlderTestHelper {
51
52     // protected final EELFLogger logger = EELFManager.getInstance().getLogger(LocalRequestValidatorImplTest.class);
53
54     @Mock
55     private LCMStateManager lcmStateManager;
56
57     @Mock
58     private TransactionRecorder transactionRecorder;
59
60     LocalRequestValidatorImpl requestValidator;
61
62     @Before
63     public void setUp() throws Exception {
64         requestValidator = spy(new LocalRequestValidatorImpl());
65         requestValidator.setLcmStateManager(lcmStateManager);
66         requestValidator.setTransactionRecorder(transactionRecorder);
67
68         Mockito.when(lcmStateManager.isLCMOperationEnabled()).thenReturn(true);
69         Mockito.when(transactionRecorder.isTransactionDuplicate(Matchers.anyObject())).thenReturn(false);
70     }
71
72     @Test(expected = LCMOperationsDisabledException.class)
73     public void validateRequestLCMDisabled() throws Exception {
74         Mockito.when(lcmStateManager.isLCMOperationEnabled()).thenReturn(false);
75         requestValidator.validateRequest(createRequestValidatorInput());
76     }
77
78     @Test(expected = DuplicateRequestException.class)
79     public void validateRequestDuplicateReqFailure() throws Exception {
80         Mockito.when(transactionRecorder.isTransactionDuplicate(Matchers.anyObject())).thenReturn(true);
81         requestValidator.validateRequest(createRequestValidatorInput());
82     }
83
84     @Test
85     public void validateRequestSuccess() throws Exception {
86         requestValidator.validateRequest(createRequestValidatorInput());
87     }
88
89     @Test(expected = InvalidInputException.class)
90     public void validateRequestPayloadFail() throws Exception {
91         String incorrectPayload = "{\"RequestId\":\"requestToCheck\"}";
92         RuntimeContext context = createRequestValidatorInput();
93         context.getRequestContext().setPayload(incorrectPayload);
94         requestValidator.validateRequest(context);
95     }
96
97     @Test(expected = InvalidInputException.class)
98     public void validateRequestActionIdentifiersNullVnfIdFail() throws Exception {
99         RuntimeContext context = createRequestValidatorInput();
100         ActionIdentifiers ai = context.getRequestContext().getActionIdentifiers();
101         ai.setVnfId(null);
102         // logger.warn("Set vnfId in ActionIdentifiers extracted from createRequestValidatorInput's RequestContext to null");
103         requestValidator.validateRequest(context);
104     }
105
106     @Test
107     public void validateRequestUnsupportedAction() throws Exception {
108         RuntimeContext context = createRequestValidatorInput();
109         context.getRequestContext().setAction(VNFOperation.AttachVolume);
110         requestValidator.validateRequest(context);
111     }
112
113     private RuntimeContext createRequestValidatorInput() {
114         return createRequestHandlerRuntimeContext("VSCP", "{\"request-id\":\"request-id\"}");
115     }
116 }