75d6d65f3e4a004875989f52a2b19616c00b5dad
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2023 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.testsuites.performance.benchmark.eventgenerator;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26
27 import jakarta.inject.Provider;
28 import jakarta.ws.rs.core.Response;
29 import org.glassfish.grizzly.http.server.Request;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.Mock;
34 import org.mockito.Mockito;
35 import org.mockito.junit.MockitoJUnitRunner;
36 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
37 import org.onap.policy.apex.testsuites.performance.benchmark.eventgenerator.events.OutputEvent;
38
39 /**
40  * Test the EventGeneratorEndpoint class.
41  *
42  */
43 @RunWith(MockitoJUnitRunner.class)
44 public class EventGeneratorEndpointTest {
45     @Mock
46     private Provider<Request> httpRequestProviderMock;
47
48     @Mock
49     private Request httpRequestMock;
50
51     /**
52      * Set up mocking of the engine service facade.
53      *
54      * @throws ApexException on engine service facade setup errors
55      */
56     @Before
57     public void initializeMocking() throws ApexException {
58
59         Mockito.doReturn(httpRequestMock).when(httpRequestProviderMock).get();
60
61         Mockito.doReturn("zooby").when(httpRequestMock).getRemoteHost();
62         Mockito.doReturn(12345).when(httpRequestMock).getRemotePort();
63
64     }
65
66     @Test
67     public void testEventGeneratorEndpointGetStats() {
68         EventGeneratorEndpoint.clearEventGenerationStats();
69         EventGeneratorEndpoint.setFinished(false);
70
71         EventGeneratorEndpoint egep = new EventGeneratorEndpoint(null);
72         assertNotNull(egep);
73
74         Response stats = egep.serviceGetStats();
75         assertEquals(200, stats.getStatus());
76     }
77
78     @Test
79     public void testEventGeneratorEndpointGetEventsZeroBatchCount() {
80         EventGeneratorParameters incomingParameters = new EventGeneratorParameters();
81         incomingParameters.setBatchCount(1);
82
83         EventGeneratorEndpoint.setParameters(incomingParameters);
84         EventGeneratorEndpoint.clearEventGenerationStats();
85         EventGeneratorEndpoint.setFinished(false);
86
87         EventGeneratorEndpoint egep = new EventGeneratorEndpoint(httpRequestProviderMock);
88         assertNotNull(egep);
89
90         Response events = egep.getEvents();
91         assertEquals(200, events.getStatus());
92
93         incomingParameters.setBatchCount(1);
94         events = egep.getEvents();
95         assertEquals(204, events.getStatus());
96     }
97
98     @Test
99     public void testEventGeneratorEndpointPostBadEvent() {
100         EventGeneratorParameters incomingParameters = new EventGeneratorParameters();
101         incomingParameters.setBatchCount(1);
102
103         EventGeneratorEndpoint.setParameters(incomingParameters);
104         EventGeneratorEndpoint.clearEventGenerationStats();
105         EventGeneratorEndpoint.setFinished(false);
106
107         EventGeneratorEndpoint egep = new EventGeneratorEndpoint(httpRequestProviderMock);
108         assertNotNull(egep);
109
110         OutputEvent oe = new OutputEvent();
111         oe.setTestSlogan("99-99: Whatever");
112
113         Response events = egep.postEventResponse(oe.asJson());
114         assertEquals(409, events.getStatus());
115     }
116 }