@Override protected GenericModel<LogSchemaDto> newInstance(Long id) { return new LogSchema(id); }
@Override public void removeLogSchemasByAppId(String applicationId) { validateId(applicationId, "Can't remove log schema. Invalid application id: " + applicationId); List<LogSchema> schemas = logSchemaDao.findByApplicationId(applicationId); if (schemas != null && !schemas.isEmpty()) { LOG.debug("Remove log shemas by application id {}", applicationId); for (LogSchema schema : schemas) { removeLogSchemaById(schema.getStringId()); } } }
@Override public List<VersionDto> findLogSchemaVersionsByApplicationId(String applicationId) { validateId(applicationId, "Can't find log schemas. Invalid application id: " + applicationId); List<LogSchema> logSchemas = logSchemaDao.findByApplicationId(applicationId); List<VersionDto> schemas = new ArrayList<>(); for (LogSchema logSchema : logSchemas) { schemas.add(logSchema.toVersionDto()); } return schemas; }
protected List<LogSchema> generateLogSchema(Tenant tenant, int ctlVersion, Application application, int count) { List<LogSchema> schemas = Collections.emptyList(); try { if (application == null) { application = generateApplication(tenant); } CtlSchema ctlSchema = generateCTLSchema(DEFAULT_FQN, ctlVersion, application.getTenant(), null); LogSchema schema; schemas = new ArrayList<>(count); for (int i = 0; i < count; i++) { schema = new LogSchema(); schema.setApplication(application); schema.setCtlSchema(ctlSchema); schema.setCreatedUsername("Test User"); schema.setName("Test Name"); schema = logSchemaDao.save(schema); Assert.assertNotNull(schema); schemas.add(schema); } } catch (Exception ex) { LOG.error("Can't generate log schemas {}", ex); Assert.fail("Can't generate log schemas."); } return schemas; }
@Override public LogSchemaDto saveLogSchema(LogSchemaDto logSchemaDto) { String id = logSchemaDto.getId(); if (StringUtils.isBlank(id)) { LogSchema logSchema = logSchemaDao.findLatestLogSchemaByAppId( logSchemaDto.getApplicationId()); int version = 0; if (logSchema != null) { version = logSchema.getVersion(); } logSchemaDto.setId(null); logSchemaDto.setVersion(++version); logSchemaDto.setCreatedTime(System.currentTimeMillis()); } else { LogSchemaDto oldLogSchemaDto = getDto(logSchemaDao.findById(id)); if (oldLogSchemaDto != null) { oldLogSchemaDto.editFields(logSchemaDto); logSchemaDto = oldLogSchemaDto; } else { LOG.error("Can't find log schema with given id [{}].", id); throw new IncorrectParameterException("Invalid log schema id: " + id); } } LogSchemaDto savedSchema = getDto(logSchemaDao.save(new LogSchema(logSchemaDto))); if (savedSchema == null) { throw new RuntimeException("Can't save log schema"); } return savedSchema; }
@Test public void findByAppIdAndVersionTest() { LogSchema logSchema = generateLogSchema(null, 1, null, 1).get(0); Assert.assertNotNull(logSchema); LogSchema found = logSchemaDao.findByApplicationIdAndVersion(logSchema.getApplication().getStringId(), logSchema.getVersion()); Assert.assertEquals(logSchema, found); }
@Test public void removeByApplicationIdTest() { LogSchema logSchema = generateLogSchema(null, 1, null, 1).get(0); Assert.assertNotNull(logSchema); logSchemaDao.removeByApplicationId(logSchema.getApplication().getStringId()); LogSchema schema = logSchemaDao.findById(logSchema.getStringId()); Assert.assertNull(schema); }
@Test public void findByApplicationIdTest() { LogSchema logSchema = generateLogSchema(null, 1, null, 1).get(0); Assert.assertNotNull(logSchema); List<LogSchema> schemaList = logSchemaDao.findByApplicationId(logSchema.getApplication().getStringId()); Assert.assertNotNull(schemaList); LogSchema found = null; for (LogSchema schema : schemaList) { if (schema.getId().equals(logSchema.getId())) { found = schema; } } Assert.assertEquals(logSchema, found); }
@Test public void removeByIdTest() { LogSchema logSchema = generateLogSchema(null, 1, null, 1).get(0); Assert.assertNotNull(logSchema); logSchemaDao.removeById(logSchema.getStringId()); LogSchema schema = logSchemaDao.findById(logSchema.getStringId()); Assert.assertNull(schema); } }