2 * =============LICENSE_START======================================================
3 * Copyright (c) 2020 ChinaMobile. All rights reserved.
4 * Copyright (c) 2022 Wipro Limited Intellectual Property. All rights reserved.
5 * ================================================================================
6 * Copyright (c) 2019 IBM
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.dcae.analytics.tca.web.integration;
24 import java.util.ArrayList;
25 import java.util.List;
27 import static org.assertj.core.api.Assertions.assertThat;
29 import org.junit.jupiter.api.Test;
30 import org.mockito.Mockito;
31 import org.onap.dcae.analytics.model.AnalyticsHttpConstants;
32 import org.onap.dcae.analytics.tca.core.service.GenericTcaExecutionContext;
33 import org.onap.dcae.analytics.tca.core.service.GenericTcaExecutionContext.GenericTcaExecutionContextBuilder;
34 import org.onap.dcae.analytics.tca.core.service.GenericTcaProcessingContext;
35 import org.onap.dcae.analytics.tca.core.service.GenericTcaResultContext;
36 import org.onap.dcae.analytics.tca.core.service.TcaAbatementContext;
37 import org.onap.dcae.analytics.tca.core.service.TcaExecutionContext;
38 import org.onap.dcae.analytics.tca.core.service.TcaProcessingContext;
39 import org.onap.dcae.analytics.tca.core.service.TcaResultContext;
40 import org.onap.dcae.analytics.tca.model.policy.TcaPolicy;
41 import org.onap.dcae.analytics.tca.model.util.json.TcaModelJsonConversion;
42 import org.onap.dcae.analytics.tca.web.BaseTcaWebTest;
43 import org.onap.dcae.analytics.tca.web.TcaAppProperties;
44 import org.onap.dcae.analytics.tca.web.domain.TestTcaAaiEnrichmentContext;
45 import org.onap.dcae.analytics.tca.web.domain.TestTcaAbatementContext;
46 import org.onap.dcae.analytics.test.BaseAnalyticsSpringBootIT;
47 import org.onap.dcae.analytics.web.util.AnalyticsHttpUtils;
48 import org.springframework.beans.factory.annotation.Autowired;
49 import org.springframework.core.env.Environment;
50 import org.springframework.messaging.Message;
51 import org.springframework.messaging.MessageHeaders;
53 public class TcaAlertTransformerTest extends BaseAnalyticsSpringBootIT {
56 Environment environment;
58 protected static final String TEST_POLICY_JSON_STRING;
59 protected static final String TEST_REQUEST_ID = "testRequestId";
60 protected static final List<TcaPolicy> TEST_TCA_POLICY;
64 TEST_POLICY_JSON_STRING = fromStream(TestFileLocation.TCA_POLICY_JSON);
65 TEST_TCA_POLICY = TcaModelJsonConversion.TCA_POLICY_JSON_FUNCTION.apply(TEST_POLICY_JSON_STRING)
66 .orElseThrow(() -> new IllegalStateException("Unable to get Test TcaPolicy"));
70 void TestTcaAppPropertiesValidator() throws Exception {
71 TcaAppProperties properties = new TcaAppProperties(environment);
73 final Message message = Mockito.mock(Message.class);
74 MessageHeaders header = Mockito.mock(MessageHeaders.class);
77 final TcaExecutionContext testExecutionContext =
78 getTestExecutionContext(BaseTcaWebTest.fromStream("data/json/cef/cef_message.json"));
80 List<TcaExecutionContext> messagePayload = new ArrayList<TcaExecutionContext>();
81 messagePayload.add(testExecutionContext);
83 Mockito.when(message.getPayload()).thenReturn(messagePayload);
84 Mockito.when(message.getHeaders()).thenReturn(header);
85 Mockito.when(AnalyticsHttpUtils.getRequestId(header)).thenReturn("resuestId-1");
86 Mockito.when(AnalyticsHttpUtils.getTransactionId(header)).thenReturn("transactionId-1");
87 Mockito.when(header.get(AnalyticsHttpConstants.REQUEST_BEGIN_TS_HEADER_KEY)).thenReturn(null);
89 TcaAlertTransformer tcaAlertTransformer = new TcaAlertTransformer(properties);
90 tcaAlertTransformer.doTransform(message);
91 assertThat(message).isNotNull();
92 assertThat(tcaAlertTransformer.doTransform(message)).getClass().getName().startsWith("TcaAlert");
96 protected TcaExecutionContext getTestExecutionContext(final String cefMessage) {
97 final TestTcaAbatementContext testTcaAbatementContext = new TestTcaAbatementContext();
98 return getTestExecutionContextBuilder(cefMessage, TEST_TCA_POLICY, testTcaAbatementContext).build();
101 protected GenericTcaExecutionContextBuilder getTestExecutionContextBuilder(
102 final String cefMessage, final List<TcaPolicy> tcaPolicy, final TcaAbatementContext tcaAbatementContext) {
104 final TcaProcessingContext tcaProcessingContext = new GenericTcaProcessingContext();
105 final TcaResultContext tcaResultContext = new GenericTcaResultContext();
106 final TestTcaAaiEnrichmentContext testTcaAaiEnrichmentContext = new TestTcaAaiEnrichmentContext();
108 return GenericTcaExecutionContext.builder()
109 .requestId(TEST_REQUEST_ID)
110 .cefMessage(cefMessage)
111 .tcaPolicy(tcaPolicy)
112 .tcaProcessingContext(tcaProcessingContext)
113 .tcaResultContext(tcaResultContext)
114 .tcaAbatementContext(tcaAbatementContext)
115 .tcaAaiEnrichmentContext(testTcaAaiEnrichmentContext);