Skip to content

Commit

Permalink
Assert j++ (#668)
Browse files Browse the repository at this point in the history
* rydde

* rydde rydde
  • Loading branch information
janolaveide authored Feb 15, 2021
1 parent 78df127 commit f43b802
Showing 24 changed files with 163 additions and 360 deletions.
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@
import no.nav.vedtak.feil.deklarasjon.ManglerTilgangFeil;
import no.nav.vedtak.feil.deklarasjon.TekniskFeil;

public abstract class AbstractFeilTestIT {
abstract class AbstractFeilTestIT {

private static List<Class<? extends DeklarerteFeil>> deklarerteFeil = FeilUtil.finnAlleDeklarerteFeil();

@@ -35,7 +35,7 @@ public abstract class AbstractFeilTestIT {
protected abstract List<String> getForventedePrefix();

@Test
public void skal_ha_at_alle_metoder_i_interface_med_deklarerte_feil_har_nøyaktig_én_feilannotering() throws Exception {
void skal_ha_at_alle_metoder_i_interface_med_deklarerte_feil_har_nøyaktig_én_feilannotering() throws Exception {
StringBuilder error = new StringBuilder();

for (Class<? extends DeklarerteFeil> deklareringsinterface : deklarerteFeil) {
@@ -60,7 +60,7 @@ public abstract class AbstractFeilTestIT {
}

@Test
public void skal_ha_at_feilkoder_følger_mønster_slik_at_de_er_gjenkjennbare_og_unike_på_tvers_av_applikasjoner() throws Exception {
void skal_ha_at_feilkoder_følger_mønster_slik_at_de_er_gjenkjennbare_og_unike_på_tvers_av_applikasjoner() throws Exception {
String prefixMønster = "(" + String.join("|", getForventedePrefix()) + ")";
String tallMønster = "\\d{6}";
Pattern mønster = Pattern.compile("^" + prefixMønster + "-" + tallMønster + "$");
@@ -81,7 +81,7 @@ public abstract class AbstractFeilTestIT {
}

@Test
public void skal_ha_unike_feilkoder_for_alle_deklarerte_feil() throws Exception {
void skal_ha_unike_feilkoder_for_alle_deklarerte_feil() throws Exception {
Map<String, List<InterfaceOgMetode>> feilPrFeilkode = new TreeMap<>();

for (Class<? extends DeklarerteFeil> deklareringsinterface : deklarerteFeil) {
@@ -117,7 +117,7 @@ public void skal_ha_unike_feilkoder_for_alle_deklarerte_feil() throws Exception
}

@Test
public void skal_ha_pattern_for_alle_parametre_som_er_oppgitt() throws Exception {
void skal_ha_pattern_for_alle_parametre_som_er_oppgitt() throws Exception {
StringBuilder error = new StringBuilder();
for (Class<? extends DeklarerteFeil> deklareringsinterface : deklarerteFeil) {
for (Method method : deklareringsinterface.getDeclaredMethods()) {
Original file line number Diff line number Diff line change
@@ -12,10 +12,10 @@
import no.nav.vedtak.feil.deklarasjon.ManglerTilgangFeil;
import no.nav.vedtak.feil.deklarasjon.TekniskFeil;

public class FeilFactoryTest {
class FeilFactoryTest {

@Test
public void skal_generere_teknisk_feil_basert_på_annoteringer_og_parametre_og_exception() throws Exception {
void skal_generere_teknisk_feil_basert_på_annoteringer_og_parametre_og_exception() throws Exception {
Exception cause = new IOException("Klarte ikke å mounte filsystem");
Feil feil = FeilFactory.create(TestFeil.class).kritiskOppstartsfeil("foo", cause);
assertThat(feil.getKode()).isEqualTo("TestFeil-1");
@@ -25,23 +25,23 @@ public class FeilFactoryTest {
}

@Test
public void skal_generere_integrasjonfeil_basert_på_annoteringer_og_parametre() throws Exception {
void skal_generere_integrasjonfeil_basert_på_annoteringer_og_parametre() throws Exception {
Feil feil = FeilFactory.create(TestFeil.class).tpsTimeout(30);
assertThat(feil.getKode()).isEqualTo("TestFeil-3");
assertThat(feil.getLogLevel()).isEqualTo(Level.WARN);
assertThat(feil.getFeilmelding()).isEqualTo("TPS svarte ikke (timeout=30 sekunder)");
}

@Test
public void skal_generere_ikkeTilgangFeil() throws Exception {
void skal_generere_ikkeTilgangFeil() throws Exception {
Feil feil = FeilFactory.create(TestFeil.class).ikkeTilgang();
assertThat(feil.getKode()).isEqualTo("TestFeil-6");
assertThat(feil.getLogLevel()).isEqualTo(Level.INFO);
assertThat(feil.getFeilmelding()).isEqualTo("Ikke lov");
}

@Test
public void skal_generere_funksjonell_feil_basert_på_annoteringer_og_parametre() throws Exception {
void skal_generere_funksjonell_feil_basert_på_annoteringer_og_parametre() throws Exception {
Feil feil = FeilFactory.create(TestFeil.class).manglerFødselsvilkår(1337);
assertThat(feil.getKode()).isEqualTo("TestFeil-2");
assertThat(feil.getLogLevel()).isEqualTo(Level.WARN);
@@ -52,7 +52,7 @@ public void skal_generere_ikkeTilgangFeil() throws Exception {
}

@Test
public void skal_ta_med_parameter_som_cause_når_det_bare_er_ett_parameter_og_det_er_en_exception() throws Exception {
void skal_ta_med_parameter_som_cause_når_det_bare_er_ett_parameter_og_det_er_en_exception() throws Exception {
IllegalArgumentException cause = new IllegalArgumentException("#5235");
Feil feil = FeilFactory.create(TestFeil.class).feilArgument(cause);
assertThat(feil.getCause()).isEqualTo(cause);
14 changes: 7 additions & 7 deletions felles/feil/src/test/java/no/nav/vedtak/feil/FeilTest.java
Original file line number Diff line number Diff line change
@@ -14,21 +14,21 @@
import no.nav.vedtak.exception.TekniskException;
import no.nav.vedtak.exception.VLException;

public class FeilTest {
class FeilTest {

private static final Logger logger = LoggerFactory.getLogger(FeilTest.class);
private static final String FEIL_KODE = "DUMMY_FEIL_KODE";

@Test
public void skal_kunne_konvertere_feil_til_exception() {
void skal_kunne_konvertere_feil_til_exception() {
Feil feil = new Feil(FEIL_KODE, "noe gikk galt", LogLevel.ERROR, TekniskException.class, null);
VLException exception = feil.toException();
assertThat(exception.getMessage()).isEqualTo(feil.toLogString());
assertThat(exception.getCause()).isNull();
}

@Test
public void skal_kunne_konvertere_feil_til_exception_og_ta_med_cause() {
void skal_kunne_konvertere_feil_til_exception_og_ta_med_cause() {
RuntimeException cause = new RuntimeException("Væææ!");
Feil feil = new Feil(FEIL_KODE, "noe gikk galt", LogLevel.ERROR, TekniskException.class, cause);
VLException exception = feil.toException();
@@ -38,7 +38,7 @@ public void skal_kunne_konvertere_feil_til_exception_og_ta_med_cause() {
}

@Test
public void skal_kunne_konvertere_funksjonell_feil_til_funksjonell_exception() {
void skal_kunne_konvertere_funksjonell_feil_til_funksjonell_exception() {
RuntimeException cause = new RuntimeException("Væææ!");
Feil feil = new FunksjonellFeil(FEIL_KODE, "funksjonellFeil", "test", LogLevel.WARN, FunksjonellException.class, cause);
assertThat(feil.getLogLevel()).isSameAs(Level.WARN);
@@ -51,14 +51,14 @@ public void skal_kunne_konvertere_funksjonell_feil_til_funksjonell_exception() {
}

@Test
public void skal_ikke_kunne_konvertere_feil_til_funksjonell_exception() {
void skal_ikke_kunne_konvertere_feil_til_funksjonell_exception() {
RuntimeException cause = new RuntimeException("Væææ!");
Feil feil = new Feil(FEIL_KODE, "funksjonellFeil", LogLevel.WARN, FunksjonellException.class, cause);
assertThrows(IllegalStateException.class, () -> feil.toException());
}

@Test
public void skal_støtte_feil_med_loglevel_INFO() {
void skal_støtte_feil_med_loglevel_INFO() {
RuntimeException cause = new RuntimeException("Væææ!");
Feil feil = new Feil(FEIL_KODE, "integrasjonFeil", LogLevel.INFO, IntegrasjonException.class, cause);
assertThat(feil.getLogLevel()).isSameAs(Level.INFO);
@@ -71,7 +71,7 @@ public void skal_ikke_kunne_konvertere_feil_til_funksjonell_exception() {
}

@Test
public void skal_kunne_konvertere_feil_til_mangler_tilgang_exception() {
void skal_kunne_konvertere_feil_til_mangler_tilgang_exception() {
RuntimeException cause = new RuntimeException("Væææ!");
Feil feil = new Feil(FEIL_KODE, "manglerTilgangFeil", LogLevel.ERROR, ManglerTilgangException.class, cause);
assertThat(feil.getLogLevel()).isSameAs(Level.ERROR);
27 changes: 14 additions & 13 deletions felles/feil/src/test/java/no/nav/vedtak/feil/FeilUtil.java
Original file line number Diff line number Diff line change
@@ -11,23 +11,24 @@
import no.nav.vedtak.feil.deklarasjon.ManglerTilgangFeil;
import no.nav.vedtak.feil.deklarasjon.TekniskFeil;

public class FeilUtil {
class FeilUtil {

@SuppressWarnings("unchecked")
public static List<Class<? extends DeklarerteFeil>> finnAlleDeklarerteFeil() {
static List<Class<? extends DeklarerteFeil>> finnAlleDeklarerteFeil() {
List<Class<? extends DeklarerteFeil>> deklarerteFeil = new ArrayList<>();
try (var scanResult = new ClassGraph().enableClassInfo().ignoreClassVisibility().scan();) {
scanResult.getSubclasses(DeklarerteFeil.class.getName()).forEach(c -> deklarerteFeil.add((Class<? extends DeklarerteFeil>) c.loadClass()));
scanResult.getSubclasses(DeklarerteFeil.class.getName())
.forEach(c -> deklarerteFeil.add((Class<? extends DeklarerteFeil>) c.loadClass()));
}

// ikke noen grunn til å ta med feil som er deklarert i test-scope i denne
// modulen
deklarerteFeil.remove(FeilFactoryTest.TestFeil.class);

return deklarerteFeil;
}

public static String feilkode(Method method) {
static String feilkode(Method method) {
TekniskFeil t = method.getAnnotation(TekniskFeil.class);
FunksjonellFeil f = method.getAnnotation(FunksjonellFeil.class);
IntegrasjonFeil i = method.getAnnotation(IntegrasjonFeil.class);
@@ -47,7 +48,7 @@ public static String feilkode(Method method) {
return null;
}

public static LogLevel logLevel(Method method) {
static LogLevel logLevel(Method method) {
TekniskFeil t = method.getAnnotation(TekniskFeil.class);
FunksjonellFeil f = method.getAnnotation(FunksjonellFeil.class);
IntegrasjonFeil i = method.getAnnotation(IntegrasjonFeil.class);
@@ -67,7 +68,7 @@ public static LogLevel logLevel(Method method) {
return null;
}

public static String type(Method method) {
static String type(Method method) {
TekniskFeil t = method.getAnnotation(TekniskFeil.class);
FunksjonellFeil f = method.getAnnotation(FunksjonellFeil.class);
IntegrasjonFeil i = method.getAnnotation(IntegrasjonFeil.class);
@@ -87,7 +88,7 @@ public static String type(Method method) {
return null;
}

public static String feilmelding(Method method) {
static String feilmelding(Method method) {
TekniskFeil t = method.getAnnotation(TekniskFeil.class);
FunksjonellFeil f = method.getAnnotation(FunksjonellFeil.class);
IntegrasjonFeil i = method.getAnnotation(IntegrasjonFeil.class);
@@ -107,25 +108,25 @@ public static String feilmelding(Method method) {
return null;
}

public static String løsningsforslag(Method method) {
static String løsningsforslag(Method method) {
FunksjonellFeil f = method.getAnnotation(FunksjonellFeil.class);
if (f != null) {
return f.løsningsforslag();
}
return null;
}

public static boolean harMedCause(Method method) {
static boolean harMedCause(Method method) {
return method.getParameterCount() > 0
&& Throwable.class.isAssignableFrom(method.getParameterTypes()[method.getParameterCount() - 1]);
&& Throwable.class.isAssignableFrom(method.getParameterTypes()[method.getParameterCount() - 1]);
}

public static int tellParametreUtenomCause(Method method) {
static int tellParametreUtenomCause(Method method) {
return harMedCause(method) ? method.getParameterCount() - 1 : method.getParameterCount();
}

@SuppressWarnings("unchecked")
public static <T extends Throwable> Class<T> deklarertCause(Method method) {
static <T extends Throwable> Class<T> deklarertCause(Method method) {
return (Class<T>) method.getParameters()[method.getParameterCount() - 1].getType();
}
}
Original file line number Diff line number Diff line change
@@ -4,15 +4,15 @@

import org.junit.jupiter.api.Test;

public class SporingsdataTest {
class SporingsdataTest {

@Test
public void skalInitialisereForMedGyldigeArgs() {
void skalInitialisereForMedGyldigeArgs() {
assertThat(Sporingsdata.opprett("login").keySet()).isEmpty();
}

@Test
public void skalHuskeIder() {
void skalHuskeIder() {
Sporingsdata sporingsdata = Sporingsdata.opprett("login");
sporingsdata.leggTilId(StandardSporingsloggId.AKTOR_ID, "1001");
assertThat(sporingsdata.getVerdi(StandardSporingsloggId.AKTOR_ID)).isEqualTo("1001");
Original file line number Diff line number Diff line change
@@ -4,26 +4,26 @@

import org.junit.jupiter.api.Test;

public class LoggerUtilsTest {
class LoggerUtilsTest {

@Test
public void removeLineBreaksSkalFjerneCR() {
void removeLineBreaksSkalFjerneCR() {

assertThat(LoggerUtils.removeLineBreaks("\r")).isEqualTo("");
assertThat(LoggerUtils.removeLineBreaks("\r")).isEmpty();
assertThat(LoggerUtils.removeLineBreaks("a\rb")).isEqualTo("ab");
assertThat(LoggerUtils.removeLineBreaks("\ra\rb")).isEqualTo("ab");
}

@Test
public void removeLineBreaksSkalFjerneLF() {
void removeLineBreaksSkalFjerneLF() {

assertThat(LoggerUtils.removeLineBreaks("\n")).isEqualTo("");
assertThat(LoggerUtils.removeLineBreaks("\n")).isEmpty();
assertThat(LoggerUtils.removeLineBreaks("a\nb")).isEqualTo("ab");
assertThat(LoggerUtils.removeLineBreaks("\na\n\nb")).isEqualTo("ab");
}

@Test
public void removeLineBreaksSkalTakleNull() {
void removeLineBreaksSkalTakleNull() {
assertThat(LoggerUtils.removeLineBreaks(null)).isNull();
}
}
Original file line number Diff line number Diff line change
@@ -9,17 +9,17 @@

import no.nav.vedtak.log.mdc.MdcExtendedLogContext;

public class MdcExtendedLogContextTest {
class MdcExtendedLogContextTest {

private MdcExtendedLogContext context = MdcExtendedLogContext.getContext("prosess");

@AfterEach
public void clear() {
void clear() {
MDC.clear();
}

@Test
public void skal_legge_til_ny_verdi() throws Exception {
void skal_legge_til_ny_verdi() throws Exception {

context.add("behandling", 1L);
assertThat(context.getFullText()).isEqualTo("prosess[behandling=1]");
@@ -32,15 +32,15 @@ public void skal_legge_til_ny_verdi() throws Exception {
}

@Test
public void skal_hente_key_part() throws Exception {
void skal_hente_key_part() throws Exception {
context.add("behandling", 1L);
context.add("fagsak", 2L);
context.add("prosess", 3L);
assertThat(context.getFullText()).isEqualTo("prosess[behandling=1;fagsak=2;prosess=3]");
}

@Test
public void skal_fjerne_verdi() throws Exception {
void skal_fjerne_verdi() throws Exception {
context.add("behandling", 1L);
context.add("fagsak", 2L);
context.add("prosess", 3L);
@@ -59,7 +59,7 @@ public void skal_fjerne_verdi() throws Exception {
}

@Test
public void skal_fjerne_verdi_i_midten() throws Exception {
void skal_fjerne_verdi_i_midten() throws Exception {
context.add("behandling", 1L);
context.add("fagsak", 2L);
context.add("prosess", 3L);
@@ -78,17 +78,17 @@ public void skal_fjerne_verdi_i_midten() throws Exception {
}

@Test
public void skal_sjekke_ugyldig_key_left_bracket() {
void skal_sjekke_ugyldig_key_left_bracket() {
assertThrows(IllegalArgumentException.class, () -> context.add("fdss[", 1L));
}

@Test
public void skal_sjekke_ugyldig_key_right_bracket() {
void skal_sjekke_ugyldig_key_right_bracket() {
assertThrows(IllegalArgumentException.class, () -> context.add("fd]ss", 1L));
}

@Test
public void skal_sjekke_ugyldig_key_semicolon() {
void skal_sjekke_ugyldig_key_semicolon() {
assertThrows(IllegalArgumentException.class, () -> context.add("fdss;", 1L));
}
}
10 changes: 8 additions & 2 deletions felles/util/src/main/java/no/nav/vedtak/util/Tuple.java
Original file line number Diff line number Diff line change
@@ -4,6 +4,10 @@ public class Tuple<X, Y> {
private final X element1;
private final Y element2;

public static <X, Y> Tuple<X, Y> of(X element1, Y element2) {
return new Tuple<>(element1, element2);
}

public Tuple(X element1, Y element2) {
java.util.Objects.requireNonNull(element1);
java.util.Objects.requireNonNull(element2);
@@ -22,8 +26,10 @@ public Y getElement2() {

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Tuple)) return false;
if (this == o)
return true;
if (!(o instanceof Tuple))
return false;

Tuple<?, ?> tuple = (Tuple<?, ?>) o;

Loading

0 comments on commit f43b802

Please # to comment.