562760f697bf73be51263c29cb36db45f4f23678
[policy/apex-pdp.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021  Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.onap.policy.apex.service.engine.event.impl.enevent;
21
22 import static org.assertj.core.api.Assertions.assertThatThrownBy;
23 import static org.junit.Assert.assertArrayEquals;
24
25 import java.util.List;
26 import java.util.Random;
27 import org.apache.commons.lang3.RandomStringUtils;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.onap.policy.apex.core.engine.event.EnEvent;
31 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
32 import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
33 import org.onap.policy.apex.service.engine.event.ApexEvent;
34 import org.onap.policy.apex.service.engine.event.ApexEventException;
35 import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException;
36
37 public class ApexEvent2EnEventConverterTest {
38     private ApexEvent2EnEventConverter converter;
39     private final Random random = new Random();
40
41     @Before
42     public void setUp() throws Exception {
43         converter = new ApexEvent2EnEventConverter(null);
44     }
45
46     @Test
47     public void toApexEventNull() {
48         final String eventName = RandomStringUtils.randomAlphabetic(3);
49         assertThatThrownBy(() -> converter.toApexEvent(eventName, null))
50             .isInstanceOf(ApexEventException.class);
51     }
52
53     @Test
54     public void toApexEventWrongClass() throws ApexException {
55         final String eventName = RandomStringUtils.randomAlphabetic(3);
56         final String name = RandomStringUtils.randomAlphanumeric(5);
57         final String version = RandomStringUtils.randomAlphanumeric(6);
58         final String nameSpace = "a" + RandomStringUtils.randomAlphanumeric(7);
59         final String source = RandomStringUtils.randomAlphanumeric(8);
60         final String target = RandomStringUtils.randomAlphanumeric(9);
61
62         final ApexEvent event = new ApexEvent(name, version, nameSpace, source, target);
63
64         assertThatThrownBy(() -> converter.toApexEvent(eventName, event))
65             .isInstanceOf(ApexEventRuntimeException.class);
66     }
67
68     @Test
69     public void toApex() throws ApexException {
70         // prepare String values for events
71         final String name = RandomStringUtils.randomAlphabetic(5);
72         final String version = RandomStringUtils.randomAlphabetic(6);
73         final String nameSpace = "b" + RandomStringUtils.randomAlphabetic(7);
74         final String source = RandomStringUtils.randomAlphabetic(8);
75         final String target = RandomStringUtils.randomAlphabetic(9);
76         final int executionId = random.nextInt(1000);
77         final String exceptionMessage = RandomStringUtils.randomAlphabetic(11);
78
79         // prepare events
80         final AxEvent axEvent = new AxEvent();
81         axEvent.getKey().setName(name);
82         axEvent.getKey().setVersion(version);
83         axEvent.setNameSpace(nameSpace);
84         axEvent.setSource(source);
85         axEvent.setTarget(target);
86         final EnEvent enEvent = new EnEvent(axEvent);
87         enEvent.setExecutionId(executionId);
88         enEvent.setExceptionMessage(exceptionMessage);
89
90         // prepare expected event
91         final ApexEvent apexEvent = new ApexEvent(name, version, nameSpace, source, target);
92         apexEvent.setExecutionId(executionId);
93         apexEvent.setExceptionMessage(exceptionMessage);
94         final Object[] expected = {apexEvent};
95
96         // Test
97         final List<ApexEvent> actual = converter.toApexEvent(RandomStringUtils.randomAlphabetic(3), enEvent);
98         assertArrayEquals(expected, actual.toArray());
99     }
100 }