Skip to content

Commit

Permalink
fix base64 encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo-catalano committed Apr 16, 2024
1 parent 7984efb commit 6086c28
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ public List<Encoding> getEncodings() {
.map(codificheRepository.findAll(), new TypeToken<List<Encoding>>() {}.getType());
}

private Base64.Encoder encoder = Base64.getEncoder().withoutPadding();


private String toXml(TplInformativaPSP element) {
try {
Expand All @@ -663,7 +663,7 @@ private String toXml(TplInformativaPSP element) {
marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, schemaInstance);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
marshaller.marshal(informativaPSP, baos);
return encoder.encodeToString(baos.toByteArray());
return Constants.ENCODER.encodeToString(baos.toByteArray());
} catch (Exception e) {
log.error("error creating TplInformativaPSP", e);
return e.toString();
Expand All @@ -680,7 +680,7 @@ private String toXml(CtListaInformativePSP element) {
marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, schemaInstance);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
marshaller.marshal(informativaPSP, baos);
return encoder.encodeToString(baos.toByteArray());
return Constants.ENCODER.encodeToString(baos.toByteArray());
} catch (Exception e) {
log.error("error creating CtListaInformativePSP", e);
return e.toString();
Expand All @@ -697,7 +697,7 @@ private String toXml(CtListaInformativeControparte element) {
marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, schemaInstance);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
marshaller.marshal(informativaPA, baos);
return encoder.encodeToString(baos.toByteArray());
return Constants.ENCODER.encodeToString(baos.toByteArray());
} catch (Exception e) {
log.error("error creating CtListaInformativeControparte", e);
return e.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

import lombok.experimental.UtilityClass;

import java.util.Base64;

@UtilityClass
public class Constants {

public static final Base64.Encoder ENCODER = Base64.getEncoder();

public static final String HEADER_REQUEST_ID = "X-Request-Id";
public static final String GZIP_JSON_V1 = "GZIP_JSON-v1";
public static final String FULL = "full";
Expand Down
17 changes: 13 additions & 4 deletions src/test/java/it/gov/pagopa/apiconfig/cache/VerifierTest.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
package it.gov.pagopa.apiconfig.cache;

import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
import static org.mockito.Mockito.when;

import it.gov.pagopa.apiconfig.cache.redis.RedisRepository;
import it.gov.pagopa.apiconfig.cache.repository.VerifierRepository;
import it.gov.pagopa.apiconfig.cache.service.VerifierService;
import java.util.List;
import it.gov.pagopa.apiconfig.cache.util.Constants;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.nio.charset.StandardCharsets;
import java.util.List;

import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
import static org.mockito.Mockito.when;

// @SpringBootTest(classes = Application.class)
@ExtendWith(MockitoExtension.class)
class VerifierTest {
Expand All @@ -33,4 +36,10 @@ void getCacheV1() throws Exception {
List<String> allData = verifierService.getPaV2();
assertThat(allData.containsAll(TestUtils.pastazioniV2));
}

@Test
void encoding() throws Exception {
String s = Constants.ENCODER.encodeToString("listacontroparti>".getBytes(StandardCharsets.UTF_8));
assertThat(s).isEqualTo("bGlzdGFjb250cm9wYXJ0aT4=");
}
}

0 comments on commit 6086c28

Please sign in to comment.