Skip to content

Commit

Permalink
Merge pull request #1229 from Bolo89/refacto-common-cypress
Browse files Browse the repository at this point in the history
Add cypress in common client
  • Loading branch information
pascalgrimaud authored Apr 8, 2022
2 parents 42d8a8d + 5026c67 commit 349493c
Show file tree
Hide file tree
Showing 44 changed files with 817 additions and 343 deletions.
65 changes: 63 additions & 2 deletions src/main/java/tech/jhipster/lite/common/domain/JsonUtils.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package tech.jhipster.lite.common.domain;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.PrettyPrinter;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeType;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.io.IOException;
import tech.jhipster.lite.error.domain.GeneratorException;

public class JsonUtils {

public static final String CARRIAGE_RETURN = System.lineSeparator();

private static ObjectMapper objectMapper = null;
private static PrettyPrinter customPrettyPrinter = null;

private JsonUtils() {
// Cannot be instantiated
Expand All @@ -17,6 +25,59 @@ public static ObjectMapper getObjectMapper() {
return objectMapper;
}
objectMapper = new ObjectMapper();
customPrettyPrinter = new CustomPrettyPrinter();
return objectMapper;
}

public static String addValueInArray(String fieldName, String value, String jsonContent) {
ObjectNode rootNode;
ObjectMapper objectMapper = getObjectMapper();
try {
rootNode = objectMapper.readValue(jsonContent, ObjectNode.class);
} catch (JsonProcessingException e) {
throw new GeneratorException("Cannot read json content : " + e.getMessage());
}

ArrayNode arrayNode;
if (rootNode.has(fieldName)) {
if (rootNode.get(fieldName).getNodeType() == JsonNodeType.ARRAY) {
arrayNode = ((ArrayNode) rootNode.get(fieldName));
} else {
throw new GeneratorException("Cannot add value in field: is not an array");
}
} else {
arrayNode = objectMapper.createArrayNode();
rootNode.set(fieldName, arrayNode);
}
arrayNode.add(value);

try {
return objectMapper.writer(customPrettyPrinter).writeValueAsString(rootNode);
} catch (JsonProcessingException e) {
throw new GeneratorException("Cannot serialize node : " + e.getMessage());
}
}

private static class CustomPrettyPrinter extends DefaultPrettyPrinter {

@Override
public DefaultPrettyPrinter createInstance() {
return new CustomPrettyPrinter();
}

@Override
public void writeObjectFieldValueSeparator(JsonGenerator g) throws IOException {
g.writeRaw(": ");
}

@Override
public void beforeArrayValues(JsonGenerator g) throws IOException {
g.writeRaw("");
}

@Override
public void writeEndArray(JsonGenerator g, int nrOfValues) throws IOException {
g.writeRaw(']');
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package tech.jhipster.lite.generator.client.common.application;

import org.springframework.stereotype.Service;
import tech.jhipster.lite.generator.client.common.domain.ClientCommonService;
import tech.jhipster.lite.generator.project.domain.Project;

@Service
public class ClientCommonApplicationService {

private final ClientCommonService clientCommonService;

public ClientCommonApplicationService(ClientCommonService clientCommonService) {
this.clientCommonService = clientCommonService;
}

public void excludeInTsconfigJson(Project project, String value) {
clientCommonService.excludeInTsconfigJson(project, value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package tech.jhipster.lite.generator.client.common.domain;

import static tech.jhipster.lite.common.domain.FileUtils.getPath;
import static tech.jhipster.lite.common.domain.FileUtils.read;
import static tech.jhipster.lite.generator.project.domain.Constants.TSCONFIG_JSON;

import java.io.IOException;
import tech.jhipster.lite.common.domain.JsonUtils;
import tech.jhipster.lite.error.domain.GeneratorException;
import tech.jhipster.lite.generator.project.domain.Project;
import tech.jhipster.lite.generator.project.domain.ProjectRepository;

public class ClientCommonDomainService implements ClientCommonService {

private final ProjectRepository projectRepository;

public ClientCommonDomainService(ProjectRepository projectRepository) {
this.projectRepository = projectRepository;
}

@Override
public void excludeInTsconfigJson(Project project, String value) {
String tsConfigFilePath = getPath(project.getFolder(), TSCONFIG_JSON);
String tsConfigContent;
try {
tsConfigContent = read(tsConfigFilePath);
} catch (IOException e) {
throw new GeneratorException("Cannot read tsconfig " + tsConfigFilePath + ": " + e.getMessage());
}

String updatedTsConfigContent = JsonUtils.addValueInArray("exclude", value, tsConfigContent);
projectRepository.write(project, updatedTsConfigContent, ".", TSCONFIG_JSON);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package tech.jhipster.lite.generator.client.common.domain;

import tech.jhipster.lite.generator.project.domain.Project;

public interface ClientCommonService {
void excludeInTsconfigJson(Project project, String value);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package tech.jhipster.lite.generator.client.common.infrastructure;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import tech.jhipster.lite.generator.client.common.domain.ClientCommonDomainService;
import tech.jhipster.lite.generator.client.common.domain.ClientCommonService;
import tech.jhipster.lite.generator.project.domain.ProjectRepository;

@Configuration
public class ClientCommonBeanConfiguration {

private final ProjectRepository projectRepository;

public ClientCommonBeanConfiguration(ProjectRepository projectRepository) {
this.projectRepository = projectRepository;
}

@Bean
public ClientCommonService clientCommonService() {
return new ClientCommonDomainService(projectRepository);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@tech.jhipster.lite.SharedKernel
package tech.jhipster.lite.generator.client.common;

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package tech.jhipster.lite.generator.client.tools.cypress.application;

import org.springframework.stereotype.Service;
import tech.jhipster.lite.generator.client.tools.cypress.domain.CypressService;
import tech.jhipster.lite.generator.project.domain.Project;

@Service
public class CypressApplicationService {

private final CypressService cypressService;

public CypressApplicationService(CypressService cypressService) {
this.cypressService = cypressService;
}

public void init(Project project) {
cypressService.init(project);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package tech.jhipster.lite.generator.client.tools.cypress.domain;

import java.util.List;
import java.util.Map;

public class Cypress {

private Cypress() {}

public static final String JAVASCRIPT_INTEGRATION = "src/test/javascript/integration";

public static List<String> devDependencies() {
return List.of("cypress", "eslint-plugin-cypress");
}

public static Map<String, String> cypressScripts() {
return Map.of(
"e2e",
"npm run test:component",
"e2e:headless",
"npm run test:component:headless",
"test:component",
"cypress open --config-file src/test/javascript/integration/cypress-config.json",
"test:component:headless",
"cypress run --headless --config-file src/test/javascript/integration/cypress-config.json"
);
}

public static Map<String, String> cypressFiles() {
String pathRoot = "src/test/javascript/integration";
return Map.ofEntries(Map.entry(".eslintrc.js", pathRoot), Map.entry("tsconfig.json", pathRoot));
}

public static Map<String, String> cypressTestFiles() {
String pathIntegrationTestPrimaryApp = "src/test/javascript/integration/common/primary/app";

return Map.ofEntries(Map.entry("Home.spec.ts", pathIntegrationTestPrimaryApp));
}

public static List<String> tsconfigPatternsToExclude() {
return List.of("src/test/javascript/integration/**/*spec.ts");
}
}
Loading

0 comments on commit 349493c

Please sign in to comment.