Skip to content
This repository has been archived by the owner on Nov 27, 2020. It is now read-only.

Commit

Permalink
Release v1.2.8
Browse files Browse the repository at this point in the history
*Fixed HTTP Status Code handling
*Fixed a NullPointerException in Wars, Nations and Tradehistory API :-(
  • Loading branch information
d1vshar committed Feb 23, 2019
1 parent 852967d commit 9e46784
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 28 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.github.adorable-skullmaster</groupId>
<artifactId>pw4j</artifactId>
<version>1.2.7</version> <!-- FIXME version bump -->
<version>1.2.8</version>
<name>pw4j</name>
<description>Java Wrapper for Politics and War API</description>
<packaging>jar</packaging>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public interface IPoliticsAndWar {

War getWar(int warId);

Wars getAllWars();
Wars getWars();

Wars getWarsByAmount(int amount);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public War getWar(int warId) {
}

@Override
public Wars getAllWars() {
public Wars getWars() {
return (Wars) execute(new WarsQuery(-1, null).build());
}

Expand All @@ -106,7 +106,7 @@ public Wars getWarsByAmount(int amount) {

@Override
public Wars getWarsByAlliance(Integer... alliance_ids) {
return (Wars) execute(new WarsQuery(-1, null).build());
return (Wars) execute(new WarsQuery(-1, alliance_ids).build());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.github.adorableskullmaster.pw4j.domains.Entity;

import java.io.PrintStream;
import java.time.Instant;
import java.util.Collections;
import java.util.Comparator;
Expand Down Expand Up @@ -65,8 +66,8 @@ private void update() {
}
}

public void printCacheStats() {
System.out.println(Integer.toString(getCacheSize()).concat("/").concat(Integer.toString(getCapacity())));
public void printCacheStats(PrintStream stream) {
stream.println(Integer.toString(getCacheSize()).concat("/").concat(Integer.toString(getCapacity())));
}

public int getCacheSize() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public Response fetchAPI() {

InputStream stream = conn.getErrorStream();

if (stream == null) {
if (stream == null && (respCode >= 200 && respCode < 300)) {
stream = conn.getInputStream();
return new Response<>(convertStreamToString(stream), t);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ public class TradehistoryQuery extends AuthenticatedQuery implements IEntityQuer
public TradehistoryQuery(String apiKey, Integer records, ResourceType[] resources) {
super.apiKey = apiKey;
this.records = records;
this.resources = Arrays.copyOf(resources,resources.length);
if (resources != null)
this.resources = Arrays.copyOf(resources, resources.length);
else
this.resources = null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ public class WarsQuery implements IEntityQuery {

public WarsQuery(int wid, Integer[] aids) {
this.wid = wid;
this.aids = Arrays.copyOf(aids,aids.length);
if (aids != null)
this.aids = Arrays.copyOf(aids, aids.length);
else this.aids = null;
}

@Override
Expand Down
82 changes: 63 additions & 19 deletions src/test/java/io/github/adorableskullmaster/pw4j/WrapperTests.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package io.github.adorableskullmaster.pw4j;

import io.github.adorableskullmaster.pw4j.domains.subdomains.SNationContainer;
import io.github.adorableskullmaster.pw4j.domains.TradeHistory;
import io.github.adorableskullmaster.pw4j.domains.Wars;
import io.github.adorableskullmaster.pw4j.enums.ResourceType;
import org.junit.Test;

import java.io.IOException;
import java.util.List;
import java.util.Properties;

import static org.junit.Assert.assertEquals;
Expand All @@ -21,6 +21,7 @@ public WrapperTests() {
properties.load(getClass().getClassLoader().getResourceAsStream("testData.properties"));
politicsAndWar = new PoliticsAndWarBuilder()
.setApiKey(properties.getProperty("apiKey"))
.setEnableCache(false)
.build();
} catch (IOException e) {
e.printStackTrace();
Expand Down Expand Up @@ -69,20 +70,33 @@ public void membersQueryTest() {

@Test
public void nationsQueryTest() {
// Check normal success
assertTrue(politicsAndWar.getNations().isSuccess());

List<SNationContainer> nationsContainer = politicsAndWar.getNations(false, 913, 1000, 500).getNationsContainer();
boolean b = nationsContainer.stream()
.noneMatch(nationContainer -> Integer.parseInt(nationContainer.getVacmode()) > 0);
assertTrue(b);

boolean b1 = nationsContainer.stream()
.allMatch(nationContainer -> nationContainer.getAllianceid() == 913);
assertTrue(b1);

boolean b2 = nationsContainer.stream()
.allMatch(nationContainer -> nationContainer.getScore() <= 1000 && nationContainer.getScore() >= 500);
assertTrue(b2);
// Check for any VM nation present
assertTrue(politicsAndWar.getNations(true)
.getNationsContainer()
.stream()
.anyMatch(nationContainer -> Integer.parseInt(nationContainer.getVacmode()) > 0));

// Check for all non-VM + alliance
assertTrue(politicsAndWar.getNationsByAlliance(false, 913)
.getNationsContainer()
.stream()
.allMatch(nationContainer -> nationContainer.getAllianceid() == 913 && Integer.parseInt(nationContainer.getVacmode()) == 0));

// Check for all non-VM + score
assertTrue(politicsAndWar.getNationsByScore(false, 4000, 3500)
.getNationsContainer()
.stream()
.allMatch(nationContainer -> nationContainer.getScore() <= 4000 && nationContainer.getScore() >= 3500 && Integer.parseInt(nationContainer.getVacmode()) == 0));

// Check for all non-VM + alliance + score
assertTrue(politicsAndWar.getNations(false, 913, 1000, 500)
.getNationsContainer()
.stream()
.allMatch(nationContainer -> (nationContainer.getScore() <= 1000 && nationContainer.getScore() >= 500) && nationContainer.getAllianceid() == 913 &&
Integer.parseInt(nationContainer.getVacmode()) == 0));
}

@Test
Expand All @@ -92,18 +106,48 @@ public void alliancesQueryTest() {

@Test
public void warsQueryTest() {
assertTrue(politicsAndWar.getAllWars().isSuccess());
// Check normal success
assertTrue(politicsAndWar.getWars().isSuccess());

// Check size
assertEquals(50, politicsAndWar.getWarsByAmount(50).getWars().size());
assertTrue(politicsAndWar.getWarsByAlliance(913).isSuccess());
assertEquals(50, politicsAndWar.getWars(50, 913).getWars().size());

// Check alliance
assertTrue(politicsAndWar.getWarsByAlliance(913)
.getWars()
.stream()
.anyMatch(sWarContainer -> sWarContainer.getAttackerAA().equalsIgnoreCase("Arrgh") ||
sWarContainer.getDefenderAA().equalsIgnoreCase("Arrgh")));

// Check size + alliance
Wars wars = politicsAndWar.getWars(50, 913);
assertEquals(50, wars.getWars().size());
assertTrue(wars.getWars()
.stream()
.anyMatch(sWarContainer -> sWarContainer.getAttackerAA().equalsIgnoreCase("Arrgh") ||
sWarContainer.getDefenderAA().equalsIgnoreCase("Arrgh")));
}

@Test
public void tradeHistoryQueryTest() {
// Check normal success
assertTrue(politicsAndWar.getAllTradehistory().isSuccess());

// Check size
assertEquals(10, politicsAndWar.getTradehistoryByAmount(10).getTrades().size());
assertEquals(50, politicsAndWar.getTradehistory(50, ResourceType.FOOD, ResourceType.ALUMINUM).getTrades().size());
assertTrue(politicsAndWar.getTradehistoryByType(ResourceType.FOOD, ResourceType.ALUMINUM).isSuccess());

// Check type
assertTrue(politicsAndWar.getTradehistoryByType(ResourceType.FOOD)
.getTrades()
.stream()
.allMatch(tradeContainer -> tradeContainer.getResource().equalsIgnoreCase("food")));

// Check size + type
TradeHistory tradehistory = politicsAndWar.getTradehistory(10, ResourceType.FOOD);
assertEquals(10, tradehistory.getTrades().size());
assertTrue(tradehistory.getTrades()
.stream()
.allMatch(tradeContainer -> tradeContainer.getResource().equalsIgnoreCase("food")));
}

@Test
Expand Down

0 comments on commit 9e46784

Please sign in to comment.