91e7056249876129e993fb8cfc1f85ebec4531a7
[policy/apex-pdp.git] / services / services-engine / src / test / java / org / onap / policy / apex / service / engine / event / impl / filecarrierplugin / consumer / ApexFileEventConsumerTest.java
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.consumer;
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.File;
28 import org.apache.commons.lang3.RandomStringUtils;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.onap.policy.apex.service.engine.event.ApexEventException;
32 import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.FileCarrierTechnologyParameters;
33 import org.onap.policy.apex.service.engine.parameters.dummyclasses.SuperDooperCarrierTechnologyParameters;
34 import org.onap.policy.apex.service.engine.parameters.dummyclasses.SuperTokenDelimitedEventProtocolParameters;
35 import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters;
36 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters;
37 import org.onap.policy.apex.service.parameters.eventprotocol.EventProtocolTextTokenDelimitedParameters;
38
39 public class ApexFileEventConsumerTest {
40     private ApexFileEventConsumer consumer;
41     private EventHandlerParameters handlerParameters;
42     private File tempFile;
43
44     /**
45      * Prepare tests.
46      *
47      * @throws Exception while file cannot be created
48      */
49     @Before
50     public void setUp() throws Exception {
51         consumer = new ApexFileEventConsumer();
52         handlerParameters = new EventHandlerParameters();
53         tempFile = File.createTempFile("afec", ".tmp");
54         tempFile.deleteOnExit();
55     }
56
57     @Test
58     public void initNoConsumerParameters() {
59         final String name = RandomStringUtils.randomAlphanumeric(4);
60         assertThatThrownBy(() -> consumer.init(name, null, null))
61             .isInstanceOf(ApexEventException.class);
62     }
63
64     @Test
65     public void initWrongCarrier() {
66         final String name = RandomStringUtils.randomAlphanumeric(4);
67         final CarrierTechnologyParameters technologyParameters = new SuperDooperCarrierTechnologyParameters();
68         handlerParameters.setCarrierTechnologyParameters(technologyParameters);
69
70         assertThatThrownBy(() -> consumer.init(name, handlerParameters, null))
71             .isInstanceOf(ApexEventException.class);
72     }
73
74     @Test
75     public void initWithoutReceiver() {
76         final String name = RandomStringUtils.randomAlphanumeric(4);
77         final EventHandlerParameters handlerParameters = new EventHandlerParameters();
78         final FileCarrierTechnologyParameters technologyParameters = new FileCarrierTechnologyParameters();
79         technologyParameters.setFileName(tempFile.getAbsolutePath());
80         final EventProtocolTextTokenDelimitedParameters params = new SuperTokenDelimitedEventProtocolParameters();
81
82         handlerParameters.setCarrierTechnologyParameters(technologyParameters);
83         handlerParameters.setEventProtocolParameters(params);
84
85         assertThatCode(() -> consumer.init(name, handlerParameters, null))
86             .doesNotThrowAnyException();
87     }
88
89     @Test
90     public void getName() throws ApexEventException {
91         final String name = RandomStringUtils.randomAlphabetic(5);
92         final EventHandlerParameters handlerParameters = new EventHandlerParameters();
93         final FileCarrierTechnologyParameters technologyParameters = new FileCarrierTechnologyParameters();
94         technologyParameters.setFileName(tempFile.getAbsolutePath());
95         final EventProtocolTextTokenDelimitedParameters params = new SuperTokenDelimitedEventProtocolParameters();
96
97         handlerParameters.setCarrierTechnologyParameters(technologyParameters);
98         handlerParameters.setEventProtocolParameters(params);
99
100         consumer.init(name, handlerParameters, null);
101         assertThat(consumer.getName()).isEqualTo(name);
102     }
103 }