02eec12ac85fc0b12f4e6e872cecbf2b7a7394d4
[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 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;
32
33 import static org.mockito.Mockito.when;
34
35 @RunWith(SpringJUnit4ClassRunner.class)
36 public class StoreControllerTest {
37
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";
41
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));
45
46     @Mock
47     private StoreService service;
48
49     @InjectMocks
50     private StoreController storeController;
51
52
53     @Test
54     public void lessShouldTakeAllMessagesTest() {
55         when(service.getLastMessages(3)).thenReturn(ALL_MESSAGES);
56
57         List<MessageDTO> lessResponse = storeController.less(3);
58
59         assertResponseContainsExpectedMessages(lessResponse, 3, MESSAGE_1, MESSAGE_2, MESSAGE_3);
60     }
61
62     @Test
63     public void lessShouldTakeTwoMessagesTest() {
64         when(service.getLastMessages(2)).thenReturn(Lists.newArrayList(new MessageDTO(Instant.now().getEpochSecond(), MESSAGE_1)));
65
66         List<MessageDTO> lessResult = storeController.less(2);
67
68         assertResponseContainsExpectedMessages(lessResult, 1, MESSAGE_1);
69     }
70
71     @Test
72     public void shouldGetAllMessages(){
73         when(service.getAllMessages()).thenReturn(ALL_MESSAGES);
74
75         List<MessageDTO> allMsgResult = storeController.getAllConfigurationChanges();
76
77         assertResponseContainsExpectedMessages(allMsgResult, 3, MESSAGE_1, MESSAGE_2, MESSAGE_3);
78     }
79
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);
84     }
85
86 }