Skip to content

Commit

Permalink
Ajuste na pipeline para enviar o código para análise do sonarqube
Browse files Browse the repository at this point in the history
  • Loading branch information
algarves committed Mar 14, 2022
1 parent 1a8463a commit ecade3d
Show file tree
Hide file tree
Showing 24 changed files with 180 additions and 117 deletions.
14 changes: 8 additions & 6 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
# This workflow will build a Java project with Gradle
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-gradle

name: Java CI with Gradle
name: Build and Test

on:
push:
branches: [ master ]
branches:
- master
pull_request:
branches: [ master ]
types: [opened, synchronize, reopened]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Set up JDK 11
Expand All @@ -31,7 +30,10 @@ jobs:
# run: ./gradlew check

- name: Generate test report with Gradle
run: ./gradlew jacocoTestReport sonarqube -Dsonar.login=${{ secrets.SONAR_KEY }} -Dsonar.qualitygate.wait=true
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
run: ./gradlew test integrationTest jacocoTestReport sonarqube -Dsonar.qualitygate.wait=true -Dsonar.verbose=true

# - name: Upload a Build Artifact
# run: ./gradlew publish
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![Build Status](https://travis-ci.com/algarves/loteria-crawlers.svg?branch=master)](https://travis-ci.com/algarves/loteria-crawlers)
[![Java CI with Gradle](https://github.com/algarves/loteria-crawlers/actions/workflows/gradle.yml/badge.svg)](https://github.com/algarves/loteria-crawlers/actions/workflows/gradle.yml)
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=algarves_loteria-crawlers&metric=alert_status)](https://sonarcloud.io/dashboard?id=algarves_loteria-crawlers)
[![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](https://github.com/algarves/loteria-crawlers/blob/master/LICENSE)

Expand Down
18 changes: 15 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
id 'java-library'
id 'maven-publish'
id 'jacoco'
id 'org.sonarqube' version '3.2.0'
id 'org.sonarqube' version '3.3'
id 'com.adarshr.test-logger' version '3.0.0'
}

Expand Down Expand Up @@ -80,8 +80,20 @@ publishing {
}
}

test {
useJUnitPlatform()
tasks.withType(Test) {
description = 'Runs the unit tests.'
group = 'validation'
useJUnitPlatform({
excludeTags 'integrationTest'
})
}

task integrationTest(type: Test) {
description = 'Runs the integration tests.'
group = 'verification'
useJUnitPlatform {
includeTags 'integrationTest'
}
}

jacoco {
Expand Down
2 changes: 0 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
vertical=LoteriaCrawlersServices

sonarQubeProjectKey=algarves_loteria-crawlers
15 changes: 6 additions & 9 deletions sonar.gradle
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
apply plugin: "org.sonarqube"

def coverageExclusions = [
"**/test/java/*.java"
]

sonarqube {
properties {
property "sonar.coverage.exclusions", coverageExclusions.join(",")
property "sonar.java.binaries", "$build/classes/java/main/**/*.class"
property "sonar.java.libraries", "**.jar"
property "sonar.projectName", rootProject.name
property "sonar.organization", sonarQubeProjectKey
property "sonar.organization", "algarves"
property "sonar.projectKey", sonarQubeProjectKey
property "sonar.host.url", 'https://sonarcloud.io/'
property "sonar.host.url", 'https://sonarcloud.io'
property "sonar.java.coveragePlugin", "jacoco"
property "sonar.tests", "src/test"
property "sonar.java.test.binaries", "build/classes/java/test"
property "sonar.coverage.jacoco.xmlReportPaths", "build/reports/jacoco/test/jacocoTestReport.xml"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.ueby.loteria.crawlers.exception;

/**
* @author Algarves, Khristian
*/
public class JsonParseErrorException extends RuntimeException {

static final long serialVersionUID = 1L;

public JsonParseErrorException(String message) {
super(message);
}

public JsonParseErrorException(String message, Throwable throwable) {
super(message, throwable);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import com.ueby.loteria.crawlers.game.CaixaGameResult;
import com.ueby.loteria.crawlers.util.JsonUtil;
import com.ueby.loteria.crawlers.util.RandomUtil;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.concurrent.TimeUnit;
import lombok.extern.log4j.Log4j2;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
Expand All @@ -17,30 +20,23 @@
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.concurrent.TimeUnit;

/**
* @author Algarves, Khristian
*/
@Log4j2
public class CaixaWebCrawlerService extends AbstractCrawlerService {

private static final String BASE_URL = "http://loterias.caixa.gov.br";
private static final String HOME_URL_PATH = "/wps/portal/loterias";

private static final String HTML_ELEMENT_FIRST = "com.ibm.lotus.NavStateUrl";
private static final String HTML_ELEMENT_SECOND = "input[type=hidden][id=urlBuscarResultado]";
private static final String HTML_DOCTYPE = "<!DOCTYPE";

private static final int MAX_ATTEMPTS = 5;
private int attempts = 1;

private Boolean firstRequest = true;
private boolean firstRequest = true;

CaixaGameType caixaGameType;
CaixaCrawlerStub caixaCrawlerStub;
final CaixaGameType caixaGameType;
final CaixaCrawlerStub caixaCrawlerStub;

public CaixaWebCrawlerService(CaixaGameType caixaGameType) {
this.caixaCrawlerStub = new CaixaCrawlerStub();
Expand Down Expand Up @@ -85,14 +81,14 @@ private void recall() throws InterruptedException {
}

private long getSleep() {
return TimeUnit.SECONDS.toMillis(RandomUtil.getRandomNumInRange(1, 30));
return TimeUnit.SECONDS.toMillis(RandomUtil.getRandomNumInRange(1, 5));
}

private void call() {
try {
final String url = getUrl();

log.debug("Request URL '{}' - {}", this.caixaGameType, url);
log.debug("Request URL '{}' - {}", caixaGameType, url);

Response response = get(url);

Expand Down Expand Up @@ -133,25 +129,28 @@ private void checkContent(String html) throws CaixaWebCrawlerException {
}

if (!html.startsWith(HTML_DOCTYPE)) {
this.caixaCrawlerStub.setHtmlContent(html);
caixaCrawlerStub.setHtmlContent(html);
}

firstRequest = false;
}

private void checkContentFirstRequest(Element link, Element input) throws CaixaWebCrawlerException {
private void checkContentFirstRequest(Element link, Element input)
throws CaixaWebCrawlerException {
if (link != null) {
String linkHref = link.attr("href");
this.caixaCrawlerStub.setLinkHref(linkHref);
caixaCrawlerStub.setLinkHref(linkHref);
} else {
throw new CaixaWebCrawlerException("Oops! I couldn't find the first element '" + HTML_ELEMENT_FIRST + "'.");
throw new CaixaWebCrawlerException(
"Oops! I couldn't find the first element '" + HTML_ELEMENT_FIRST + "'.");
}

if (input != null) {
String inputValue = input.attr("value");
this.caixaCrawlerStub.setUrlBuscarResultado(inputValue);
caixaCrawlerStub.setUrlBuscarResultado(inputValue);
} else {
throw new CaixaWebCrawlerException("Oops! I couldn't find the second element '" + HTML_ELEMENT_SECOND + "'.");
throw new CaixaWebCrawlerException(
"Oops! I couldn't find the second element '" + HTML_ELEMENT_SECOND + "'.");
}
}

Expand All @@ -160,7 +159,8 @@ private String getUrl() throws URISyntaxException {

if (caixaCrawlerStub.getLinkHref() != null
&& caixaCrawlerStub.getUrlBuscarResultado() != null) {
uriBuilder.setPath(this.caixaCrawlerStub.getLinkHref() + this.caixaCrawlerStub.getUrlBuscarResultado());
uriBuilder.setPath(
caixaCrawlerStub.getLinkHref() + caixaCrawlerStub.getUrlBuscarResultado());
uriBuilder.addParameter("timestampAjax", Long.toString(System.currentTimeMillis()));

if (caixaCrawlerStub.getConcurso() != null) {
Expand All @@ -172,10 +172,11 @@ private String getUrl() throws URISyntaxException {
}

private URIBuilder getUriBuilder() throws URISyntaxException {
StringBuilder url = new StringBuilder(BASE_URL);
StringBuilder url = new StringBuilder("http://loterias.caixa.gov.br");

if (firstRequest) {
url.append(HOME_URL_PATH + this.caixaGameType.getPath());
url.append("/wps/portal/loterias");
url.append(caixaGameType.getPath());
}

return new URIBuilder(url.toString());
Expand All @@ -185,4 +186,4 @@ public CaixaGameResult getMatchResult() {
return JsonUtil.fromJson(caixaCrawlerStub.getHtmlContent(), CaixaGameResult.class);
}

}
}
14 changes: 7 additions & 7 deletions src/main/java/com/ueby/loteria/crawlers/util/JsonUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
import com.ueby.loteria.crawlers.exception.JsonParseErrorException;
import java.io.IOException;
import java.util.Objects;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.log4j.Log4j2;

import java.io.IOException;
import java.util.Objects;

/**
* @author Algarves, Khristian
*/
Expand All @@ -39,8 +39,8 @@ public static String toJson(final Object data) {
try {
return mapper.writeValueAsString(data);
} catch (Exception e) {
log.error(e.getLocalizedMessage(), e);
throw new RuntimeException(e);
log.error(e.getMessage(), e);
throw new JsonParseErrorException(e.getMessage(), e);
}
}

Expand All @@ -50,10 +50,10 @@ public static <A> A fromJson(String json, Class<A> clazz) {
return mapper.readValue(json, clazz);
} catch (JsonParseException e) {
log.error(e.getLocalizedMessage(), e);
throw new RuntimeException(e.getLocalizedMessage());
throw new JsonParseErrorException(e.getMessage());
} catch (Exception e) {
log.error(e.getLocalizedMessage(), e);
throw new RuntimeException(e.getLocalizedMessage());
throw new JsonParseErrorException(e.getMessage());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
/**
* @author Algarves, Khristian
*/
public class CaixaCrawlerStubTest {
class CaixaCrawlerStubTest {

@DisplayName("Getters and Setters of CaixaCrawlerStub")
@Test
public void shouldNewInstanceOfCaixaCrawlerStub() {
void shouldNewInstanceOfCaixaCrawlerStub() {
CaixaCrawlerStub caixaCrawlerStub = new CaixaCrawlerStub.Builder()
.withLinkHref("LinkHref")
.withUrlBuscarResultado("UrlBuscarResultado")
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/com/ueby/loteria/crawlers/IntegrationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.ueby.loteria.crawlers;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.junit.jupiter.api.Tag;

@Tag("integrationTest")
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface IntegrationTest {

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
/**
* @author Algarves, Khristian
*/
public class CaixaWebCrawlerServiceExceptionTest {
class CaixaWebCrawlerServiceExceptionTest {

@Test
public void shouldCaixaWebCrawlerExceptionTest() {
void shouldCaixaWebCrawlerExceptionTest() {
Exception exception = Assertions.assertThrows(
CaixaWebCrawlerException.class,
() -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
package com.ueby.loteria.crawlers.game;

import com.ueby.loteria.crawlers.CaixaGameType;
import com.ueby.loteria.crawlers.IntegrationTest;
import com.ueby.loteria.crawlers.service.CaixaWebCrawlerService;
import org.junit.jupiter.api.*;

/**
* @author Algarves, Khristian
*/
@IntegrationTest
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class DiaDeSorteWebCrawlerTest {
class DiaDeSorteWebCrawlerTest {

private CaixaWebCrawlerService crawler;
private final CaixaGameType caixaGameType = CaixaGameType.DIA_DE_SORTE;

@BeforeAll
public void setUp() {
void setUp() {
crawler = new CaixaWebCrawlerService(caixaGameType);
}

@DisplayName("DiaDeSorte - Request by last contest")
@Test
public void shouldDiaDeSorteWebCrawlerByLastContest() {
void shouldDiaDeSorteWebCrawlerByLastContest() {
crawler.byLastContest();

CaixaGameResult caixaGameResult = crawler.getMatchResult();
Expand All @@ -31,7 +33,7 @@ public void shouldDiaDeSorteWebCrawlerByLastContest() {

@DisplayName("DiaDeSorte - Request by contest number")
@Test
public void shouldDiaDeSorteWebCrawlerByContestNumber() {
void shouldDiaDeSorteWebCrawlerByContestNumber() {
final Integer contest = 1;

crawler.byContestNumber(contest);
Expand All @@ -42,4 +44,4 @@ public void shouldDiaDeSorteWebCrawlerByContestNumber() {
Assertions.assertEquals(caixaGameType, caixaGameResult.getTipoJogo());
}

}
}
Loading

0 comments on commit ecade3d

Please sign in to comment.