fixed for merge

This commit is contained in:
Djalim Simaila 2022-12-14 15:31:47 +01:00
commit 68df90c73d
8 changed files with 162 additions and 97 deletions

View File

@ -29,6 +29,14 @@ public class ArchDistribution extends AsyncRequestsParser implements Distributio
*/
private static final Logger logger = LoggerManager.getLogger("ArchDistribution");
private static String trimAfterCharacters(String str, String trimAfterCharacters){
for(char c : trimAfterCharacters.toCharArray()){
int index = str.indexOf(c);
if(index>0)str = str.substring(index);
}
return str;
}
/**
* This function return a package from arch package api in the form of a Pair
* Composed of a Package object, and a set of string containing the names of
@ -61,17 +69,20 @@ public class ArchDistribution extends AsyncRequestsParser implements Distributio
// get infos
Set<String> dependenciesNames = new HashSet<>();
for (Object dependency : resultJson.getJSONArray("depends")) {
dependenciesNames.add((String) dependency);
for(Object dependency : resultJson.getJSONArray("depends")){
dependenciesNames.add(trimAfterCharacters((String)dependency, "<>="));
}
futureResult.complete(new Pair<>(
new Package(
resultJson.getString("pkgname"),
resultJson.getString("pkgver"),
resultJson.getString("repo"),
resultJson.getString("pkgdesc")),
dependenciesNames));
}).exceptionally(error -> {
resultJson.getString("pkgdesc"),
"arch"
),
dependenciesNames
));
}).exceptionally(error ->{
error.printStackTrace();
logger.warning("Error while fetching package %s from the API : \n%s".formatted(packageName, error));
futureResult.complete(null);
@ -82,6 +93,7 @@ public class ArchDistribution extends AsyncRequestsParser implements Distributio
}
/**
* Search for a package matching a pattern and return a list of packages and
* return a list of string matching this pattern.
@ -108,10 +120,12 @@ public class ArchDistribution extends AsyncRequestsParser implements Distributio
JSONObject searchResultJson = (JSONObject) searchResultObj;
// add package into to list
searchedPackagesList.add(new SearchedPackage(
searchResultJson.getString("pkgname"),
searchResultJson.getString("pkgver"),
searchResultJson.getString("repo"),
searchResultJson.getString("pkgdesc")));
searchResultJson.getString("pkgname"),
searchResultJson.getString("pkgver"),
searchResultJson.getString("repo"),
searchResultJson.getString("pkgdesc"),
"arch"
));
}
futureSearchedPackages.complete(searchedPackagesList);
}).exceptionally(error -> {

View File

@ -1,6 +1,5 @@
package fr.packageviewer.distribution;
import java.io.IOException;
import java.net.URI;
import java.util.*;
import java.net.http.*;
@ -14,8 +13,6 @@ import java.util.logging.Logger;
import fr.packageviewer.pack.Package;
import fr.packageviewer.pack.SearchedPackage;
import fr.packageviewer.LoggerManager;
import fr.packageviewer.pack.Package;
import fr.packageviewer.pack.SearchedPackage;
/**
* This class handles package requests for Fedora. All return objects in
@ -78,9 +75,12 @@ public class FedoraDistribution extends AsyncRequestsParser implements Distribut
json.getString("basename"),
json.getString("version"),
json.getString("repo"),
json.getString("description")),
dependenciesNames));
}).exceptionally(error -> {
json.getString("description"),
"fedora"
),
dependenciesNames
));
}).exceptionally(error->{
error.printStackTrace();
logger.warning("Error while fetching package %s from the API : \n%s".formatted(packageName, error));
futureResult.complete(null);
@ -120,10 +120,12 @@ public class FedoraDistribution extends AsyncRequestsParser implements Distribut
JSONObject searchResultJson = (JSONObject) searchResultObj;
// add package into to list
searchedPackagesList.add(new SearchedPackage(
searchResultJson.getString("neofetch"),
searchResultJson.getString("name"),
null,
searchResultJson.getString("fullname"),
searchResultJson.getString("description")));
null,
searchResultJson.getString("description"),
"fedora"
));
}
futureSearchedPackages.complete(searchedPackagesList);
}).exceptionally(error -> {

View File

@ -43,8 +43,8 @@ public class Package extends SearchedPackage {
* @param repo String, repository where the package is located
* @param description String, description of the package
*/
public Package(String name, String version, String repo, String description) {
this(name, version, repo, description, new ArrayList<>());
public Package(String name, String version, String repo, String description,String distribution) {
this(name, version, repo, description, distribution, new ArrayList<>());
}
/**
@ -56,8 +56,8 @@ public class Package extends SearchedPackage {
* @param description String, description of the package
* @param deps List of Package, dependencies of the package
*/
public Package(String name, String version, String repo, String description, List<Package> deps) {
super(name, version, repo, description);
public Package(String name, String version, String repo, String description,String distribution, List<Package> deps) {
super(name, version, repo, description, distribution);
this.deps = deps;
}
/**

View File

@ -24,6 +24,7 @@ public class SearchedPackage {
* Description of the package
*/
private final String description;
private final String distribution;
/**
* Getter for the name attribute
@ -61,6 +62,11 @@ public class SearchedPackage {
return description;
}
public String getDistribution() {
return distribution;
}
/**
* Constructor for the SearchedPackage class
*
@ -70,11 +76,12 @@ public class SearchedPackage {
* @param description String, description of the package
*
*/
public SearchedPackage(String name, String version, String repo, String desciption) {
public SearchedPackage(String name, String version, String repo, String description, String distribution) {
this.name = name;
this.version = version;
this.repo = repo;
this.description = desciption;
this.description = description;
this.distribution = distribution;
}
/**
@ -84,6 +91,6 @@ public class SearchedPackage {
*/
@Override
public String toString() {
return "SearchedPackage{name=%s,version=%s,repo=%s,description=%s}".formatted(name, version, repo, description);
return "SearchedPackage{name=%s,version=%s,repo=%s,description=%s,distribution=%s}".formatted(name, version, repo, description, distribution);
}
}

View File

@ -1,85 +1,19 @@
package fr.packageviewer;
import fr.packageviewer.distribution.ArchDistribution;
import fr.packageviewer.distribution.Distribution;
import fr.packageviewer.pack.Package;
import fr.packageviewer.pack.SearchedPackage;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.concurrent.ExecutionException;
public class ArchTest {
@Test
public void testBasicQueryDoNotFail(){
Distribution arch = new ArchDistribution();
arch.getPackageTree("bash", 0);
public class ArchTest extends DistroTest<ArchDistribution> {
@Override
protected ArchDistribution createInstance() {
return new ArchDistribution();
}
@Test
public void testBashPackageHasNameBash() throws ExecutionException, InterruptedException {
Distribution arch = new ArchDistribution();
Package pack = arch.getPackageTree("bash", 0).get();
Assertions.assertEquals(pack.getName(), "bash");
}
@Test
public void testQueryWithDepth0HasNoDeps() throws ExecutionException, InterruptedException {
Distribution arch = new ArchDistribution();
Package pack = arch.getPackageTree("bash", 0).get();
Assertions.assertEquals(pack.getDeps().size(), 0);
}
@Test
public void testQueryWithDepth1hasOneLevelOfDeps() throws ExecutionException, InterruptedException {
Distribution arch = new ArchDistribution();
Package pack = arch.getPackageTree("bash", 1).get();
Assertions.assertNotEquals(pack.getDeps().size(), 0);
for(Package dep : pack.getDeps()){
Assertions.assertEquals(dep.getDeps().size(), 0);
}
}
@Test
public void testBashIsInCore() throws ExecutionException, InterruptedException {
Distribution arch = new ArchDistribution();
Package pack = arch.getPackageTree("bash", 1).get();
public void testBashIsInCore() {
Package pack = helperGetPackageTree("bash", 0);
Assertions.assertEquals(pack.getRepo(), "core");
}
@Test
public void testBashDescriptionIsNotEmpty() throws ExecutionException, InterruptedException {
Distribution arch = new ArchDistribution();
Package pack = arch.getPackageTree("bash", 1).get();
Assertions.assertFalse(pack.getDescription().isEmpty());
}
@Test
public void testBashVersionIsNotEmpty() throws ExecutionException, InterruptedException {
Distribution arch = new ArchDistribution();
Package pack = arch.getPackageTree("bash", 1).get();
Assertions.assertFalse(pack.getVersion().isEmpty());
}
@Test
public void testInvalidPackageReturnsNull() throws ExecutionException, InterruptedException {
Distribution arch = new ArchDistribution();
Package pack = arch.getPackageTree("lndhsgudw", 1).get();
Assertions.assertNull(pack);
}
@Test
public void testThatBashSearchReturnsResults() throws ExecutionException, InterruptedException {
Distribution arch = new ArchDistribution();
Assertions.assertNotEquals(arch.searchPackage("bash").get().size(), 0);
}
@Test
public void testThatBashSearchContainsBash() throws ExecutionException, InterruptedException {
Distribution arch = new ArchDistribution();
for(SearchedPackage pack : arch.searchPackage("bash").get()){
if(pack.getName().equals("bash")){
Assertions.assertTrue(true);
return;
}
}
Assertions.fail("No package named 'bash' in results");
}
}

View File

@ -0,0 +1,96 @@
package fr.packageviewer;
import fr.packageviewer.distribution.ArchDistribution;
import fr.packageviewer.distribution.Distribution;
import fr.packageviewer.pack.Package;
import fr.packageviewer.pack.SearchedPackage;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
public abstract class DistroTest<T extends Distribution> {
protected abstract T createInstance();
protected List<SearchedPackage> helperSearchPackage(String packageName){
Distribution distribution = createInstance();
Future<List<SearchedPackage>> future = distribution.searchPackage(packageName);
try{
return future.get();
} catch (ExecutionException | InterruptedException e) {
throw new RuntimeException(e);
}
}
protected Package helperGetPackageTree(String packageName, int depth){
Distribution distribution = createInstance();
Future<Package> future = distribution.getPackageTree(packageName, depth);
try{
return future.get();
} catch (ExecutionException | InterruptedException e) {
throw new RuntimeException(e);
}
}
@Test
public void testBasicQueryDoNotFail(){
helperGetPackageTree("bash", 0);
}
@Test
public void testBashPackageHasNameBash() throws ExecutionException, InterruptedException {
Distribution arch = new ArchDistribution();
Package pack = arch.getPackageTree("bash", 0).get();
Assertions.assertEquals(pack.getName(), "bash");
}
@Test
public void testQueryWithDepth0HasNoDeps() {
Distribution arch = new ArchDistribution();
Package pack = helperGetPackageTree("bash", 0);
Assertions.assertEquals(pack.getDeps().size(), 0);
}
@Test
public void testQueryWithDepth1hasOneLevelOfDeps() {
Package pack = helperGetPackageTree("bash", 1);
Assertions.assertNotEquals(pack.getDeps().size(), 0);
for(Package dep : pack.getDeps()){
Assertions.assertEquals(dep.getDeps().size(), 0);
}
}
@Test
public void testBashDescriptionIsNotEmpty() {
Package pack = helperGetPackageTree("bash", 1);
Assertions.assertFalse(pack.getDescription().isEmpty());
}
@Test
public void testBashVersionIsNotEmpty() {
Package pack = helperGetPackageTree("bash", 1);
Assertions.assertFalse(pack.getVersion().isEmpty());
}
@Test
public void testInvalidPackageReturnsNull() {
Package pack = helperGetPackageTree("lndhsgudw", 1);
Assertions.assertNull(pack);
}
@Test
public void testThatBashSearchReturnsResults() {
Assertions.assertNotEquals(helperSearchPackage("bash").size(), 0);
}
@Test
public void testThatBashSearchContainsBash() {
for(SearchedPackage pack : helperSearchPackage("bash")){
if(pack.getName().equals("bash")){
Assertions.assertTrue(true);
return;
}
}
Assertions.fail("No package named 'bash' in results");
}
}

View File

@ -0,0 +1,10 @@
package fr.packageviewer;
import fr.packageviewer.distribution.FedoraDistribution;
public class FedoraTest extends DistroTest<FedoraDistribution> {
@Override
protected FedoraDistribution createInstance() {
return new FedoraDistribution();
}
}

View File

@ -0,0 +1,2 @@
junit.jupiter.execution.parallel.enabled=true
junit.jupiter.execution.parallel.mode.default=concurrent