Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix statistics creation when creationTime is provided #357

Merged
merged 5 commits into from
May 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,9 @@
import com.fasterxml.jackson.databind.node.ArrayNode;
import io.github.jhipster.online.domain.YoRC;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.TimeZone;

public class YoRCDeserializer extends StdDeserializer<YoRC> {

Expand Down Expand Up @@ -64,7 +59,8 @@ public YoRC deserialize(JsonParser jp, DeserializationContext ctxt) throws IOExc
String applicationType = getDefaultIfNull(node.get("applicationType"), "");
boolean enableTranslation = getDefaultIfNull(node.get("enableTranslation"), false);
String nativeLanguage = getDefaultIfNull(node.get("nativeLanguage"), "");
String creationDate = getDefaultIfNull(node.get("creationTimestamp"), Instant.now().toString());
Instant creationDate = getCreationDate(node.get("creationTimestamp"));

boolean hasProtractor = false;
boolean hasGatling = false;
boolean hasCucumber = false;
Expand Down Expand Up @@ -114,7 +110,15 @@ public YoRC deserialize(JsonParser jp, DeserializationContext ctxt) throws IOExc
.hasGatling(hasGatling)
.hasCucumber(hasCucumber)
.selectedLanguages(languages)
.creationDate(Instant.parse(creationDate));
.creationDate(creationDate);
}

private Instant getCreationDate(JsonNode node) {
if (node == null) {
return Instant.now();
}

return Instant.ofEpochMilli(node.asLong());
}

