Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests coverage+ #44

Merged
merged 4 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pipeline {

stage('Check'){
steps {
sleep 10
sleep 15
sh 'curl -s http://127.0.0.1:${RUN_PORT}/interface > /dev/null'
}
}
Expand Down
19 changes: 12 additions & 7 deletions src/main/java/com/wireguard/external/wireguard/Paging.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@
import java.util.List;

public class Paging<T> {
Class<T> genericType;

public Paging(Class<T> genericType) {
this.genericType = genericType;
}

public Page<T> apply(Pageable pageable, List<T> list) {
return buildPage(pageable, list);
}

private List<T> sort(Sort sort, List<T> list) {
private ArrayList<T> sort(Sort sort, ArrayList<T> list) {
try{
return sortList(sort.iterator(), list);
} catch (NoSuchFieldException e){
Expand All @@ -28,21 +33,21 @@ private List<T> sort(Sort sort, List<T> list) {


@SuppressWarnings("unchecked")
private List<T> sortList(Iterator<Sort.Order> orders, List<T> list) throws NoSuchFieldException {
private ArrayList<T> sortList(Iterator<Sort.Order> orders, ArrayList<T> list) throws NoSuchFieldException {
if (!orders.hasNext() || list.isEmpty()) {
return list;
}
Sort.Order order = orders.next();
Field field = list.get(0).getClass().getDeclaredField(order.getProperty());
Field field = genericType.getDeclaredField(order.getProperty());
field.setAccessible(true);

Comparator<Object> comparator = (o1, o2) -> {
try {
if (field.get(o1) == null && field.get(o2) == null) {
if ((o1 == null && o2 == null) || (!(o1==null || o2==null) && (field.get(o1) == null && field.get(o2) == null))) {
return 0;
} else if (field.get(o1) == null) {
} else if (o1 == null || field.get(o1) == null) {
return -1;
} else if (field.get(o2) == null) {
} else if (o2 == null || field.get(o2) == null) {
return 1;
}
if (field.get(o1) instanceof Comparable){
Expand All @@ -61,7 +66,7 @@ private List<T> sortList(Iterator<Sort.Order> orders, List<T> list) throws NoSuc
}

private Page<T> buildPage(Pageable pageable, List<T> list) {
ArrayList<T> sorted = new ArrayList<T>(sort(pageable.getSort(), list));
ArrayList<T> sorted = sort(pageable.getSort(), new ArrayList<T>(list));
PagedListHolder<T> page = new PagedListHolder<>(sorted);
page.setPageSize(pageable.getPageSize());
page.setPage(pageable.getPageNumber());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,43 +22,38 @@ public class WgPeerCreator {
@Value("${wg.interface.default.mask}")
private final int DEFAULT_MASK_FOR_NEW_CLIENTS = 32;
@Value("${wg.interface.default.persistent_keepalive}")
private final int DEFAULT_PERSISTENT_KEEPALIVE = 0;
public final int DEFAULT_PERSISTENT_KEEPALIVE = 0;

private final ISubnetSolver wgSubnetSolver;
private final WgTool wgTool;

@Autowired
public WgPeerCreator(WgTool wgTool, ISubnetSolver wgSubnetSolver, NetworkInterfaceDTO wgInterface) {
public WgPeerCreator(WgTool wgTool, ISubnetSolver wgSubnetSolver) {
this.wgTool = wgTool;
this.wgSubnetSolver = wgSubnetSolver;
}

private CreatedPeer createPeer(String privateKey, String publicKey, String presharedKey, Set<Subnet> allowedIps, int persistentKeepalive) {
CreatedPeer createdPeer = new CreatedPeer(publicKey, presharedKey, privateKey,
allowedIps,
persistentKeepalive);
logger.info("Created peer, public key: %s".formatted(publicKey.substring(0, 6)));
return createdPeer;
}

public CreatedPeer createPeerGenerateNulls(@Nullable String publicKey, @Nullable String presharedKey,
@Nullable String privateKey, @Nullable Set<Subnet> allowedIps,
@Nullable Integer persistentKeepalive){
privateKey = privateKey == null ? wgTool.generatePrivateKey() : privateKey;
publicKey = publicKey == null ? wgTool.generatePublicKey(privateKey) : publicKey;
presharedKey = presharedKey == null ? wgTool.generatePresharedKey() : presharedKey;
persistentKeepalive = persistentKeepalive == null ? DEFAULT_PERSISTENT_KEEPALIVE : persistentKeepalive;
if (allowedIps != null) {
allowedIps.forEach(wgSubnetSolver::obtain);
} else {
allowedIps = Set.of(wgSubnetSolver.obtainFree(DEFAULT_MASK_FOR_NEW_CLIENTS));
}
try {
return createPeer(privateKey, publicKey, presharedKey, allowedIps, persistentKeepalive);
} catch (CommandExecutionException e) {
if (allowedIps != null) {
allowedIps.forEach(wgSubnetSolver::obtain);
} else {
allowedIps = Set.of(wgSubnetSolver.obtainFree(DEFAULT_MASK_FOR_NEW_CLIENTS));
}
} catch (Exception e) {
assert allowedIps != null;
allowedIps.forEach(wgSubnetSolver::release);
throw e;
}
logger.info("Created peer, public key: %s".formatted(publicKey.substring(0, 6)));
return new CreatedPeer(publicKey, presharedKey, privateKey, allowedIps, persistentKeepalive);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class WgPeerRepository implements RepositoryPageable<WgPeer> {

private final WgTool wgTool;
private final NetworkInterfaceDTO wgInterface;
private final Paging<WgPeer> paging = new Paging<>();
private final Paging<WgPeer> paging = new Paging<>(WgPeer.class);

@Autowired
public WgPeerRepository(WgTool wgTool, NetworkInterfaceDTO wgInterface) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.wireguard.external.network.Subnet;
import com.wireguard.external.shell.ShellRunner;
import com.wireguard.external.wireguard.ParsingException;
import com.wireguard.external.wireguard.Repository;
import com.wireguard.external.wireguard.RepositoryPageable;
import com.wireguard.external.wireguard.peer.spec.FindByPublicKey;
import jakarta.annotation.Nullable;
import org.slf4j.Logger;
Expand All @@ -24,10 +26,10 @@ public class WgPeerService {

private static final Logger logger = LoggerFactory.getLogger(ShellRunner.class);
WgPeerCreator wgPeerCreator;
WgPeerRepository wgPeerRepository;
RepositoryPageable<WgPeer> wgPeerRepository;

@Autowired
public WgPeerService(NetworkInterfaceDTO wgInterface, WgPeerCreator wgPeerCreator, WgPeerRepository wgPeerRepository) {
public WgPeerService(WgPeerCreator wgPeerCreator, RepositoryPageable<WgPeer> wgPeerRepository) {
this.wgPeerCreator = wgPeerCreator;
this.wgPeerRepository = wgPeerRepository;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import com.wireguard.external.wireguard.Specification;
import com.wireguard.external.wireguard.peer.WgPeer;
import lombok.EqualsAndHashCode;

@EqualsAndHashCode
public class FindByPublicKey implements Specification<WgPeer> {

private final String publicKey;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class PeerControllerTest {
.endpoint("1.1.1.1")
.build()
);
Paging<WgPeer> paging = new Paging<>();
Paging<WgPeer> paging = new Paging<>(WgPeer.class);


@Test
Expand Down
104 changes: 104 additions & 0 deletions src/test/java/com/wireguard/external/wireguard/PagingTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package com.wireguard.external.wireguard;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import java.util.ArrayList;
import java.util.List;

class PagingTest {

Paging<String> paging = new Paging<>(String.class);
Paging<TestClass> pagingTestClass = new Paging<>(TestClass.class);
List<TestClass> testList = List.of(
new TestClass("a", 1),
new TestClass("b", 2),
new TestClass("c", 3),
new TestClass("d", 4)
);
@Test
void applyNoRules() {
Page<String> paged = paging.apply(Pageable.ofSize(3), List.of("a", "b", "c", "d"));
Assertions.assertEquals(2, paged.getTotalPages());
Assertions.assertEquals(4, paged.getTotalElements());
Assertions.assertEquals(3, paged.getSize());
}

@Test
void applyNoFieldException(){
Assertions.assertThrows(ParsingException.class, () -> {
paging.apply(PageRequest.of(0, 1, Sort.by("not_exits")), List.of("a", "b", "c", "d"));
});
}

@Test
void emptyListTest(){
Page<String> paged = paging.apply(Pageable.ofSize(3), List.of());
Assertions.assertEquals(0, paged.getTotalPages());
Assertions.assertEquals(0, paged.getTotalElements());
}

@Test
void normalSortingTest(){
Page<TestClass> paged = pagingTestClass.apply(PageRequest.of(0, 2, Sort.by("str")), testList);
Assertions.assertEquals(2, paged.getTotalPages());
Assertions.assertEquals(4, paged.getTotalElements());
Assertions.assertEquals(2, paged.getSize());
Assertions.assertEquals("a", paged.getContent().get(0).str);
Assertions.assertEquals("b", paged.getContent().get(1).str);
}

@Test
void reverseSortingTest(){
Page<TestClass> paged = pagingTestClass.apply(PageRequest.of(0, 2, Sort.by("str").descending()), testList);
Assertions.assertEquals("d", paged.getContent().get(0).str);
Assertions.assertEquals("c", paged.getContent().get(1).str);
}

@Test
void nullObjectsSortingTest(){
List<TestClass> testListNulls = new ArrayList<>();
testListNulls.add(new TestClass("a", 1));
testListNulls.add(null);
testListNulls.add(new TestClass("b", 2));
testListNulls.add(null);
testListNulls.add(null);
Page<TestClass> paged = pagingTestClass.apply(PageRequest.of(0, 10, Sort.by("str").descending()), testListNulls);
Assertions.assertEquals("b", paged.getContent().get(0).str);
Assertions.assertEquals("a", paged.getContent().get(1).str);
Assertions.assertNull(paged.getContent().get(2));
}

@Test
void nullValuesSortingTest(){
List<TestClass> testListNulls = new ArrayList<>();
testListNulls.add(new TestClass("a", 1));
testListNulls.add(new TestClass(null, 2));
testListNulls.add(new TestClass("b", 3));
testListNulls.add(new TestClass(null, 4));
testListNulls.add(new TestClass(null, 5));
Page<TestClass> paged = pagingTestClass.apply(PageRequest.of(0, 3, Sort.by("str").descending()), testListNulls);
Assertions.assertEquals("b", paged.getContent().get(0).str);
Assertions.assertEquals("a", paged.getContent().get(1).str);
Assertions.assertNull(paged.getContent().get(2).str);
}

private static class TestClass{
private final String str;
private final int num;

private TestClass(String str, int num) {
this.str = str;
this.num = num;
}
}



}
Loading