Changed to unmaintained
[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
26 import static org.junit.Assert.fail;
27 import static org.powermock.api.mockito.PowerMockito.spy;
28
29 import com.att.eelf.configuration.EELFLogger;
30 import com.att.eelf.configuration.EELFLogger.Level;
31 import com.att.eelf.configuration.EELFManager;
32
33 import java.util.Date;
34
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.mockito.Matchers;
39 import org.mockito.Mock;
40 import org.mockito.Mockito;
41 import org.onap.appc.domainmodel.lcm.ActionIdentifiers;
42 import org.onap.appc.domainmodel.lcm.RuntimeContext;
43 import org.onap.appc.domainmodel.lcm.VNFOperation;
44 import org.onap.appc.exceptions.InvalidInputException;
45 import org.onap.appc.requesthandler.LCMStateManager;
46 import org.onap.appc.requesthandler.exceptions.DuplicateRequestException;
47 import org.onap.appc.requesthandler.exceptions.LCMOperationsDisabledException;
48 import org.onap.appc.requesthandler.exceptions.RequestExpiredException;
49 import org.onap.appc.transactionrecorder.TransactionRecorder;
50 import org.powermock.modules.junit4.PowerMockRunner;
51 import org.powermock.reflect.Whitebox;
52
53 /**
54  * Test class for LocalRequestValidatorImpl
55  */
56
57 @RunWith(PowerMockRunner.class)
58 public class LocalRequestValidatorImplTest implements LocalRequestHanlderTestHelper {
59
60     @Mock
61     private LCMStateManager lcmStateManager;
62
63     @Mock
64     private TransactionRecorder transactionRecorder;
65
66     LocalRequestValidatorImpl requestValidator;
67
68     @Before
69     public void setUp() throws Exception {
70         requestValidator = spy(new LocalRequestValidatorImpl());
71         requestValidator.setLcmStateManager(lcmStateManager);
72         requestValidator.setTransactionRecorder(transactionRecorder);
73
74         Mockito.when(lcmStateManager.isLCMOperationEnabled()).thenReturn(true);
75         Mockito.when(transactionRecorder.isTransactionDuplicate(Matchers.anyObject())).thenReturn(false);
76         final EELFLogger logger = EELFManager.getInstance().getLogger(LocalRequestValidatorImpl.class);
77         logger.setLevel(Level.TRACE);
78         Whitebox.setInternalState(requestValidator, "logger", logger);
79     }
80
81     @Test(expected = LCMOperationsDisabledException.class)
82     public void validateRequestLCMDisabled() throws Exception {
83         Mockito.when(lcmStateManager.isLCMOperationEnabled()).thenReturn(false);
84         requestValidator.validateRequest(createRequestValidatorInput());
85     }
86
87     @Test(expected = DuplicateRequestException.class)
88     public void validateRequestDuplicateReqFailure() throws Exception {
89         Mockito.when(transactionRecorder.isTransactionDuplicate(Matchers.anyObject())).thenReturn(true);
90         requestValidator.validateRequest(createRequestValidatorInput());
91     }
92
93     @Test
94     public void validateRequestSuccess() throws Exception {
95         requestValidator.validateRequest(createRequestValidatorInput());
96     }
97
98     @Test(expected = InvalidInputException.class)
99     public void validateRequestPayloadFail() throws Exception {
100         String incorrectPayload = "{\"RequestId\":\"requestToCheck\"}";
101         RuntimeContext context = createRequestValidatorInput();
102         context.getRequestContext().setPayload(incorrectPayload);
103         requestValidator.validateRequest(context);
104     }
105
106     @Test(expected = InvalidInputException.class)
107     public void validateRequestActionIdentifiersNullVnfIdFail() throws Exception {
108         RuntimeContext context = createRequestValidatorInput();
109         ActionIdentifiers ai = context.getRequestContext().getActionIdentifiers();
110         ai.setVnfId(null);
111         requestValidator.validateRequest(context);
112     }
113
114     @Test
115     public void validateRequestUnsupportedAction() throws Exception {
116         RuntimeContext context = createRequestValidatorInput();
117         context.getRequestContext().setAction(VNFOperation.AttachVolume);
118         requestValidator.validateRequest(context);
119     }
120
121     @Test(expected = InvalidInputException.class)
122     public void validateRequestInvalidInputInvalidTime() throws Exception {
123         RuntimeContext context = createRequestValidatorInput();
124         context.getResponseContext().getCommonHeader().setTimestamp(new Date(System.currentTimeMillis() + 999999));
125         final EELFLogger logger = EELFManager.getInstance().getLogger(AbstractRequestValidatorImpl.class);
126         logger.setLevel(Level.TRACE);
127         Whitebox.setInternalState(requestValidator, "logger", logger);
128         requestValidator.validateRequest(context);
129     }
130
131     @Test(expected = RequestExpiredException.class)
132     public void validateRequestRequestExpiredException() throws Exception {
133         RuntimeContext context = createRequestValidatorInput();
134         context.getResponseContext().getCommonHeader().setTimestamp(new Date(System.currentTimeMillis() - 999999));
135         final EELFLogger logger = EELFManager.getInstance().getLogger(AbstractRequestValidatorImpl.class);
136         logger.setLevel(Level.TRACE);
137         Whitebox.setInternalState(requestValidator, "logger", logger);
138         requestValidator.validateRequest(context);
139     }
140
141     private RuntimeContext createRequestValidatorInput() {
142         return createRequestHandlerRuntimeContext("VSCP", "{\"request-id\":\"request-id\"}");
143     }
144 }