private String getDefaultIfNull(JsonNode node, String defaultValue) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,45 @@ class StatisticsResourceIntTest {
@Autowired
private ExceptionTranslator exceptionTranslator;

private MockMvc restStatiticsMockMvc;
private MockMvc restStatisticsMockMvc;

private YoRC yoRC;
private final String generatorId = "cf51ff78-187a-4554-9b09-8f6f95f1a7a5";

private String generatorId = "cf51ff78-187a-4554-9b09-8f6f95f1a7a5";
private final String generatorJhipsterWithCreationTimestamp =
"{\n" +
" \"useYarn\": false,\n" +
" \"experimental\": false,\n" +
" \"skipI18nQuestion\": true,\n" +
" \"logo\": false,\n" +
" \"clientPackageManager\": \"npm\",\n" +
" \"creationTimestamp\": 1650832223564,\n" +
" \"cacheProvider\": \"ehcache\",\n" +
" \"enableHibernateCache\": true,\n" +
" \"websocket\": false,\n" +
" \"databaseType\": \"sql\",\n" +
" \"devDatabaseType\": \"h2Disk\",\n" +
" \"prodDatabaseType\": \"mysql\",\n" +
" \"searchEngine\": false,\n" +
" \"messageBroker\": false,\n" +
" \"serviceDiscoveryType\": false,\n" +
" \"buildTool\": \"maven\",\n" +
" \"enableSwaggerCodegen\": false,\n" +
" \"authenticationType\": \"jwt\",\n" +
" \"serverPort\": \"8080\",\n" +
" \"clientFramework\": \"angularX\",\n" +
" \"withAdminUi\": \"true\",\n" +
" \"useSass\": false,\n" +
" \"testFrameworks\": [],\n" +
" \"enableTranslation\": true,\n" +
" \"nativeLanguage\": \"en\",\n" +
" \"languages\": [\n" +
" \"en\"\n" +
" ],\n" +
" \"applicationType\": \"monolith\"\n" +
" },\n";

private String dummyYo =
private final String generatorJhipsterWithoutCreationTimestamp =
"{\n" +
" \"generator-jhipster\": {\n" +
" \"useYarn\": false,\n" +
" \"experimental\": false,\n" +
" \"skipI18nQuestion\": true,\n" +
Expand Down Expand Up @@ -116,21 +146,29 @@ class StatisticsResourceIntTest {
" \"en\"\n" +
" ],\n" +
" \"applicationType\": \"monolith\"\n" +
" },\n" +
" \"generator-id\": \"" +
generatorId +
"\",\n" +
" \"generator-version\": \"5.1.0\",\n" +
" \"git-provider\": \"local\",\n" +
" \"node-version\": \"v8.11.1\",\n" +
" \"os\": \"linux:4.15.0-29-generic\",\n" +
" \"arch\": \"x64\",\n" +
" \"cpu\": \"Intel(R) Core(TM) i7-7700K CPU @ 4.20GHz\",\n" +
" \"cores\": 8,\n" +
" \"memory\": 16776642560,\n" +
" \"user-language\": \"en_GB\",\n" +
" \"isARegeneration\": true\n" +
" }";
" },\n";

private String dummyYo(String dummyGeneratorJhipsterData) {
return (
"{\n" +
" \"generator-jhipster\":" +
dummyGeneratorJhipsterData +
" \"generator-id\": \"" +
generatorId +
"\",\n" +
" \"generator-version\": \"5.1.0\",\n" +
" \"git-provider\": \"local\",\n" +
" \"node-version\": \"v8.11.1\",\n" +
" \"os\": \"linux:4.15.0-29-generic\",\n" +
" \"arch\": \"x64\",\n" +
" \"cpu\": \"Intel(R) Core(TM) i7-7700K CPU @ 4.20GHz\",\n" +
" \"cores\": 8,\n" +
" \"memory\": 16776642560,\n" +
" \"user-language\": \"en_GB\",\n" +
" \"isARegeneration\": true\n" +
" }"
);
}

@BeforeEach
public void setup() {
Expand All @@ -145,7 +183,7 @@ public void setup() {
entityStatService
);

this.restStatiticsMockMvc =
this.restStatisticsMockMvc =
MockMvcBuilders
.standaloneSetup(statisticsResource)
.setMessageConverters(httpMessageConverters)
Expand All @@ -156,13 +194,13 @@ public void setup() {
@Test
@Transactional
void shouldNotGetCountWithUnknownFrequency() throws Exception {
restStatiticsMockMvc.perform(get("/api/s/count-yo/{frequency}", "every minutes")).andExpect(status().isBadRequest());
restStatisticsMockMvc.perform(get("/api/s/count-yo/{frequency}", "every minutes")).andExpect(status().isBadRequest());
}

@Test
@Transactional
void shouldNotGetFieldCountWithUnknownFieldOrFrequency() throws Exception {
restStatiticsMockMvc
restStatisticsMockMvc
.perform(get("/api/s/yo/{field}/{frequency}", "clientFramework", "every minutes"))
.andExpect(status().isBadRequest());
}
Expand All @@ -172,7 +210,7 @@ void shouldNotGetFieldCountWithUnknownFieldOrFrequency() throws Exception {
void getYoCount() throws Exception {
int databaseSizeBeforeAdd = yoRCRepository.findAll().size();

restStatiticsMockMvc.perform(get("/api/s/count-yo")).andExpect(status().isOk());
restStatisticsMockMvc.perform(get("/api/s/count-yo")).andExpect(status().isOk());

YoRC yorc = new YoRC().owner(new GeneratorIdentity());
generatorIdentityRepository.saveAndFlush(yorc.getOwner());
Expand All @@ -188,7 +226,23 @@ void getYoCount() throws Exception {
void addEntry() throws Exception {
int databaseSizeBeforeAdd = yoRCRepository.findAll().size();

restStatiticsMockMvc.perform(post("/api/s/entry").content(dummyYo)).andExpect(status().isCreated());
final String content = dummyYo(generatorJhipsterWithoutCreationTimestamp);

restStatisticsMockMvc.perform(post("/api/s/entry").content(content)).andExpect(status().isCreated());

int databaseSizeAfterAdd = yoRCRepository.findAll().size();

assertThat(databaseSizeAfterAdd).isEqualTo(databaseSizeBeforeAdd + 1);
}

@Test
@Transactional
void addEntryWithCreationTimestamp() throws Exception {
int databaseSizeBeforeAdd = yoRCRepository.findAll().size();

final String content = dummyYo(generatorJhipsterWithCreationTimestamp);

restStatisticsMockMvc.perform(post("/api/s/entry").content(content)).andExpect(status().isCreated());

int databaseSizeAfterAdd = yoRCRepository.findAll().size();

Expand Down