ICS sample producer and consumer
[nonrtric.git] / sample-services / ics-producer-consumer / producer / src / test / java / com / demo / producer / ThreadsControllerTest.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  *
5  * Copyright (C) 2024: OpenInfra Foundation Europe
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ========================LICENSE_END===================================
19  */
20
21 package com.demo.producer;
22
23 import com.demo.producer.controllers.ThreadsController;
24 import com.demo.producer.producer.SimpleProducer;
25
26 import org.junit.jupiter.api.AfterEach;
27 import org.junit.jupiter.api.BeforeEach;
28 import org.junit.jupiter.api.Test;
29 import org.mockito.InjectMocks;
30 import org.mockito.Mock;
31 import org.mockito.MockitoAnnotations;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.boot.test.context.SpringBootTest;
34 import org.springframework.mock.web.MockHttpServletRequest;
35 import org.springframework.mock.web.MockHttpServletResponse;
36 import org.springframework.test.context.ContextConfiguration;
37 import org.springframework.web.context.request.RequestContextHolder;
38 import org.springframework.web.context.request.ServletRequestAttributes;
39 import java.util.concurrent.CompletableFuture;
40
41 import static org.junit.jupiter.api.Assertions.assertEquals;
42 import static org.mockito.Mockito.*;
43
44 @SpringBootTest
45 @ContextConfiguration(classes = SimpleProducer.class)
46 public class ThreadsControllerTest {
47
48     @Mock
49     @Autowired
50     private SimpleProducer simpleProducer;
51
52     @InjectMocks
53     private ThreadsController threadsController;
54
55     private MockHttpServletRequest request;
56     private MockHttpServletResponse response;
57
58     private AutoCloseable closable;
59
60     @BeforeEach
61     public void setUp() {
62         closable = MockitoAnnotations.openMocks(this);
63         request = new MockHttpServletRequest();
64         response = new MockHttpServletResponse();
65         RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request, response));
66     }
67
68     @AfterEach
69     public void close() throws Exception {
70         closable.close();
71     }
72
73     @Test
74     public void testStopProducerWhenNoActiveProducer() throws Exception {
75         String result = threadsController.stopProducer();
76         assertEquals("No active producer to stop", result);
77         verify(simpleProducer, never()).shutdown();
78     }
79
80     @Test
81     public void testPublishNMessage() throws Exception {
82         String topicName = "testTopic";
83         int numberOfMessages = 10;
84         CompletableFuture<String> future = CompletableFuture.completedFuture("Producer started for topic: " + topicName);
85         CompletableFuture<String> result = threadsController.publishNMessage(numberOfMessages, topicName);
86         assertEquals(future.getClass(), result.getClass());
87     }
88
89 }