summaryrefslogtreecommitdiffstats
path: root/dcae-analytics/dcae-analytics-tca-web/src/test/java/org/onap/dcae/analytics/tca/web/integration/TcaAlertTransformerTest.java
blob: 8d1b45ceaafa416c80f34abf8a783d689e162c8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
 * ================================================================================
 * Copyright (c) 2020 ChinaMobile. All rights reserved.
 * ================================================================================
 * Copyright Copyright (c) 2019 IBM
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 *
 */
package org.onap.dcae.analytics.tca.web.integration;

import java.util.ArrayList;
import java.util.List;

import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.onap.dcae.analytics.model.AnalyticsHttpConstants;
import org.onap.dcae.analytics.tca.core.service.GenericTcaExecutionContext;
import org.onap.dcae.analytics.tca.core.service.GenericTcaExecutionContext.GenericTcaExecutionContextBuilder;
import org.onap.dcae.analytics.tca.core.service.GenericTcaProcessingContext;
import org.onap.dcae.analytics.tca.core.service.GenericTcaResultContext;
import org.onap.dcae.analytics.tca.core.service.TcaAbatementContext;
import org.onap.dcae.analytics.tca.core.service.TcaExecutionContext;
import org.onap.dcae.analytics.tca.core.service.TcaProcessingContext;
import org.onap.dcae.analytics.tca.core.service.TcaResultContext;
import org.onap.dcae.analytics.tca.model.policy.TcaPolicy;
import org.onap.dcae.analytics.tca.model.util.json.TcaModelJsonConversion;
import org.onap.dcae.analytics.tca.web.BaseTcaWebTest;
import org.onap.dcae.analytics.tca.web.TcaAppProperties;
import org.onap.dcae.analytics.tca.web.domain.TestTcaAaiEnrichmentContext;
import org.onap.dcae.analytics.tca.web.domain.TestTcaAbatementContext;
import org.onap.dcae.analytics.test.BaseAnalyticsSpringBootIT;
import org.onap.dcae.analytics.web.util.AnalyticsHttpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;

public class TcaAlertTransformerTest extends BaseAnalyticsSpringBootIT {

    @Autowired
    Environment environment;

    protected static final String TEST_POLICY_JSON_STRING;
    protected static final String TEST_REQUEST_ID = "testRequestId";
    protected static final TcaPolicy TEST_TCA_POLICY;

    static {

        TEST_POLICY_JSON_STRING = fromStream(TestFileLocation.TCA_POLICY_JSON);
        TEST_TCA_POLICY = TcaModelJsonConversion.TCA_POLICY_JSON_FUNCTION.apply(TEST_POLICY_JSON_STRING)
                .orElseThrow(() -> new IllegalStateException("Unable to get Test TcaPolicy"));
    }
  
    @Test
    void TestTcaAppPropertiesValidator() throws Exception {
        TcaAppProperties properties = new TcaAppProperties(environment);

        final Message message = Mockito.mock(Message.class);
        MessageHeaders header = Mockito.mock(MessageHeaders.class);


        final TcaExecutionContext testExecutionContext =
                  getTestExecutionContext(BaseTcaWebTest.fromStream("data/json/cef/cef_message.json"));

        List<TcaExecutionContext> messagePayload = new ArrayList<TcaExecutionContext>();
        messagePayload.add(testExecutionContext);

        Mockito.when(message.getPayload()).thenReturn(messagePayload);
        Mockito.when(message.getHeaders()).thenReturn(header);
        Mockito.when(AnalyticsHttpUtils.getRequestId(header)).thenReturn("resuestId-1");
        Mockito.when(AnalyticsHttpUtils.getTransactionId(header)).thenReturn("transactionId-1");
        Mockito.when(header.get(AnalyticsHttpConstants.REQUEST_BEGIN_TS_HEADER_KEY)).thenReturn(null);

        TcaAlertTransformer tcaAlertTransformer = new TcaAlertTransformer(properties);
        tcaAlertTransformer.doTransform(message);
        
    }

    protected TcaExecutionContext getTestExecutionContext(final String cefMessage) {
        final TestTcaAbatementContext testTcaAbatementContext = new TestTcaAbatementContext();
        return getTestExecutionContextBuilder(cefMessage, TEST_TCA_POLICY, testTcaAbatementContext).build();
    }

    protected GenericTcaExecutionContextBuilder getTestExecutionContextBuilder(
            final String cefMessage, final TcaPolicy tcaPolicy, final TcaAbatementContext tcaAbatementContext) {

        final TcaProcessingContext tcaProcessingContext = new GenericTcaProcessingContext();
        final TcaResultContext tcaResultContext = new GenericTcaResultContext();
        final TestTcaAaiEnrichmentContext testTcaAaiEnrichmentContext = new TestTcaAaiEnrichmentContext();

        return GenericTcaExecutionContext.builder()
                .requestId(TEST_REQUEST_ID)
                .cefMessage(cefMessage)
                .tcaPolicy(tcaPolicy)
                .tcaProcessingContext(tcaProcessingContext)
                .tcaResultContext(tcaResultContext)
                .tcaAbatementContext(tcaAbatementContext)
                .tcaAaiEnrichmentContext(testTcaAaiEnrichmentContext);
    }

}