Skip to content
Snippets Groups Projects
Select Git revision
  • 108190cd6c460af3963680de8eb2be1538d331d4
  • main default protected
  • release
  • master-interface-proto4
  • ozg-5634-ozgcloud-ingress
  • OZG-3322_connect_to_mongodb_over_tls
  • 2.20.0
  • 2.19.0
  • 2.18.0
  • 2.17.0
  • 2.16.0
  • 2.15.0
  • 2.14.0
  • 2.13.0
  • 2.12.0
  • 2.11.0
  • 2.10.1
  • 2.10.0
  • 2.9.0
  • 2.8.0
  • 2.7.1
  • 2.7.0
  • 2.6.0
  • 2.5.1
  • 2.5.0
  • 2.4.0
26 results

UserServiceTest.java

Blame
  • XtaClientTest.java 4.91 KiB
    package de.ozgcloud.xta.client;
    
    import static de.ozgcloud.xta.client.factory.MessageMetaDataTestFactory.*;
    import static de.ozgcloud.xta.client.factory.XtaClientConfigTestFactory.*;
    import static org.assertj.core.api.Assertions.*;
    import static org.mockito.Mockito.*;
    
    import java.util.List;
    
    import org.junit.jupiter.api.BeforeEach;
    import org.junit.jupiter.api.DisplayName;
    import org.junit.jupiter.api.Nested;
    import org.junit.jupiter.api.Test;
    import org.junit.jupiter.api.extension.ExtendWith;
    import org.mockito.InjectMocks;
    import org.mockito.Mock;
    import org.mockito.Spy;
    import org.mockito.junit.jupiter.MockitoExtension;
    
    import de.ozgcloud.xta.client.config.XtaClientConfig;
    import de.ozgcloud.xta.client.core.WrappedXtaService;
    import de.ozgcloud.xta.client.model.XtaMessage;
    import de.ozgcloud.xta.client.model.XtaMessageMetaDataListing;
    import de.ozgcloud.xta.client.model.XtaTransportReport;
    import lombok.SneakyThrows;
    
    @ExtendWith(MockitoExtension.class)
    class XtaClientTest {
    
    	@Mock
    	private WrappedXtaService service;
    
    	@Mock
    	private XtaClientConfig config;
    
    	@Spy
    	@InjectMocks
    	private XtaClient client;
    
    	@DisplayName("get messages metadata")
    	@Nested
    	class TestGetMessagesMetadata {
    
    		@Mock
    		XtaMessageMetaDataListing xtaMessageMetaDataListing;
    
    		@BeforeEach
    		@SneakyThrows
    		void mock() {
    			doReturn(SELF_IDENTIFIER).when(client).deriveIdentifier(SELF_IDENTIFIER_VALUE);
    			when(service.getStatusList(SELF_IDENTIFIER, MAX_LIST_ITEMS)).thenReturn(xtaMessageMetaDataListing);
    			when(config.getMaxListItems()).thenReturn(MAX_LIST_ITEMS);
    		}
    
    		@DisplayName("should call checkAccountActive")
    		@Test
    		@SneakyThrows
    		void shouldCallCheckAccountActive() {
    			client.getMessagesMetadata(SELF_IDENTIFIER_VALUE);
    
    			verify(service).checkAccountActive(SELF_IDENTIFIER);
    		}
    
    		@DisplayName("should return get status list response")
    		@Test
    		@SneakyThrows
    		void shouldReturnGetStatusListResponse() {
    			var result = client.getMessagesMetadata(SELF_IDENTIFIER_VALUE);
    
    			assertThat(result).isEqualTo(xtaMessageMetaDataListing);
    		}
    
    	}
    
    	@DisplayName("get next messages meta data")
    	@Nested
    	class TestGetNextMessagesMetaData {
    
    		@Mock
    		XtaMessageMetaDataListing xtaMessageMetaDataListing;
    
    		@BeforeEach
    		@SneakyThrows
    		void mock() {
    			doReturn(SELF_IDENTIFIER).when(client).deriveIdentifier(SELF_IDENTIFIER_VALUE);
    			when(service.getStatusList(SELF_IDENTIFIER, MAX_LIST_ITEMS)).thenReturn(xtaMessageMetaDataListing);
    			when(config.getMaxListItems()).thenReturn(MAX_LIST_ITEMS);
    		}
    
    		@DisplayName("should return get status list response")
    		@Test
    		@SneakyThrows
    		void shouldReturnGetStatusListResponse() {
    			var result = client.getNextMessagesMetadata(SELF_IDENTIFIER_VALUE);
    
    			assertThat(result).isEqualTo(xtaMessageMetaDataListing);
    		}
    	}
    
    	@DisplayName("derive identifier")
    	@Nested
    	class TestDeriveIdentifier {
    
    		@DisplayName("should use value")
    		@Test
    		void shouldUseValue() {
    			when(config.getClientIdentifiers()).thenReturn(List.of(SELF_IDENTIFIER2, SELF_IDENTIFIER));
    
    			var result = client.deriveIdentifier(SELF_IDENTIFIER_VALUE);
    
    			assertThat(result.value()).isEqualTo(SELF_IDENTIFIER_VALUE);
    		}
    
    		@DisplayName("should throw when unknown")
    		@Test
    		void shouldThrowWhenUnknown() {
    			when(config.getClientIdentifiers()).thenReturn(List.of(SELF_IDENTIFIER2));
    
    			assertThatThrownBy(() -> client.deriveIdentifier(SELF_IDENTIFIER_VALUE))
    					.isInstanceOf(IllegalArgumentException.class)
    					.hasMessage("Unknown identifier: " + SELF_IDENTIFIER_VALUE);
    		}
    	}
    
    	@DisplayName("get message")
    	@Nested
    	class TestGetMessage {
    
    		@Mock
    		XtaMessage xtaMessage;
    
    		@Mock
    		XtaTransportReport xtaTransportReport;
    
    		@BeforeEach
    		@SneakyThrows
    		void mock() {
    			doReturn(SELF_IDENTIFIER).when(client).deriveIdentifier(SELF_IDENTIFIER_VALUE);
    			when(service.getMessage(MESSAGE_ID, SELF_IDENTIFIER)).thenReturn(xtaMessage);
    			when(service.getTransportReport(MESSAGE_ID, SELF_IDENTIFIER)).thenReturn(xtaTransportReport);
    		}
    
    		@DisplayName("should call close")
    		@Test
    		@SneakyThrows
    		void shouldCallClose() {
    			client.getMessage(SELF_IDENTIFIER_VALUE, MESSAGE_ID);
    
    			verify(service).close(MESSAGE_ID, SELF_IDENTIFIER);
    		}
    
    		@DisplayName("should return with message")
    		@Test
    		@SneakyThrows
    		void shouldReturn() {
    			var result = client.getMessage(SELF_IDENTIFIER_VALUE, MESSAGE_ID);
    
    			assertThat(result.message()).isEqualTo(xtaMessage);
    		}
    
    		@DisplayName("should return with transport report")
    		@Test
    		@SneakyThrows
    		void shouldReturnWithTransportReport() {
    			var result = client.getMessage(SELF_IDENTIFIER_VALUE, MESSAGE_ID);
    
    			assertThat(result.transportReport()).isEqualTo(xtaTransportReport);
    		}
    	}
    
    	@DisplayName("close")
    	@Nested
    	class TestClose {
    
    		@DisplayName("should call close")
    		@Test
    		@SneakyThrows
    		void shouldCallClose() {
    			doReturn(SELF_IDENTIFIER).when(client).deriveIdentifier(SELF_IDENTIFIER_VALUE);
    
    			client.close(SELF_IDENTIFIER_VALUE, MESSAGE_ID);
    
    			verify(service).close(MESSAGE_ID, SELF_IDENTIFIER);
    		}
    	}
    
    }