2 * ========================LICENSE_START=================================
5 * Copyright (C) 2024: OpenInfra Foundation Europe
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
21 package com.demo.producer;
23 import com.demo.producer.controllers.ThreadsController;
24 import com.demo.producer.producer.SimpleProducer;
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;
41 import static org.junit.jupiter.api.Assertions.assertEquals;
42 import static org.mockito.Mockito.*;
45 @ContextConfiguration(classes = SimpleProducer.class)
46 public class ThreadsControllerTest {
50 private SimpleProducer simpleProducer;
53 private ThreadsController threadsController;
55 private MockHttpServletRequest request;
56 private MockHttpServletResponse response;
58 private AutoCloseable closable;
62 closable = MockitoAnnotations.openMocks(this);
63 request = new MockHttpServletRequest();
64 response = new MockHttpServletResponse();
65 RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request, response));
69 public void close() throws Exception {
74 public void testStopProducerWhenNoActiveProducer() throws Exception {
75 String result = threadsController.stopProducer();
76 assertEquals("No active producer to stop", result);
77 verify(simpleProducer, never()).shutdown();
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());