2add69857bfc4b1932b5fb43bd42d5da76a639e9
[policy/apex-pdp.git] / testsuites / performance / performance-benchmark-test / src / test / java / org / onap / policy / apex / testsuites / performance / benchmark / eventgenerator / EventGeneratorEndpointTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
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.testsuites.performance.benchmark.eventgenerator;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25
26 import javax.inject.Provider;
27 import javax.ws.rs.core.Response;
28
29 import org.glassfish.grizzly.http.server.Request;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.mockito.Mock;
33 import org.mockito.Mockito;
34 import org.mockito.MockitoAnnotations;
35 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
36 import org.onap.policy.apex.testsuites.performance.benchmark.eventgenerator.events.OutputEvent;
37
38 /**
39  * Test the EventGeneratorEndpoint class.
40  *
41  */
42 public class EventGeneratorEndpointTest {
43     @Mock
44     private Provider<Request> httpRequestProviderMock;
45
46     @Mock
47     private Request httpRequestMock;
48
49     /**
50      * Set up mocking of the engine service facade.
51      *
52      * @throws ApexException on engine service facade setup errors
53      */
54     @Before
55     public void initializeMocking() throws ApexException {
56         MockitoAnnotations.initMocks(this);
57
58         Mockito.doReturn(httpRequestMock).when(httpRequestProviderMock).get();
59
60         Mockito.doReturn("zooby").when(httpRequestMock).getRemoteHost();
61         Mockito.doReturn(12345).when(httpRequestMock).getRemotePort();
62
63     }
64
65     @Test
66     public void testEventGeneratorEndpointGetStats() {
67         EventGeneratorEndpoint.clearEventGenerationStats();
68         EventGeneratorEndpoint.setFinished(false);
69
70         EventGeneratorEndpoint egep = new EventGeneratorEndpoint(null);
71         assertNotNull(egep);
72
73         Response stats = egep.serviceGetStats();
74         assertEquals(200, stats.getStatus());
75     }
76
77     @Test
78     public void testEventGeneratorEndpointGetEventsZeroBatchCount() {
79         EventGeneratorParameters incomingParameters = new EventGeneratorParameters();
80         incomingParameters.setBatchCount(1);
81
82         EventGeneratorEndpoint.setParameters(incomingParameters);
83         EventGeneratorEndpoint.clearEventGenerationStats();
84         EventGeneratorEndpoint.setFinished(false);
85
86         EventGeneratorEndpoint egep = new EventGeneratorEndpoint(httpRequestProviderMock);
87         assertNotNull(egep);
88
89         Response events = egep.getEvents();
90         assertEquals(200, events.getStatus());
91
92         incomingParameters.setBatchCount(1);
93         events = egep.getEvents();
94         assertEquals(204, events.getStatus());
95     }
96
97     @Test
98     public void testEventGeneratorEndpointPostBadEvent() {
99         EventGeneratorParameters incomingParameters = new EventGeneratorParameters();
100         incomingParameters.setBatchCount(1);
101
102         EventGeneratorEndpoint.setParameters(incomingParameters);
103         EventGeneratorEndpoint.clearEventGenerationStats();
104         EventGeneratorEndpoint.setFinished(false);
105
106         EventGeneratorEndpoint egep = new EventGeneratorEndpoint(httpRequestProviderMock);
107         assertNotNull(egep);
108
109         OutputEvent oe = new OutputEvent();
110         oe.setTestSlogan("99-99: Whatever");
111
112         Response events = egep.postEventResponse(oe.asJson());
113         assertEquals(409, events.getStatus());
114     }
115 }