c4211ff401a33226193fbd33ea20ca934ee520a7
[dcaegen2/services/sdk.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * DCAEGEN2-SERVICES-SDK
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 package org.onap.dcaegen2.services.sdk.services.hvves.client.producer.impl;
21
22 import static org.assertj.core.api.Assertions.assertThat;
23 import static org.mockito.ArgumentMatchers.any;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.times;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.when;
28 import static org.onap.dcaegen2.services.sdk.services.hvves.client.producer.impl.utils.VesEvents.defaultVesEvent;
29
30 import io.netty.buffer.ByteBuf;
31 import io.netty.buffer.ByteBufAllocator;
32 import io.netty.buffer.Unpooled;
33 import org.junit.jupiter.api.BeforeEach;
34 import org.junit.jupiter.api.Test;
35 import org.onap.dcaegen2.services.sdk.services.hvves.client.producer.impl.encoders.EncodersFactory;
36 import org.onap.dcaegen2.services.sdk.services.hvves.client.producer.impl.encoders.ProtobufEncoder;
37 import org.onap.dcaegen2.services.sdk.services.hvves.client.producer.impl.encoders.WireFrameEncoder;
38 import org.onap.ves.VesEventOuterClass.VesEvent;
39 import reactor.core.publisher.Flux;
40
41 /**
42  * @author <a href="mailto:jakub.dudycz@nokia.com">Jakub Dudycz</a>
43  */
44 public class ProducerCoreTest {
45
46     private ProducerCore producerCore;
47     private EncodersFactory encodersFactoryMock;
48
49     @BeforeEach
50     public void setUp() {
51         encodersFactoryMock = mock(EncodersFactory.class);
52         producerCore = new ProducerCore(encodersFactoryMock);
53     }
54
55     @Test
56     public void encode_should_encode_message_stream_to_wire_frame() {
57         final WireFrameEncoder wireFrameEncoder = mock(WireFrameEncoder.class);
58         final ProtobufEncoder protobufEncoder = mock(ProtobufEncoder.class);
59         final ByteBuf protoBuffer = Unpooled.copiedBuffer(new byte[3]);
60         final ByteBuf wireFrameBuffer = Unpooled.copiedBuffer(new byte[5]);
61
62         when(protobufEncoder.encode(any(VesEvent.class))).thenReturn(protoBuffer);
63         when(wireFrameEncoder.encode(protoBuffer)).thenReturn(wireFrameBuffer);
64         when(encodersFactoryMock.createProtobufEncoder(ByteBufAllocator.DEFAULT)).thenReturn(protobufEncoder);
65         when(encodersFactoryMock.createWireFrameEncoder(ByteBufAllocator.DEFAULT)).thenReturn(wireFrameEncoder);
66
67         // given
68         final int messageStreamSize = 2;
69         final Flux<VesEvent> messages = Flux.just(defaultVesEvent()).repeat(messageStreamSize - 1);
70
71         // when
72         final ByteBuf lastMessage = producerCore.encode(messages, ByteBufAllocator.DEFAULT).blockLast();
73
74         // then
75         verify(encodersFactoryMock).createProtobufEncoder(ByteBufAllocator.DEFAULT);
76         verify(encodersFactoryMock).createWireFrameEncoder(ByteBufAllocator.DEFAULT);
77         verify(protobufEncoder, times(messageStreamSize)).encode(any(VesEvent.class));
78         verify(wireFrameEncoder, times(messageStreamSize)).encode(protoBuffer);
79
80         assertThat(lastMessage).isNotNull();
81         assertThat(lastMessage).isEqualTo(wireFrameBuffer);
82     }
83 }