NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

package com.highradius.g4.catalog.service.impl;

import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.junit.jupiter.api.Assertions;
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.TestInstance;
import org.mockito.ArgumentMatchers;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import com.highradius.g4.catalog.dto.ConnectionDTO;
import com.highradius.g4.catalog.dto.CountDTO;
import com.highradius.g4.catalog.entity.DcAuthType;
import com.highradius.g4.catalog.entity.DcConnection;
import com.highradius.g4.catalog.entity.DcDataProvider;
import com.highradius.g4.catalog.entity.DcDataSource;
import com.highradius.g4.catalog.entity.MapDataSourceProvider;
import com.highradius.g4.catalog.service.manager.ConnectionManager;
import com.highradius.g4.catalog.util.Constants;

class ConnectionServiceImplTest {

@InjectMocks
private ConnectionServiceImpl connectionServiceImpl;

@Mock
private ConnectionManager connectionManager;

@BeforeEach
public void setup() {
MockitoAnnotations.openMocks(this);
}

/**
* Generate random integer.
*
* @param max the max
* @param min the min
* @return the int
*/
public int generateRandomInteger(int max, int min) {
return (int) (Math.random() * (max - min + 1) + min);
}

/**
* Generate random list of long.
*
* @param max the max
* @param min the min
* @return the list
*/
public List<Long> generateRandomListOfLong(int max, int min) {
List<Long> randomList = new ArrayList<>();
for (int i = 1; i <= generateRandomInteger(max, min); i++) {
randomList.add(Long.valueOf(i));
}
return randomList;
}

@Nested
@DisplayName("function getMyConnection()")
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class GetMyConnectionTests {
private List<Long> connectionIds;
private List<DcConnection> connections;
private Map<Long, Long> ingestionJobCount;
private Map<Long, DcAuthType> authTypes;

@BeforeEach
public void setup() {
connectionIds = generateRandomListOfLong(20, 1);
connections = connectionIds.stream().map(id -> {
DcConnection connection = new DcConnection();
MapDataSourceProvider mapDataSourceProvider = new MapDataSourceProvider();
DcDataProvider dataProvider = new DcDataProvider();
DcDataSource dataSource = new DcDataSource();
mapDataSourceProvider.setDcDataProvider(dataProvider);
mapDataSourceProvider.setDcDataSource(dataSource);
connection.setPkDcConnectionId(id);
connection.setLastUsed(new Date());
connection.setMapDataSourceProvider(mapDataSourceProvider);
return connection;
}).collect(Collectors.toList());

ingestionJobCount = connectionIds.stream()
.collect(Collectors.toMap(id -> id, id -> Long.valueOf(generateRandomInteger(100, 1))));

authTypes = connectionIds.stream().collect(Collectors.toMap(id -> id, id -> {
DcAuthType authType = new DcAuthType();
authType.setName(Constants.TEXT);
authType.setPkDcAuthTypeId(Long.valueOf(generateRandomInteger(1000, 101)));
return authType;
}));
}

@Test
void should_return_connections_when_getMyConnections_works() throws Exception {
when(connectionManager.getSelectedConnections(anyLong(), anyString(), anyInt(), anyInt(), any()))
.thenReturn(connections);
when(connectionManager.getIngestionJobCount(anyLong(), ArgumentMatchers.<Long>anyList()))
.thenReturn(ingestionJobCount);
when(connectionManager.getAuthType(anyLong(), ArgumentMatchers.<Long>anyList())).thenReturn(authTypes);
when(connectionManager.getTotalConnectionCount(anyString(), anyLong()))
.thenReturn(Long.valueOf(connections.size()));
CountDTO<ConnectionDTO> result = connectionServiceImpl.getMyConnection(Constants.TEXT,
generateRandomInteger(10, 1), generateRandomInteger(10, 1));
Integer randomPosition = generateRandomInteger(result.getResults().size() - 1, 0);
ConnectionDTO connectionAtRandomChosenPosition = result.getResults().get(randomPosition);
Assertions.assertAll(
() -> assertTrue(connectionIds.containsAll(result.getResults().stream()
.map(connection -> connection.getConnectionId()).collect(Collectors.toList()))),
() -> assertEquals(connections.size(), result.getCount()),
() -> assertEquals(ingestionJobCount.get(connectionAtRandomChosenPosition.getConnectionId()),
connectionAtRandomChosenPosition.getIngestionJobCount()),
() -> assertEquals(authTypes.get(connectionAtRandomChosenPosition.getConnectionId()).getName(),
connectionAtRandomChosenPosition.getAuthTypeDisplayName()));
}

@Test
void should_return_empty_list_of_connections_when_getSelectedConnections_returns_empty_list() throws Exception {
when(connectionManager.getSelectedConnections(anyLong(), anyString(), anyInt(), anyInt(), any()))
.thenReturn(Collections.emptyList());
when(connectionManager.getTotalConnectionCount(anyString(), anyLong())).thenReturn(Long.valueOf(0L));
CountDTO<ConnectionDTO> result = connectionServiceImpl.getMyConnection(Constants.TEXT,
generateRandomInteger(10, 1), generateRandomInteger(10, 1));
Assertions.assertAll(() -> assertTrue(result.getResults().isEmpty()),
() -> assertEquals(0L, result.getCount()));
}

@Test
void should_return_connections_with_authType_null_and_ingestionJobCount_zero_when_managers_returns_empty_hashmap()
throws Exception {
when(connectionManager.getSelectedConnections(anyLong(), anyString(), anyInt(), anyInt(), any()))
.thenReturn(connections);
when(connectionManager.getIngestionJobCount(anyLong(), ArgumentMatchers.<Long>anyList()))
.thenReturn(new HashMap<Long, Long>());
when(connectionManager.getAuthType(anyLong(), ArgumentMatchers.<Long>anyList()))
.thenReturn(new HashMap<Long, DcAuthType>());
when(connectionManager.getTotalConnectionCount(anyString(), anyLong()))
.thenReturn(Long.valueOf(connections.size()));
CountDTO<ConnectionDTO> result = connectionServiceImpl.getMyConnection(Constants.TEXT,
generateRandomInteger(10, 1), generateRandomInteger(10, 1));
Integer randomPosition = generateRandomInteger(result.getResults().size() - 1, 0);
ConnectionDTO connectionAtRandomChosenPosition = result.getResults().get(randomPosition);
Assertions.assertAll(() -> assertEquals(0, connectionAtRandomChosenPosition.getIngestionJobCount()),
() -> assertEquals(null, connectionAtRandomChosenPosition.getAuthTypeDisplayName()));
}
}

}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.