1ac1cdd7c0d269d2218994481b8cb817de0ea94d
[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  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.producer;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatCode;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26
27 import java.io.ByteArrayOutputStream;
28 import java.io.PrintStream;
29 import java.util.Random;
30 import org.apache.commons.lang3.RandomStringUtils;
31 import org.junit.After;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.onap.policy.apex.service.engine.event.ApexEventException;
35 import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException;
36 import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.FileCarrierTechnologyParameters;
37 import org.onap.policy.apex.service.engine.parameters.dummyclasses.SuperDooperCarrierTechnologyParameters;
38 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters;
39
40 public class ApexFileEventProducerTest {
41     private final PrintStream out = System.out;
42     private final Random random = new Random();
43     private ApexFileEventProducer eventProducer;
44     private EventHandlerParameters parameters;
45     private FileCarrierTechnologyParameters technologyParameters;
46
47     /**
48      * Prepare for testing.
49      */
50     @Before
51     public void setUp() {
52         eventProducer = new ApexFileEventProducer();
53         parameters = new EventHandlerParameters();
54         technologyParameters = new FileCarrierTechnologyParameters();
55     }
56
57     @After
58     public void tearDown() {
59         System.setOut(out);
60     }
61
62     @Test
63     public void initNullParams() {
64         final String name = RandomStringUtils.randomAlphabetic(5);
65         assertThatThrownBy(() -> eventProducer.init(name, null))
66             .isInstanceOf(ApexEventException.class);
67     }
68
69     @Test
70     public void initParamsInvalidCarrier() {
71         final String name = RandomStringUtils.randomAlphabetic(5);
72         parameters.setCarrierTechnologyParameters(new SuperDooperCarrierTechnologyParameters());
73         assertThatThrownBy(() -> eventProducer.init(name, parameters))
74             .isInstanceOf(ApexEventException.class);
75     }
76
77     @Test
78     public void initParamsWithStandardError() {
79         final String name = RandomStringUtils.randomAlphabetic(5);
80         technologyParameters.setStandardError(true);
81
82         parameters.setCarrierTechnologyParameters(technologyParameters);
83
84         assertThatCode(() -> eventProducer.init(name, parameters))
85             .doesNotThrowAnyException();
86     }
87
88     @Test
89     public void initParamsWithStandardIo() {
90         final String name = RandomStringUtils.randomAlphabetic(5);
91         technologyParameters.setStandardIo(true);
92
93         parameters.setCarrierTechnologyParameters(technologyParameters);
94         assertThatCode(() -> eventProducer.init(name, parameters))
95             .doesNotThrowAnyException();
96     }
97
98     @Test
99     public void initParamsWithFileIsDirectory() {
100         final String name = RandomStringUtils.randomAlphabetic(5);
101         final String fileName = System.getProperty("user.dir");
102
103         technologyParameters.setFileName(fileName);
104
105         parameters.setCarrierTechnologyParameters(technologyParameters);
106         assertThatThrownBy(() -> eventProducer.init(name, parameters))
107             .isInstanceOf(ApexEventException.class);
108     }
109
110     @Test
111     public void initParamsWithDelay() {
112         final String name = RandomStringUtils.randomAlphabetic(5);
113         technologyParameters.setStandardIo(true);
114         parameters.setCarrierTechnologyParameters(technologyParameters);
115
116         assertThatCode(() -> eventProducer.init(name, parameters))
117             .doesNotThrowAnyException();
118     }
119
120     @Test
121     public void sendEventWrongEvent() throws ApexEventException {
122         final long executionId = random.nextLong();
123         final String eventName = RandomStringUtils.randomAlphanumeric(4);
124         final String producerName = RandomStringUtils.randomAlphanumeric(5);
125         final Object event = new Object();
126
127         technologyParameters.setStandardIo(true);
128         final EventHandlerParameters parameters = new EventHandlerParameters();
129         parameters.setCarrierTechnologyParameters(technologyParameters);
130
131         ApexFileEventProducer apexFileEventProducer = new ApexFileEventProducer();
132         apexFileEventProducer.init(producerName, parameters);
133         assertThatThrownBy(() -> apexFileEventProducer.sendEvent(executionId, null, eventName, event))
134             .isInstanceOf(ApexEventRuntimeException.class);
135     }
136
137     @Test
138     public void sendEvent() throws ApexEventException {
139         // Prepare output stream to read data
140         final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
141         System.setOut(new PrintStream(outContent));
142
143         // Prepare producer
144         final String producerName = RandomStringUtils.randomAlphanumeric(5);
145         technologyParameters.setStandardIo(true);
146         final EventHandlerParameters parameters = new EventHandlerParameters();
147         parameters.setCarrierTechnologyParameters(technologyParameters);
148         eventProducer.init(producerName, parameters);
149
150         // Prepare for sending
151         final long executionId = random.nextLong();
152         final String eventName = RandomStringUtils.randomAlphanumeric(4);
153         final String event = RandomStringUtils.randomAlphabetic(6);
154         eventProducer.sendEvent(executionId, null, eventName, event);
155
156         // Check results
157         assertThat(outContent.toString()).contains(event);
158     }
159
160 }