Skip to content

Commit

Permalink
[#25] Refactoring: Umbenennung zu AbfrageDaten
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Schubert committed Oct 13, 2019
1 parent 272efcc commit c126047
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ protected void loadPropertiesFromFile(final File configFile) {
this.configurationProperties = properties;
}

public File getConfigurationfile() {
protected File getConfigurationfile() {
return this.configurationFile;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
package com.github.funthomas424242.jenkinsmonitor.jenkins;

/*-
* #%L
* Jenkins Monitor
* %%
* Copyright (C) 2019 PIUG
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-3.0.html>.
* #L%
*/

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -8,19 +30,19 @@
import java.net.URL;
import java.util.Base64;

public class StatusAbfrageInformationen {
public class AbfrageDaten {

protected final Logger LOGGER = LoggerFactory.getLogger(StatusAbfrageInformationen.class);
protected final Logger LOGGER = LoggerFactory.getLogger(AbfrageDaten.class);

protected final URL jenkinsJobUrl;
protected final String userName;
protected final String password;

public StatusAbfrageInformationen(final URL jenkinsJobUrl) {
public AbfrageDaten(final URL jenkinsJobUrl) {
this(jenkinsJobUrl, null, null);
}

public StatusAbfrageInformationen(final URL jenkinsJobUrl, final String userName, String password) {
public AbfrageDaten(final URL jenkinsJobUrl, final String userName, String password) {
this.jenkinsJobUrl = jenkinsJobUrl;
this.userName = userName;
this.password = password;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Arrays;
import java.util.Base64;
import java.util.stream.Collectors;


Expand All @@ -52,26 +51,26 @@ public class JenkinsClient {
public static final String JSONKEY_RESULT = "result";


protected JobStatusBeschreibung getJobStatus(final StatusAbfrageInformationen statusAbfrageInformationen) throws IOException {
protected JobStatusBeschreibung getJobStatus(final AbfrageDaten abfrageDaten) throws IOException {

final JSONObject resultJSON = sendGetRequest(statusAbfrageInformationen);
final JSONObject resultJSON = sendGetRequest(abfrageDaten);
try {
final String jobName = resultJSON.getString(JSONKEY_FULL_DISPLAY_NAME);
final String jobStatus = resultJSON.getString(JSONKEY_RESULT);
return new JobStatusBeschreibung(jobName, JobStatus.valueOf(jobStatus), statusAbfrageInformationen.getJenkinsJobUrl());
return new JobStatusBeschreibung(jobName, JobStatus.valueOf(jobStatus), abfrageDaten.getJenkinsJobUrl());
} catch (JSONException ex) {
return new JobStatusBeschreibung(statusAbfrageInformationen.getJenkinsJobUrl().getPath(), JobStatus.OTHER, statusAbfrageInformationen.getJenkinsJobUrl());
return new JobStatusBeschreibung(abfrageDaten.getJenkinsJobUrl().getPath(), JobStatus.OTHER, abfrageDaten.getJenkinsJobUrl());
}
}

protected JSONObject sendGetRequest(final StatusAbfrageInformationen statusabfrageInformationen) throws IOException {
final URL statusAbfrageUrl = statusabfrageInformationen.getStatusAbfrageUrl();
protected JSONObject sendGetRequest(final AbfrageDaten statusabfrageDaten) throws IOException {
final URL statusAbfrageUrl = statusabfrageDaten.getStatusAbfrageUrl();

JSONObject resultJSON = null;
try (final CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
final HttpHost target = new HttpHost(statusAbfrageUrl.getHost(), statusAbfrageUrl.getPort(), statusAbfrageUrl.getProtocol());
final HttpGet httpGetRequest = new HttpGet(statusAbfrageUrl.getPath());
final String basicAuthToken = statusabfrageInformationen.getBasicAuthToken(statusabfrageInformationen.getPassword());
final String basicAuthToken = statusabfrageDaten.getBasicAuthToken(statusabfrageDaten.getPassword());
if( basicAuthToken != null && basicAuthToken.length() >1) {
httpGetRequest.setHeader("Authorization", "Basic " +basicAuthToken);
}
Expand Down Expand Up @@ -100,8 +99,8 @@ public JobStatusBeschreibung[] ladeJobsStatus(JobBeschreibung[] jobBeschreibunge
JobStatusBeschreibung returnValue = null;
try {
// TODO zugangsdaten
final StatusAbfrageInformationen statusAbfrageInformationen = new StatusAbfrageInformationen(beschreibung.getJobUrl(),null,null);
final JobStatusBeschreibung jobStatus = getJobStatus(statusAbfrageInformationen);
final AbfrageDaten abfrageDaten = new AbfrageDaten(beschreibung.getJobUrl(),null,null);
final JobStatusBeschreibung jobStatus = getJobStatus(abfrageDaten);
returnValue = new JobStatusBeschreibung(jobStatus.getJobName()
, jobStatus.getJobStatus()
, beschreibung.getJobUrl());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public JenkinsClientMock(JobStatus... jobStatus) {
}

@Override
protected JobStatusBeschreibung getJobStatus(final StatusAbfrageInformationen statusAbfrageInformationen) throws IOException {
return new JobStatusBeschreibung("xxx", jobStatus[jobNr++], statusAbfrageInformationen.getJenkinsJobUrl());
protected JobStatusBeschreibung getJobStatus(final AbfrageDaten abfrageDaten) throws IOException {
return new JobStatusBeschreibung("xxx", jobStatus[jobNr++], abfrageDaten.getJenkinsJobUrl());
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package com.github.funthomas424242.jenkinsmonitor.jenkins;

/*-
* #%L
* Jenkins Monitor
* %%
* Copyright (C) 2019 PIUG
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-3.0.html>.
* #L%
*/

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.Base64;

import static org.junit.jupiter.api.Assertions.*;

class AbfrageDatenTest {

@Test
@DisplayName("Prüfe das alle Daten als Properties enthalten sind")
public void containsAllProperties() {
final AbfrageDaten abfrageDaten = assertDoesNotThrow(() -> {
return new AbfrageDaten(
new URL("http://localhost:8080/"),
"Nutzername",
"Passwort"

);
});
assertNotNull(abfrageDaten.getStatusAbfrageUrl());
assertEquals("Nutzername", abfrageDaten.getUserName());
assertEquals("Passwort", abfrageDaten.getPassword());
assertNotNull(abfrageDaten.getBasicAuthToken(abfrageDaten.getPassword()));
}

@Test
@Disabled
@DisplayName("Das BasicAuth Token wird korrekt mit base64 kodiert")
public void getBase64CodedToken() {

final AbfrageDaten abfrageDaten = assertDoesNotThrow(() -> {
return new AbfrageDaten(
new URL("http://localhost:8080/"),
"Nutzername",
"Passwort"
);
});
try {
final String expectedToken = Base64.getEncoder().encodeToString("Nutzername:Passwort".getBytes("utf-8"));
assertNotNull(abfrageDaten.getBasicAuthToken(abfrageDaten.getPassword()));
} catch (UnsupportedEncodingException e) {
fail();
}
}

@Test
@DisplayName("Das BasicAuth Token wird bei gültigem Passwort zurückgegeben")
public void getTokenWithValidPassword() {

final AbfrageDaten abfrageDaten = assertDoesNotThrow(() -> {
return new AbfrageDaten(
new URL("http://localhost:8080/"),
"Nutzername",
"Passwort"
);
});
assertNotNull(abfrageDaten.getStatusAbfrageUrl());
assertEquals("Nutzername", abfrageDaten.getUserName());
assertEquals("Passwort", abfrageDaten.getPassword());
assertNotNull(abfrageDaten.getBasicAuthToken(abfrageDaten.getPassword()));
}

@Test
@DisplayName("Als BasicAuth Token wird null bei ungültigem Passwort zurückgegeben")
public void getNullWithInValidPassword() {

final AbfrageDaten abfrageDaten = assertDoesNotThrow(() -> {
return new AbfrageDaten(
new URL("http://localhost:8080/"),
"Nutzername",
"Passwort"
);
});
assertNull(abfrageDaten.getBasicAuthToken("blah blup falsch"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ protected void getStatusRed() {
final JenkinsClient requester = new JenkinsClient();
assumeTrue(requester != null);
final JobStatusBeschreibung jobStatusBeschreibung = assertDoesNotThrow(() -> {
final StatusAbfrageInformationen statusAbfrageInformationen = new StatusAbfrageInformationen(JOB_URL_MULTIBRANCH_JOB1_RED);
return requester.getJobStatus(statusAbfrageInformationen);
final AbfrageDaten abfrageDaten = new AbfrageDaten(JOB_URL_MULTIBRANCH_JOB1_RED);
return requester.getJobStatus(abfrageDaten);
});
assertNotNull(jobStatusBeschreibung);
assertNotNull(jobStatusBeschreibung.getJobStatus());
Expand All @@ -106,8 +106,8 @@ protected void getStatusGreen() {
final JenkinsClient requester = new JenkinsClient();
assumeTrue(requester != null);
final JobStatusBeschreibung jobStatusBeschreibung = assertDoesNotThrow(() -> {
final StatusAbfrageInformationen statusAbfrageInformationen = new StatusAbfrageInformationen(JOB_URL_MULTIBRANCH_JOB1_GREEN);
return requester.getJobStatus(statusAbfrageInformationen);
final AbfrageDaten abfrageDaten = new AbfrageDaten(JOB_URL_MULTIBRANCH_JOB1_GREEN);
return requester.getJobStatus(abfrageDaten);
});
assertNotNull(jobStatusBeschreibung);
assertNotNull(jobStatusBeschreibung.getJobStatus());
Expand All @@ -121,8 +121,8 @@ protected void getStatusYellow() {
final JenkinsClient requester = new JenkinsClient();
assumeTrue(requester != null);
final JobStatusBeschreibung jobStatusBeschreibung = assertDoesNotThrow(() -> {
final StatusAbfrageInformationen statusAbfrageInformationen = new StatusAbfrageInformationen(JOB_URL_MULTIBRANCH_JOB1_YELLOW);
return requester.getJobStatus(statusAbfrageInformationen);
final AbfrageDaten abfrageDaten = new AbfrageDaten(JOB_URL_MULTIBRANCH_JOB1_YELLOW);
return requester.getJobStatus(abfrageDaten);
});
assertNotNull(jobStatusBeschreibung);
assertNotNull(jobStatusBeschreibung.getJobStatus());
Expand All @@ -136,8 +136,8 @@ protected void getStatusGray() {
final JenkinsClient requester = new JenkinsClient();
assumeTrue(requester != null);
final JobStatusBeschreibung jobStatusBeschreibung = assertDoesNotThrow(() -> {
final StatusAbfrageInformationen statusAbfrageInformationen = new StatusAbfrageInformationen(JOB_URL_MULTIBRANCH_JOB1_GRAY);
return requester.getJobStatus(statusAbfrageInformationen);
final AbfrageDaten abfrageDaten = new AbfrageDaten(JOB_URL_MULTIBRANCH_JOB1_GRAY);
return requester.getJobStatus(abfrageDaten);
});
assertNotNull(jobStatusBeschreibung);
assertNotNull(jobStatusBeschreibung.getJobStatus());
Expand All @@ -151,7 +151,7 @@ protected void getStatusGray() {
protected void getValidJsonRed() {
final JenkinsClient requester = new JenkinsClient();
final JSONObject json = assertDoesNotThrow(() -> {
return requester.sendGetRequest(new StatusAbfrageInformationen(JOB_URL_MULTIBRANCH_JOB1_RED));
return requester.sendGetRequest(new AbfrageDaten(JOB_URL_MULTIBRANCH_JOB1_RED));
});
assertNotNull(json);
assertEquals("mypocketmod » master #2", json.get("fullDisplayName"));
Expand All @@ -163,7 +163,7 @@ protected void getValidJsonRed() {
protected void getValidJsonGreen() {
final JenkinsClient requester = new JenkinsClient();
final JSONObject json = assertDoesNotThrow(() -> {
return requester.sendGetRequest(new StatusAbfrageInformationen(JOB_URL_MULTIBRANCH_JOB1_GREEN,null,null));
return requester.sendGetRequest(new AbfrageDaten(JOB_URL_MULTIBRANCH_JOB1_GREEN,null,null));
});
assertNotNull(json);
assertEquals("mypocketmod » master #2", json.get("fullDisplayName"));
Expand All @@ -175,7 +175,7 @@ protected void getValidJsonGreen() {
protected void getValidJsonYellow() {
final JenkinsClient requester = new JenkinsClient();
final JSONObject json = assertDoesNotThrow(() -> {
return requester.sendGetRequest(new StatusAbfrageInformationen(JOB_URL_MULTIBRANCH_JOB1_YELLOW,null,null));
return requester.sendGetRequest(new AbfrageDaten(JOB_URL_MULTIBRANCH_JOB1_YELLOW,null,null));
});
assertNotNull(json);
assertEquals("mypocketmod » master #2", json.get("fullDisplayName"));
Expand All @@ -187,7 +187,7 @@ protected void getValidJsonYellow() {
protected void getValidJsonGray() {
final JenkinsClient requester = new JenkinsClient();
final JSONObject json = assertDoesNotThrow(() -> {
return requester.sendGetRequest(new StatusAbfrageInformationen(JOB_URL_MULTIBRANCH_JOB1_GRAY,null,null));
return requester.sendGetRequest(new AbfrageDaten(JOB_URL_MULTIBRANCH_JOB1_GRAY,null,null));
});
assertNotNull(json);
assertTrue(json.isEmpty());
Expand All @@ -199,7 +199,7 @@ void checkLadeStatusWithException() {

final JenkinsClient requester = new JenkinsClient() {
@Override
protected JobStatusBeschreibung getJobStatus(final StatusAbfrageInformationen statusAbfrageInformationen) throws IOException {
protected JobStatusBeschreibung getJobStatus(final AbfrageDaten statusAbfrageInformationen) throws IOException {
throw new IOException();
}
};
Expand All @@ -221,7 +221,7 @@ void checkLadeOneJobStatusFailure() {

final JenkinsClient requester = new JenkinsClient() {
@Override
protected JobStatusBeschreibung getJobStatus(final StatusAbfrageInformationen statusAbfrageInformationen) throws IOException {
protected JobStatusBeschreibung getJobStatus(final AbfrageDaten statusAbfrageInformationen) throws IOException {
return new JobStatusBeschreibung("hallo", JobStatus.FAILURE, statusAbfrageInformationen.getJenkinsJobUrl());
}
};
Expand All @@ -243,7 +243,7 @@ void checkLadeTwoJobStatusSUCCESS_UNSTABLE() {
int counter = 0;

@Override
protected JobStatusBeschreibung getJobStatus(final StatusAbfrageInformationen statusAbfrageInformationen) throws IOException {
protected JobStatusBeschreibung getJobStatus(final AbfrageDaten statusAbfrageInformationen) throws IOException {
if (counter == 0) {
counter++;
return new JobStatusBeschreibung("hallo", JobStatus.FAILURE, statusAbfrageInformationen.getJenkinsJobUrl());
Expand Down
Loading

0 comments on commit c126047

Please sign in to comment.