Skip to content

Commit

Permalink
Release of version 1.7.2 (#35)
Browse files Browse the repository at this point in the history
- Use jwiki published on GitHub Packages
- Make Travis able to access GitHub Packages
- Make Google Cloud build able to access GitHub Packages
- Make use of new features in jwiki (Namespace constructor, Wiki builder
- Add e2e tests
- Change logging back from logback to slf4j-log12
  • Loading branch information
benjaminkomen authored Jan 4, 2020
2 parents 39118d0 + 3b7c7b3 commit eee3e92
Show file tree
Hide file tree
Showing 11 changed files with 181 additions and 75 deletions.
36 changes: 36 additions & 0 deletions .travis.settings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
http://maven.apache.org/xsd/settings-1.0.0.xsd">

<activeProfiles>
<activeProfile>github</activeProfile>
</activeProfiles>

<profiles>
<profile>
<id>github</id>
<repositories>
<repository>
<id>central</id>
<url>https://repo1.maven.org/maven2</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>false</enabled></snapshots>
</repository>
<repository>
<id>github-benjaminkomen</id>
<name>GitHub Benjamin Komen Apache Maven Packages</name>
<url>https://maven.pkg.github.com/benjaminkomen/jwiki</url>
</repository>
</repositories>
</profile>
</profiles>

<servers>
<server>
<id>github-benjaminkomen</id>
<username>benjaminkomen</username>
<password>${env.GITHUB_TOKEN}</password>
</server>
</servers>
</settings>
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,10 @@ cache:
- '$HOME/.m2/repository'
- '$HOME/.sonar/cache'

# the dependency benjaminkomen/jwiki recides in GitHub Packages, for which you need credentials to download.
# They are set here, with the GITHUB_TOKEN in Travis.
install:
- cp .travis.settings.xml $HOME/.m2/settings.xml

script:
- ./mvnw clean org.jacoco:jacoco-maven-plugin:prepare-agent install sonar:sonar
4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ WORKDIR /app
COPY pom.xml .
COPY src ./src

ARG GITHUB_TOKEN
ENV GITHUB_TOKEN=${GITHUB_TOKEN}
COPY .travis.settings.xml /root/.m2/settings.xml

# Build a release artifact.
RUN mvn package -DskipTests

Expand Down
18 changes: 16 additions & 2 deletions cloudbuild.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
steps:
# build the container image
- name: 'gcr.io/cloud-builders/docker'
args: ['build', '-t', 'gcr.io/tibiawikiapi-246008/tibiawikiapi', '.']
# push the container image to Container Registry
entrypoint: 'bash'
args: [
'-c',
'docker build -t gcr.io/$PROJECT_ID/tibiawikiapi -f Dockerfile --build-arg GITHUB_TOKEN=$$GITHUB_TOKEN .'
]
secretEnv: ['GITHUB_TOKEN']

# push the container image to Container Registry
- name: 'gcr.io/cloud-builders/docker'
args: ['push', 'gcr.io/tibiawikiapi-246008/tibiawikiapi']


# Deploy container image to Cloud Run
- name: 'gcr.io/cloud-builders/gcloud'
args: ['beta', 'run', 'deploy', 'tibiawikiapi', '--image', 'gcr.io/tibiawikiapi-246008/tibiawikiapi', '--region', 'europe-west1','--platform', 'managed', '--memory', '1Gi', '--project', 'tibiawikiapi-246008', '--quiet']

images:
- gcr.io/tibiawikiapi-246008/tibiawikiapi

secrets:
- kmsKeyName: projects/tibiawikiapi-246008/locations/global/keyRings/my-secrets/cryptoKeys/github-token
secretEnv:
GITHUB_TOKEN: CiQA5BHiVau0r93/go0GclWewjQvi10erqEHGi7nG5CHJpJudLwSUgAVCO4xq1IpCWVcMlDAM8Hg87XsjBcSojA7bS8W+gMGT43KLQNAf+1aj6qnjJ+l+Z6mST8nIGQW6bONZ014xy5SlW681SdgKUo6CoyVfAeyBf4=
33 changes: 12 additions & 21 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.tibiawiki</groupId>
<artifactId>TibiaWikiApi</artifactId>
<version>1.7.1</version>
<version>1.7.2</version>
<packaging>jar</packaging>
<name>TibiaWikiApi</name>
<url>https://github.com/benjaminkomen/TibiaWikiApi</url> <!-- https://tibiawiki.dev -->
Expand All @@ -16,12 +16,17 @@
<relativePath/> <!-- lookup parent from repository -->
</parent>

<repositories>
<repository>
<id>github</id>
<url>https://maven.pkg.github.com/benjaminkomen/jwiki</url>
</repository>
</repositories>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<slf4j.version>1.7.28</slf4j.version>
<logback.version>1.2.3</logback.version>
<logstash.version>6.3</logstash.version>
<json.version>20190722</json.version>
<lombok.version>1.18.10</lombok.version>
<jetbrains.version>18.0.0</jetbrains.version>
Expand All @@ -33,21 +38,13 @@
<maven.failsafe.plugin.version>2.22.2</maven.failsafe.plugin.version>
<maven.compiler.plugin.version>3.8.1</maven.compiler.plugin.version>
<build.helper.maven.plugin.version>3.0.0</build.helper.maven.plugin.version>
<jwiki.version>2.0.1</jwiki.version>
<jwiki.version>2.2.0</jwiki.version>
<guava.version>28.1-jre</guava.version>
<swagger.jersey2.jaxrs.version>1.5.21</swagger.jersey2.jaxrs.version>
<javax.activation.version>1.1.1</javax.activation.version>
<vavr.version>0.10.2</vavr.version>
</properties>

<repositories>
<!-- Needed for jwiki -->
<repository>
<id>jcenter</id>
<url>https://jcenter.bintray.com/</url>
</repository>
</repositories>

<dependencies>
<!-- Spring boot -->
<dependency>
Expand Down Expand Up @@ -91,15 +88,9 @@
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>

<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>${logstash.version}</version>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>

<!-- JSON -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package com.tibiawiki.serviceinterface;

import benjaminkomen.jwiki.core.NS;
import com.tibiawiki.domain.enums.InfoboxTemplate;
import com.tibiawiki.domain.repositories.ArticleRepository;
import org.json.JSONObject;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import static com.tibiawiki.process.RetrieveAny.CATEGORY_LISTS;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.mockito.Mockito.doReturn;

@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class LootStatisticsResourceIT {

private static final NS LOOT_NAMESPACE = new NS(112);
@Autowired
private TestRestTemplate restTemplate;

@MockBean
private ArticleRepository articleRepository; // don't instantiate this real class, but use a mock implementation

private static final String LOOT_AMAZON_TEXT = "{{Loot2\n" +
"|version=8.6\n" +
"|kills=22009\n" +
"|name=Amazon\n" +
"|Empty, times:253\n" +
"|Dagger, times:17626, amount:1, total:17626\n" +
"|Skull, times:17604, amount:1-2, total:26348\n" +
"|Gold Coin, times:8829, amount:1-20, total:93176\n" +
"|Brown Bread, times:6496, amount:1, total:6496\n" +
"|Sabre, times:5098, amount:1, total:5098\n" +
"|Girlish Hair Decoration, times:2179, amount:1, total:2179\n" +
"|Protective Charm, times:1154, amount:1, total:1154\n" +
"|Torch, times:223, amount:1, total:223\n" +
"|Crystal Necklace, times:56, amount:1, total:56\n" +
"|Small Ruby, times:27, amount:1, total:27\n" +
"}}\n";

@Test
void givenGetLootsNotExpanded_whenCorrectRequest_thenResponseIsOkAndContainsTwoLootNames() {
doReturn(Arrays.asList("foo", "bar")).when(articleRepository).getPageNamesFromCategory(InfoboxTemplate.LOOT.getCategoryName(), LOOT_NAMESPACE);

final ResponseEntity<List> result = restTemplate.getForEntity("/api/loot?expand=false", List.class);

assertThat(result.getStatusCode(), is(HttpStatus.OK));
assertThat(result.getBody().size(), is(2));
assertThat(result.getBody().get(0), is("foo"));
assertThat(result.getBody().get(1), is("bar"));
}

@Test
void givenGetLootsExpanded_whenCorrectRequest_thenResponseIsOkAndContainsOneLoot() {
doReturn(Collections.emptyList()).when(articleRepository).getPageNamesFromCategory(CATEGORY_LISTS);
doReturn(Collections.singletonList("Loot:Amazon")).when(articleRepository).getPageNamesFromCategory(InfoboxTemplate.LOOT.getCategoryName(), LOOT_NAMESPACE);
doReturn(Map.of("Loot:Amazon", LOOT_AMAZON_TEXT)).when(articleRepository).getArticlesFromCategory(Collections.singletonList("Loot:Amazon"));

final ResponseEntity<List> result = restTemplate.getForEntity("/api/loot?expand=true", List.class);

assertThat(result.getStatusCode(), is(HttpStatus.OK));
assertThat(result.getBody().size(), is(1));
assertThat(((Map) result.getBody().get(0)).get("kills"), is("22009"));
assertThat(((Map) result.getBody().get(0)).get("name"), is("Amazon"));
assertThat(((Map) result.getBody().get(0)).get("version"), is("8.6"));
assertThat(((Map) result.getBody().get(0)).get("pageName"), is("Loot:Amazon"));
}

@Test
void givenGetLootsByName_whenCorrectRequest_thenResponseIsOkAndContainsTheLoot() {
doReturn(LOOT_AMAZON_TEXT).when(articleRepository).getArticle("Loot_Statistics:Amazon");

final ResponseEntity<String> result = restTemplate.getForEntity("/api/loot/Amazon", String.class);
assertThat(result.getStatusCode(), is(HttpStatus.OK));

final JSONObject resultAsJSON = new JSONObject(result.getBody());
assertThat(resultAsJSON.get("kills"), is("22009"));
assertThat(resultAsJSON.get("name"), is("Amazon"));
assertThat(resultAsJSON.get("version"), is("8.6"));
assertThat(resultAsJSON.get("pageName"), is("Loot_Statistics:Amazon"));
}

@Test
void givenGetLootsByName_whenWrongRequest_thenResponseIsNotFound() {
doReturn(null).when(articleRepository).getArticle("Loot:Foobar");

final ResponseEntity<String> result = restTemplate.getForEntity("/api/loot/Foobar", String.class);
assertThat(result.getStatusCode(), is(HttpStatus.NOT_FOUND));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public enum InfoboxTemplate implements WikiTemplate {
HUNT("Hunt", "Hunting Places"),
ITEM("Item", "Items"),
KEY("Key", "Keys"),
LOOT("Loot2", "Loot Statistics"),
MISSILE("Missile", "Missiles"),
MOUNT("Mount", "Mounts"),
NPC("NPC", "NPCs"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class ArticleRepository {
private Wiki wiki;

public ArticleRepository() {
wiki = new Wiki(null, null, HttpUrl.parse(DEFAULT_WIKI_URI), null, null, true);
wiki = new Wiki.Builder().withApiEndpoint(HttpUrl.parse(DEFAULT_WIKI_URI)).build();

this.login(wiki);
}
Expand Down
18 changes: 1 addition & 17 deletions src/main/java/com/tibiawiki/process/RetrieveLoot.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@
import com.tibiawiki.domain.factories.ArticleFactory;
import com.tibiawiki.domain.factories.JsonFactory;
import com.tibiawiki.domain.repositories.ArticleRepository;
import org.jetbrains.annotations.NotNull;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
Expand All @@ -27,7 +24,7 @@ public RetrieveLoot(ArticleRepository articleRepository, ArticleFactory articleF
}

public List<String> getLootList() {
final List<String> lootStatisticsCategory = articleRepository.getPageNamesFromCategory(LOOT_STATISTICS_CATEGORY_NAME, makeLootNamespace());
final List<String> lootStatisticsCategory = articleRepository.getPageNamesFromCategory(LOOT_STATISTICS_CATEGORY_NAME, new NS(112));

return new ArrayList<>(lootStatisticsCategory);
}
Expand All @@ -54,17 +51,4 @@ private Optional<JSONObject> getLootArticleAsJSON(String pageName) {
.map(articleContent -> articleFactory.extractLootPartOfArticle(pageName, articleContent))
.map(lootPartOfArticle -> jsonFactory.convertLootPartOfArticleToJson(pageName, lootPartOfArticle));
}

// TODO replace this reflection hack with functionality in jwiki to construct a custom namespace
@NotNull
private NS makeLootNamespace() {
try {
final Constructor<?>[] constructors = NS.class.getDeclaredConstructors();
constructors[0].setAccessible(true);
Object namespace = constructors[0].newInstance(112);
return (NS) namespace;
} catch (IllegalAccessException | InvocationTargetException | InstantiationException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private void configureSwagger() {
BeanConfig beanConfig = new BeanConfig();
beanConfig.setConfigId("tibiawikiapi");
beanConfig.setTitle("TibiaWikiApi");
beanConfig.setVersion("1.7.1");
beanConfig.setVersion("1.7.2");
beanConfig.setContact("B. Komen");
beanConfig.setSchemes(new String[]{"https"});
beanConfig.setBasePath(this.apiPath); // location where dynamically created swagger.json is reachable
Expand Down
33 changes: 0 additions & 33 deletions src/main/resources/logback.xml

This file was deleted.

0 comments on commit eee3e92

Please sign in to comment.