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