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 java.time.Instant;
24 import java.util.List;
25 import org.assertj.core.api.Assertions;
26 import org.assertj.core.util.Lists;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.mockito.InjectMocks;
30 import org.mockito.Mock;
31 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
33 import static org.mockito.Mockito.when;
35 @RunWith(SpringJUnit4ClassRunner.class)
36 public class StoreControllerTest {
38 private static final String MESSAGE_3 = "message 3";
39 private static final String MESSAGE_2 = "message 2";
40 private static final String MESSAGE_1 = "message 1";
42 private static final List<MessageDTO> ALL_MESSAGES = Lists.newArrayList(new MessageDTO(Instant.now().getEpochSecond(), MESSAGE_1),
43 new MessageDTO(Instant.now().getEpochSecond(), MESSAGE_2),
44 new MessageDTO(Instant.now().getEpochSecond(), MESSAGE_3));
47 private StoreService service;
50 private StoreController storeController;
54 public void lessShouldTakeAllMessagesTest() {
55 when(service.getLastMessages(3)).thenReturn(ALL_MESSAGES);
57 List<MessageDTO> lessResponse = storeController.less(3);
59 assertResponseContainsExpectedMessages(lessResponse, 3, MESSAGE_1, MESSAGE_2, MESSAGE_3);
63 public void lessShouldTakeTwoMessagesTest() {
64 when(service.getLastMessages(2)).thenReturn(Lists.newArrayList(new MessageDTO(Instant.now().getEpochSecond(), MESSAGE_1)));
66 List<MessageDTO> lessResult = storeController.less(2);
68 assertResponseContainsExpectedMessages(lessResult, 1, MESSAGE_1);
72 public void shouldGetAllMessages(){
73 when(service.getAllMessages()).thenReturn(ALL_MESSAGES);
75 List<MessageDTO> allMsgResult = storeController.getAllConfigurationChanges();
77 assertResponseContainsExpectedMessages(allMsgResult, 3, MESSAGE_1, MESSAGE_2, MESSAGE_3);
80 private void assertResponseContainsExpectedMessages(List<MessageDTO> actualMessages, int expectedMessageCount, String... expectedMessages){
81 Assertions.assertThat(actualMessages.stream().map(MessageDTO::getConfiguration))
82 .hasSize(expectedMessageCount)
83 .containsExactly(expectedMessages);