2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 Nokia. All rights reserved.
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
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 org.onap.netconfsimulator.kafka;
23 import org.bitbucket.radistao.test.annotation.BeforeAllMethods;
24 import org.bitbucket.radistao.test.runner.BeforeAfterSpringTestRunner;
25 import org.junit.ClassRule;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.springframework.beans.factory.annotation.Autowired;
29 import org.springframework.boot.test.context.SpringBootTest;
30 import org.springframework.kafka.core.KafkaTemplate;
31 import org.springframework.kafka.test.context.EmbeddedKafka;
32 import org.springframework.kafka.test.rule.EmbeddedKafkaRule;
34 import java.util.List;
36 import static org.assertj.core.api.Assertions.assertThat;
38 @RunWith(BeforeAfterSpringTestRunner.class)
39 @SpringBootTest(classes = {StoreService.class, EmbeddedKafkaConfig.class})
41 public class StoreServiceTest {
43 private static final String MESSAGE_1 = "message1";
44 private static final String MESSAGE_2 = "message2";
45 private static final String MESSAGE_3 = "message3";
48 public static EmbeddedKafkaRule embeddedKafka = new EmbeddedKafkaRule(1, true, 1, "config");
54 KafkaTemplate<String, String> kafkaTemplate;
57 public void setupBeforeAll() {
62 public void testShouldReturnAllAvailableMessages(){
64 List<MessageDTO> actualMessages = service.getAllMessages();
66 assertResponseContainsExpectedMessages(actualMessages, 3, MESSAGE_1, MESSAGE_2, MESSAGE_3);
70 public void testShouldGetLastMessagesRespectingOffset(){
72 List<MessageDTO> wantedLastMsg = service.getLastMessages(1L);
74 assertResponseContainsExpectedMessages(wantedLastMsg, 1, MESSAGE_3);
78 public void testShouldGetAll3Messages() {
79 List<MessageDTO> wantedLastMsgs = service.getLastMessages(3L);
81 assertResponseContainsExpectedMessages(wantedLastMsgs, 3, MESSAGE_1, MESSAGE_2, MESSAGE_3);
84 private void prepareProducer(){
85 kafkaTemplate.send("config", "message1");
86 kafkaTemplate.send("config", "message2");
87 kafkaTemplate.send("config", "message3");
90 private void assertResponseContainsExpectedMessages(List<MessageDTO> actualMessages, int expectedMessageCount, String... expectedMessages){
91 assertThat(actualMessages.stream().map(MessageDTO::getConfiguration))
92 .hasSize(expectedMessageCount)
93 .containsExactly(expectedMessages);