2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.onap.appc.requesthandler.impl;
26 import static org.junit.Assert.fail;
27 import static org.powermock.api.mockito.PowerMockito.spy;
29 import com.att.eelf.configuration.EELFLogger;
30 import com.att.eelf.configuration.EELFLogger.Level;
31 import com.att.eelf.configuration.EELFManager;
33 import java.util.Date;
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;
54 * Test class for LocalRequestValidatorImpl
57 @RunWith(PowerMockRunner.class)
58 public class LocalRequestValidatorImplTest implements LocalRequestHanlderTestHelper {
61 private LCMStateManager lcmStateManager;
64 private TransactionRecorder transactionRecorder;
66 LocalRequestValidatorImpl requestValidator;
69 public void setUp() throws Exception {
70 requestValidator = spy(new LocalRequestValidatorImpl());
71 requestValidator.setLcmStateManager(lcmStateManager);
72 requestValidator.setTransactionRecorder(transactionRecorder);
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);
81 @Test(expected = LCMOperationsDisabledException.class)
82 public void validateRequestLCMDisabled() throws Exception {
83 Mockito.when(lcmStateManager.isLCMOperationEnabled()).thenReturn(false);
84 requestValidator.validateRequest(createRequestValidatorInput());
87 @Test(expected = DuplicateRequestException.class)
88 public void validateRequestDuplicateReqFailure() throws Exception {
89 Mockito.when(transactionRecorder.isTransactionDuplicate(Matchers.anyObject())).thenReturn(true);
90 requestValidator.validateRequest(createRequestValidatorInput());
94 public void validateRequestSuccess() throws Exception {
95 requestValidator.validateRequest(createRequestValidatorInput());
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);
106 @Test(expected = InvalidInputException.class)
107 public void validateRequestActionIdentifiersNullVnfIdFail() throws Exception {
108 RuntimeContext context = createRequestValidatorInput();
109 ActionIdentifiers ai = context.getRequestContext().getActionIdentifiers();
111 requestValidator.validateRequest(context);
115 public void validateRequestUnsupportedAction() throws Exception {
116 RuntimeContext context = createRequestValidatorInput();
117 context.getRequestContext().setAction(VNFOperation.AttachVolume);
118 requestValidator.validateRequest(context);
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);
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);
141 private RuntimeContext createRequestValidatorInput() {
142 return createRequestHandlerRuntimeContext("VSCP", "{\"request-id\":\"request-id\"}");