Skip to content

Commit

Permalink
Merge pull request #154 from Infosys/review-changes
Browse files Browse the repository at this point in the history
[INJICERT-657] Added review changed related to rendering template names and docker
  • Loading branch information
vishwa-vyom authored Dec 17, 2024
2 parents a2fa2f8 + 821d46f commit 880c8b1
Show file tree
Hide file tree
Showing 23 changed files with 91 additions and 446 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

import io.mosip.certify.core.constants.ErrorConstants;

public class TemplateException extends RuntimeException {
public class RenderingTemplateException extends RuntimeException {
private String errorCode;

public TemplateException() {
public RenderingTemplateException() {
super(ErrorConstants.UNKNOWN_ERROR);
this.errorCode = ErrorConstants.UNKNOWN_ERROR;
}

public TemplateException(String errorCode) {
public RenderingTemplateException(String errorCode) {
super(errorCode);
this.errorCode = errorCode;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.mosip.certify.api.dto;

import lombok.Data;

import java.time.LocalDateTime;

@Data
public class RenderingTemplateDTO {
private String id;
private String template;
private LocalDateTime createdTimes;
private LocalDateTime updatedTimes;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import io.mosip.certify.core.exception.CertifyException;
import io.mosip.certify.core.exception.InvalidRequestException;
import io.mosip.certify.core.exception.NotAuthenticatedException;
import io.mosip.certify.core.exception.TemplateException;
import io.mosip.certify.core.exception.RenderingTemplateException;
import io.mosip.certify.core.util.CommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.TypeMismatchException;
Expand Down Expand Up @@ -131,7 +131,7 @@ private ResponseEntity<ResponseWrapper> handleInternalControllerException(Except
String errorCode = ((CertifyException) ex).getErrorCode();
return new ResponseEntity<ResponseWrapper>(getResponseWrapper(errorCode, getMessage(errorCode)), HttpStatus.OK);
}
if(ex instanceof TemplateException) {
if(ex instanceof RenderingTemplateException) {
return new ResponseEntity<>(getResponseWrapper(INVALID_REQUEST, ex.getMessage()) ,HttpStatus.NOT_FOUND);
}
if(ex instanceof AuthenticationCredentialsNotFoundException) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
*/
package io.mosip.certify.controller;

import io.mosip.certify.api.dto.RenderingTemplateDTO;
import io.mosip.certify.services.entity.RenderingTemplate;
import io.mosip.certify.core.exception.TemplateException;
import io.mosip.certify.core.exception.RenderingTemplateException;
import io.mosip.certify.services.spi.RenderingTemplateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -32,12 +33,12 @@ public class RenderingTemplateController {
RenderingTemplateService renderingTemplateService;

@GetMapping("/rendering-template/{id}")
public ResponseEntity<String> serveSvgTemplate(@PathVariable String id) throws TemplateException {
RenderingTemplate template = renderingTemplateService.getSvgTemplate(id);
public ResponseEntity<String> serveSvgTemplate(@PathVariable String id) throws RenderingTemplateException {
RenderingTemplateDTO template = renderingTemplateService.getSvgTemplate(id);
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_TYPE, "image/svg+xml")
.cacheControl(CacheControl.maxAge(maxAgeDays, TimeUnit.DAYS).cachePublic())
.lastModified(template.getUpdatedtimes().atZone(ZoneId.systemDefault()).toInstant())
.lastModified(template.getUpdatedTimes().atZone(ZoneId.systemDefault()).toInstant())
.body(template.getTemplate());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public class CertifyIssuanceServiceImpl implements VCIssuanceService {
private String issuerURI;

@Value("${mosip.certify.data-provider-plugin.rendering-template-id:}")
private String svgTemplateId;
private String renderTemplateId;

@Autowired
private ProofValidatorFactory proofValidatorFactory;
Expand Down Expand Up @@ -285,8 +285,8 @@ private VCResult<?> getVerifiableCredential(CredentialRequest credentialRequest,
Map<String, Object> templateParams = new HashMap<>();
templateParams.put(VelocityTemplatingConstants.TEMPLATE_NAME, CredentialUtils.getTemplateName(vcRequestDto));
templateParams.put(VelocityTemplatingConstants.ISSUER_URI, issuerURI);
if (!StringUtils.isEmpty(svgTemplateId)) {
templateParams.put(VelocityTemplatingConstants.SVG_TEMPLATE, svgTemplateId);
if (!StringUtils.isEmpty(renderTemplateId)) {
templateParams.put(VelocityTemplatingConstants.SVG_TEMPLATE, renderTemplateId);
}
String unSignedVC = vcFormatter.format(jsonObject, templateParams);
vcResult = vcSigner.attachSignature(unSignedVC);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import io.ipfs.multibase.Multibase;

public class RenderUtils {
public class CredentialUtils {
/**
* Generate SVG digest for @param svg image as per spec.
* ref: https://w3c-ccg.github.io/vc-render-method/#svgrenderingtemplate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
*/
package io.mosip.certify.services;

import io.mosip.certify.api.dto.RenderingTemplateDTO;
import io.mosip.certify.core.constants.ErrorConstants;
import io.mosip.certify.services.entity.RenderingTemplate;
import io.mosip.certify.core.exception.TemplateException;
import io.mosip.certify.core.exception.RenderingTemplateException;
import io.mosip.certify.services.repository.RenderingTemplateRepository;
import io.mosip.certify.services.spi.RenderingTemplateService;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -24,11 +25,16 @@ public class RenderingTemplateServiceImpl implements RenderingTemplateService {


@Override
public RenderingTemplate getSvgTemplate(String id) {
public RenderingTemplateDTO getSvgTemplate(String id) {
Optional<RenderingTemplate> optional = svgRenderTemplateRepository.findById(id);
RenderingTemplate svgRenderTemplate = optional.orElseThrow(() -> new TemplateException(ErrorConstants.INVALID_TEMPLATE_ID));
RenderingTemplate renderingTemplate = optional.orElseThrow(() -> new RenderingTemplateException(ErrorConstants.INVALID_TEMPLATE_ID));
RenderingTemplateDTO renderingTemplateDTO = new RenderingTemplateDTO();
renderingTemplateDTO.setId(renderingTemplate.getId());
renderingTemplateDTO.setTemplate(renderingTemplate.getTemplate());
renderingTemplateDTO.setCreatedTimes(renderingTemplate.getCreatedtimes());
renderingTemplateDTO.setUpdatedTimes(renderingTemplate.getUpdatedtimes());

return svgRenderTemplate;
return renderingTemplateDTO;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
*/
package io.mosip.certify.services.spi;

import io.mosip.certify.services.entity.RenderingTemplate;
import io.mosip.certify.api.dto.RenderingTemplateDTO;


public interface RenderingTemplateService {
RenderingTemplate getSvgTemplate(String id);
RenderingTemplateDTO getSvgTemplate(String id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
import io.mosip.certify.core.constants.Constants;
import io.mosip.certify.core.constants.VCDM2Constants;
import io.mosip.certify.core.constants.VCDMConstants;
import io.mosip.certify.core.exception.TemplateException;
import io.mosip.certify.core.exception.RenderingTemplateException;
import io.mosip.certify.services.repository.TemplateRepository;
import io.mosip.certify.services.spi.RenderingTemplateService;
import io.mosip.certify.services.RenderUtils;
import io.mosip.certify.services.CredentialUtils;
import jakarta.annotation.PostConstruct;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -83,7 +83,7 @@ public String format(JSONObject valueMap, Map<String, Object> templateSettings)
String template = getTemplate(templateName);
if (template == null) {
log.error("Template {} not found", templateName);
throw new TemplateException("Expected template not found");
throw new RenderingTemplateException("Expected template not found");
}
String issuer = templateSettings.get(ISSUER_URI).toString();
StringWriter writer = new StringWriter();
Expand Down Expand Up @@ -117,9 +117,9 @@ public String format(JSONObject valueMap, Map<String, Object> templateSettings)
if (templateSettings.containsKey(SVG_TEMPLATE) && templateName.contains(VCDM2Constants.URL)) {
try {
finalTemplate.put("_renderMethodSVGdigest",
RenderUtils.getDigestMultibase(renderingTemplateService.getSvgTemplate(
CredentialUtils.getDigestMultibase(renderingTemplateService.getSvgTemplate(
(String) templateSettings.get(SVG_TEMPLATE)).getTemplate()));
} catch (TemplateException e) {
} catch (RenderingTemplateException e) {
log.error("SVG Template: " + templateSettings.get(SVG_TEMPLATE) + " not available in DB", e);
}
}
Expand Down
6 changes: 0 additions & 6 deletions certify-service/src/main/resources/svg-template.json

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package io.mosip.certify.controller;

import io.mosip.certify.api.dto.RenderingTemplateDTO;
import io.mosip.certify.core.constants.ErrorConstants;
import io.mosip.certify.core.dto.ParsedAccessToken;
import io.mosip.certify.services.entity.RenderingTemplate;
import io.mosip.certify.core.exception.TemplateException;
import io.mosip.certify.core.exception.RenderingTemplateException;
import io.mosip.certify.services.spi.RenderingTemplateService;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -32,32 +33,32 @@ public class RenderingTemplateControllerTest {

@Test
public void getSvgTemplate_withValidId_thenPass() throws Exception {
RenderingTemplate renderingTemplate = new RenderingTemplate();
renderingTemplate.setId("fake-id");
RenderingTemplateDTO renderingTemplateDTO = new RenderingTemplateDTO();
renderingTemplateDTO.setId("fake-id");
String template = """
<svg xmlns=\\"http://www.w3.org/2000/svg\\" width=\\"200\\" height=\\"200\\">
<rect width=\\"200\\" height=\\"200\\" fill=\\"#ff6347\\"/>
<text x=\\"100\\" y=\\"100\\" font-size=\\"30\\" text-anchor=\\"middle\\" fill=\\"white\\">
Hello, SVG!
</text></svg>
""";
renderingTemplate.setTemplate(template);
renderingTemplateDTO.setTemplate(template);
LocalDateTime date = LocalDateTime.now();
renderingTemplate.setCreatedtimes(date);
renderingTemplate.setUpdatedtimes(date);
renderingTemplateDTO.setCreatedTimes(date);
renderingTemplateDTO.setUpdatedTimes(date);

Mockito.when(renderingTemplateService.getSvgTemplate(Mockito.any())).thenReturn(renderingTemplate);
Mockito.when(renderingTemplateService.getSvgTemplate(Mockito.any())).thenReturn(renderingTemplateDTO);

mockMvc.perform(get("/public/rendering-template/fake-id"))
.andExpect(status().isOk())
.andExpect(content().string(renderingTemplate.getTemplate()))
.andExpect(content().string(renderingTemplateDTO.getTemplate()))
.andExpect(content().contentType("image/svg+xml"))
.andExpect(header().string("Cache-Control", "max-age=86400, public"));
}

@Test
public void getSvgTemplate_withInValidId_thenFail() throws Exception {
TemplateException templateException = new TemplateException(ErrorConstants.INVALID_TEMPLATE_ID);
RenderingTemplateException templateException = new RenderingTemplateException(ErrorConstants.INVALID_TEMPLATE_ID);
Mockito.when(renderingTemplateService.getSvgTemplate("fake-id")).thenThrow(templateException);

mockMvc.perform(get("/public/rendering-template/fake-id"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ void getDigestMultibase() {
String svg = """
<svg viewBox=".5 .5 3 4" fill="none" stroke="#20b2a" stroke-linecap="round"> <path d=" M1 4h-.001 V1h2v.001 M1 2.6 h1v.001"/> </svg>
""";
String actual = RenderUtils.getDigestMultibase(svg);
String actual = CredentialUtils.getDigestMultibase(svg);
String expected = "z4po9QkJj1fhMt6cxHSnDnAUat4PEVrerUGGsPHLxJnK5";
assertEquals(expected, actual);
}
Expand Down
Loading

0 comments on commit 880c8b1

Please sign in to comment.