fd36116a8fc5aa8207ac52612d1fd7389030b41c
[integration.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Simulator
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
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 org.onap.netconfsimulator.kafka;
22
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;
33
34 import java.util.List;
35
36 import static org.assertj.core.api.Assertions.assertThat;
37
38 @RunWith(BeforeAfterSpringTestRunner.class)
39 @SpringBootTest(classes = {StoreService.class, EmbeddedKafkaConfig.class})
40 @EmbeddedKafka
41 public class StoreServiceTest {
42
43     private static final String MESSAGE_1 = "message1";
44     private static final String MESSAGE_2 = "message2";
45     private static final String MESSAGE_3 = "message3";
46
47     @ClassRule
48     public static EmbeddedKafkaRule embeddedKafka = new EmbeddedKafkaRule(1, true, 1, "config");
49
50     @Autowired
51     StoreService service;
52
53     @Autowired
54     KafkaTemplate<String, String> kafkaTemplate;
55
56     @BeforeAllMethods
57     public void setupBeforeAll() {
58         prepareProducer();
59     }
60
61     @Test
62     public void testShouldReturnAllAvailableMessages(){
63
64         List<MessageDTO> actualMessages = service.getAllMessages();
65
66         assertResponseContainsExpectedMessages(actualMessages, 3, MESSAGE_1, MESSAGE_2, MESSAGE_3);
67     }
68
69     @Test
70     public void testShouldGetLastMessagesRespectingOffset(){
71
72         List<MessageDTO> wantedLastMsg = service.getLastMessages(1L);
73
74         assertResponseContainsExpectedMessages(wantedLastMsg, 1, MESSAGE_3);
75     }
76
77     @Test
78     public void testShouldGetAll3Messages()  {
79         List<MessageDTO> wantedLastMsgs = service.getLastMessages(3L);
80
81         assertResponseContainsExpectedMessages(wantedLastMsgs, 3, MESSAGE_1, MESSAGE_2, MESSAGE_3);
82     }
83
84     private void prepareProducer(){
85         kafkaTemplate.send("config", "message1");
86         kafkaTemplate.send("config", "message2");
87         kafkaTemplate.send("config", "message3");
88     }
89
90     private void assertResponseContainsExpectedMessages(List<MessageDTO> actualMessages, int expectedMessageCount, String... expectedMessages){
91         assertThat(actualMessages.stream().map(MessageDTO::getConfiguration))
92                 .hasSize(expectedMessageCount)
93                 .containsExactly(expectedMessages);
94     }
95
96 }
97
98
99
100
101
102
103