Skip to content

Commit

Permalink
Service and Tests changes for Configuration Schema
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirill380 committed Jun 23, 2016
1 parent 7de1877 commit 0300d0b
Show file tree
Hide file tree
Showing 15 changed files with 84 additions and 135 deletions.
Expand Up @@ -36,7 +36,7 @@
@Entity
@Table(name = CONFIGURATION_SCHEMA_TABLE_NAME)
@OnDelete(action = OnDeleteAction.CASCADE)
public class ConfigurationSchema extends Schema<ConfigurationSchemaDto> implements Serializable {
public class ConfigurationSchema extends BaseSchema<ConfigurationSchemaDto> implements Serializable {

private static final long serialVersionUID = -8854035430683210037L;

Expand Down Expand Up @@ -95,9 +95,6 @@ public void setOverrideSchema(String overrideSchema) {
this.overrideSchema = overrideSchema;
}

public String getApplicationId() {
return application != null ? application.getStringId() : null;
}

@Override
public ConfigurationSchemaDto toDto() {
Expand All @@ -118,60 +115,11 @@ protected GenericModel<ConfigurationSchemaDto> newInstance(Long id) {
return new ConfigurationSchema(id);
}

@Override
public int hashCode() {
final int prime = 37;
int result = 1;
result = prime * result + super.hashCode();
result = prime * result + ((baseSchema == null) ? 0 : baseSchema.hashCode());
result = prime * result + ((protocolSchema == null) ? 0 : protocolSchema.hashCode());
result = prime * result + ((overrideSchema == null) ? 0 : overrideSchema.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (!super.equals(obj)) {
return false;
}
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
ConfigurationSchema other = (ConfigurationSchema) obj;
if (baseSchema == null) {
if (other.baseSchema != null) {
return false;
}
} else if (!baseSchema.equals(other.baseSchema)) {
return false;
}
if (protocolSchema == null) {
if (other.protocolSchema != null) {
return false;
}
} else if (!protocolSchema.equals(other.protocolSchema)) {
return false;
}
if (overrideSchema == null) {
if (other.overrideSchema != null) {
return false;
}
} else if (!overrideSchema.equals(other.overrideSchema)) {
return false;
}
return true;
}

@Override
public String toString() {
return "ConfigurationSchema [version=" + version + ", name=" + name + ", description="
+ description + ", createdUsername=" + createdUsername + ", createdTime=" + createdTime + ", endpointCount=" + endpointCount + ", id=" + id
+ description + ", createdUsername=" + createdUsername + ", createdTime=" + createdTime + ", id=" + id
+ "]";
}

Expand Down
Expand Up @@ -242,12 +242,8 @@ private EndpointProfileSchemaDto createDefaultProfileSchema(String appId, String
private ConfigurationDto createDefaultConfigurationWithSchema(String appId, String groupId, String createdUsername) {
ConfigurationSchemaDto schema = new ConfigurationSchemaDto();
schema.setApplicationId(appId);
DataSchema confSchema = new KaaSchemaFactoryImpl().createDataSchema(getStringFromFile(DEFAULT_CONFIGURATION_SCHEMA_FILE, ApplicationServiceImpl.class));
if (!confSchema.isEmpty()) {
schema.setSchema(confSchema.getRawSchema());
} else {
throw new RuntimeException("Can't read default configuration schema."); //NOSONAR
}
CTLSchemaDto ctlSchema = ctlService.getOrCreateEmptySystemSchema(createdUsername);
schema.setCtlSchemaId(ctlSchema.getId());
schema.setName(DEFAULT_SCHEMA_NAME);
schema.setCreatedUsername(createdUsername);
ConfigurationSchemaDto savedSchema = configurationService.saveConfSchema(schema, groupId);
Expand Down
Expand Up @@ -32,6 +32,7 @@
import java.util.Collections;
import java.util.List;

import org.apache.avro.Schema;
import org.apache.avro.generic.GenericRecord;
import org.apache.commons.lang.StringUtils;
import org.kaaproject.kaa.common.avro.GenericAvroConverter;
Expand All @@ -45,6 +46,7 @@
import org.kaaproject.kaa.common.dto.HistoryDto;
import org.kaaproject.kaa.common.dto.UpdateStatus;
import org.kaaproject.kaa.common.dto.VersionDto;
import org.kaaproject.kaa.common.dto.ctl.CTLSchemaDto;
import org.kaaproject.kaa.server.common.core.algorithms.generation.DefaultRecordGenerationAlgorithm;
import org.kaaproject.kaa.server.common.core.algorithms.generation.DefaultRecordGenerationAlgorithmImpl;
import org.kaaproject.kaa.server.common.core.algorithms.schema.SchemaCreationException;
Expand All @@ -60,6 +62,7 @@
import org.kaaproject.kaa.server.common.core.schema.DataSchema;
import org.kaaproject.kaa.server.common.core.schema.OverrideSchema;
import org.kaaproject.kaa.server.common.core.schema.ProtocolSchema;
import org.kaaproject.kaa.server.common.dao.CTLService;
import org.kaaproject.kaa.server.common.dao.ConfigurationService;
import org.kaaproject.kaa.server.common.dao.HistoryService;
import org.kaaproject.kaa.server.common.dao.exception.DatabaseProcessingException;
Expand Down Expand Up @@ -98,6 +101,10 @@ public class ConfigurationServiceImpl implements ConfigurationService {
@Autowired
private HistoryService historyService;


@Autowired
private CTLService ctlService;

@Autowired
private SchemaGenerationAlgorithmFactory schemaGeneratorFactory;

Expand Down Expand Up @@ -499,7 +506,9 @@ private ChangeNotificationDto createNotification(ConfigurationDto configurationD
}

private void generateSchemas(ConfigurationSchemaDto schema) throws SchemaCreationException {
DataSchema dataSchema = new DataSchema(schema.getSchema());
CTLSchemaDto ctlSchema = ctlService.findCTLSchemaById(schema.getCtlSchemaId());
String sch = ctlService.flatExportAsString(ctlSchema);
DataSchema dataSchema = new DataSchema(sch);
if (!dataSchema.isEmpty()) {
SchemaGenerationAlgorithm schemaGenerator = schemaGeneratorFactory.createSchemaGenerator(dataSchema);
ProtocolSchema protocol = schemaGenerator.getProtocolSchema();
Expand Down
Expand Up @@ -67,8 +67,6 @@
import org.kaaproject.kaa.common.dto.TopicTypeDto;
import org.kaaproject.kaa.common.dto.UpdateNotificationDto;
import org.kaaproject.kaa.common.dto.UserDto;
import org.kaaproject.kaa.common.dto.credentials.CredentialsDto;
import org.kaaproject.kaa.common.dto.credentials.CredentialsStatus;
import org.kaaproject.kaa.common.dto.ctl.CTLSchemaDto;
import org.kaaproject.kaa.common.dto.ctl.CTLSchemaMetaInfoDto;
import org.kaaproject.kaa.common.dto.logs.LogAppenderDto;
Expand All @@ -82,7 +80,6 @@
import org.kaaproject.kaa.server.common.core.schema.KaaSchema;
import org.kaaproject.kaa.server.common.core.schema.KaaSchemaFactoryImpl;
import org.kaaproject.kaa.server.common.core.schema.OverrideSchema;
import org.kaaproject.kaa.server.common.dao.exception.CredentialsServiceException;
import org.kaaproject.kaa.server.common.dao.impl.LogAppenderDao;
import org.kaaproject.kaa.server.common.dao.impl.TenantDao;
import org.kaaproject.kaa.server.common.dao.impl.UserDao;
Expand Down Expand Up @@ -223,7 +220,7 @@ public class AbstractTest {
@Autowired
protected SdkProfileDao<SdkProfile> sdkProfileDao;
@Autowired
protected CTLSchemaDao<CTLSchema> ctlSchemaDao;
protected CTLSchemaDao<CTLSchema> ctlSchemaDao;
@Autowired
protected CTLSchemaMetaInfoDao<CTLSchemaMetaInfo> ctlSchemaMetaInfoDao;
@Autowired
Expand All @@ -247,7 +244,7 @@ protected void clearDBData() {
if (url.contains("h2")) {
LOG.info("Deleting data from H2 database");
new H2DBTestRunner().truncateTables(dataSource);
} else if(url.contains("postgres")) {
} else if (url.contains("postgres")) {
LOG.info("Deleting data from PostgreSQL database");
new PostgreDBTestRunner().truncateTables(dataSource);
} else {
Expand Down Expand Up @@ -286,7 +283,7 @@ protected String readSchemaFileAsString(String filePath) throws IOException {
protected ApplicationDto generateApplicationDto() {
return generateApplicationDto(null);
}

protected ApplicationDto generateApplicationDto(String tenantId) {
return generateApplicationDto(tenantId, null);
}
Expand Down Expand Up @@ -321,33 +318,39 @@ protected EndpointGroupDto generateEndpointGroupDto(String appId, String endpoin
return endpointService.saveEndpointGroup(group);
}

protected List<ConfigurationSchemaDto> generateConfSchemaDto(String appId, int count) {
return generateConfSchemaDto(appId, count, "dao/schema/testDataSchema.json");
}

protected List<ConfigurationSchemaDto> generateConfSchemaDto(String appId, int count, String path) {
protected List<ConfigurationSchemaDto> generateConfSchemaDto(String tenantId, String appId, int count) {
List<ConfigurationSchemaDto> schemas = Collections.emptyList();
try {
if (isBlank(tenantId)) {
tenantId = generateTenantDto().getId();
}
if (isBlank(appId)) {
appId = generateApplicationDto().getId();
}

ConfigurationSchemaDto schemaDto;
CTLSchemaDto ctlSchemaDto = ctlService.saveCTLSchema(generateCTLSchemaDto(tenantId));
schemas = new ArrayList<>(count);

for (int i = 0; i < count; i++) {
schemaDto = new ConfigurationSchemaDto();
schemaDto.setApplicationId(appId);
schemaDto.setSchema(readSchemaFileAsString(path));
schemaDto.setCtlSchemaId(ctlSchemaDto.getId());
schemaDto.setCreatedUsername("Test User");
schemaDto.setName("Test Name");
schemaDto = configurationService.saveConfSchema(schemaDto);
Assert.assertNotNull(schemaDto);
schemas.add(schemaDto);
}

} catch (Exception e) {
LOG.error("Can't generate configs {}", e);
Assert.fail("Can't generate configuration schemas." + e.getMessage());
}
return schemas;
}


protected List<ConfigurationDto> generateConfigurationDto(String schemaId, String groupId, int count, boolean activate,
boolean useBaseSchema) {
List<ConfigurationDto> ids = Collections.emptyList();
Expand All @@ -356,7 +359,7 @@ protected List<ConfigurationDto> generateConfigurationDto(String schemaId, Strin
if (isNotBlank(schemaId)) {
schemaDto = configurationService.findConfSchemaById(schemaId);
} else {
schemaDto = generateConfSchemaDto(null, 1).get(0);
schemaDto = generateConfSchemaDto(null, null, 1).get(0);
}
Assert.assertNotNull(schemaDto);
KaaSchema kaaSchema = useBaseSchema ? new BaseSchema(schemaDto.getBaseSchema()) : new OverrideSchema(
Expand Down Expand Up @@ -711,7 +714,7 @@ protected EndpointUserConfigurationDto generateEndpointUserConfigurationDto(Endp
configurationDto.setUserId(endpointUser.getId());

if (configurationSchema == null) {
configurationSchema = generateConfSchemaDto(applicationDto.getId(), 1).get(0);
configurationSchema = generateConfSchemaDto(null, applicationDto.getId(), 1).get(0);
}
configurationDto.setSchemaVersion(configurationSchema.getVersion());

Expand Down Expand Up @@ -787,7 +790,7 @@ protected CTLSchemaDto generateCTLSchemaDto(String fqn, String tenantId, String
ctlSchema.setBody(body.toString());
return ctlSchema;
}

protected String ctlRandomFqn() {
return DEFAULT_FQN + RANDOM.nextInt(100000);
}
Expand All @@ -798,14 +801,14 @@ protected ServerProfileSchemaDto generateServerProfileSchema(String appId, Strin

protected ServerProfileSchemaDto generateServerProfileSchema(String appId, String tenantId, int version) {
ServerProfileSchemaDto schemaDto = new ServerProfileSchemaDto();
if(isBlank(tenantId)) {
if (isBlank(tenantId)) {
ApplicationDto applicationDto = generateApplicationDto();
appId = applicationDto.getId();
tenantId = applicationDto.getTenantId();
}
schemaDto.setApplicationId(appId);
schemaDto.setCreatedTime(System.currentTimeMillis());

CTLSchemaDto ctlSchema = ctlService.saveCTLSchema(generateCTLSchemaDto(ctlRandomFqn(), tenantId, appId, version));
schemaDto.setCtlSchemaId(ctlSchema.getId());
return serverProfileService.saveServerProfileSchema(schemaDto);
Expand Down
Expand Up @@ -160,18 +160,21 @@ protected List<ConfigurationSchema> generateConfSchema(Application app, int coun
if (app == null) {
app = generateApplication(null);
}
CTLSchema ctlSchema = generateCTLSchema(DEFAULT_FQN, 1, app.getTenant(), null);
ConfigurationSchema schema;
schemas = new ArrayList<>(count);
for (int i = 0; i < count; i++) {
schema = new ConfigurationSchema();
schema.setApplication(app);
schema.setSchema(readSchemaFileAsString("dao/schema/testDataSchema.json"));
schema.setCreatedUsername("Test User");
schema.setCtlSchema(ctlSchema);
schema.setVersion(i + 1);
schema.setName("Test Name");
schema = configurationSchemaDao.save(schema);
Assert.assertNotNull(schema);
schemas.add(schema);
}
} catch (IOException e) {
} catch (Exception e) {
LOG.error("Can't generate configuration schemas {}", e);
Assert.fail("Can't generate configuration schemas." + e.getMessage());
}
Expand Down
Expand Up @@ -139,7 +139,7 @@ public void editConfigurationObjectTest() {

@Test
public void findConfSchemaByIdTest() {
List<ConfigurationSchemaDto> schemas = generateConfSchemaDto(null, 1);
List<ConfigurationSchemaDto> schemas = generateConfSchemaDto(null, null, 1);
ConfigurationSchemaDto schema = schemas.get(0);
ConfigurationSchemaDto foundSchema = configurationService.findConfSchemaById(schema.getId());
Assert.assertNotNull(foundSchema);
Expand Down Expand Up @@ -222,13 +222,14 @@ public void findConfSchemaByAppIdAndVersionTest() {

@Test
public void saveConfSchemaTest() throws SchemaCreationException, IOException {
String id = generateConfSchemaDto(null, 1).get(0).getId();
String id = generateConfSchemaDto(null, null, 1).get(0).getId();
ConfigurationSchemaDto schema = configurationService.findConfSchemaById(id);
Assert.assertNotNull(schema);
int version = schema.getVersion();
schema.setId(null);
schema.setSchema(new DataSchema(readSchemaFileAsString("dao/configuration/default_schema.json")).getRawSchema());
ConfigurationSchemaDto saved = configurationService.saveConfSchema(schema);
Assert.assertNotNull(saved);
Assert.assertNotEquals(version, saved.getVersion());
Assert.assertNotEquals(schema.getId(), saved.getId());
}

Expand Down Expand Up @@ -272,14 +273,14 @@ public void createSchemaTest() throws Exception {
@Test
public void createDefaultSchemaTest() {
String id = generateApplicationDto().getId();
ConfigurationSchemaDto schema = generateConfSchemaDto(id, 1).get(0);
ConfigurationSchemaDto schema = generateConfSchemaDto(null, id, 1).get(0);
ConfigurationDto config = configurationService.findConfigurationByAppIdAndVersion(id, schema.getVersion());
Assert.assertEquals(config.getStatus(), UpdateStatus.ACTIVE);
}

@Test
public void findDefaultConfigurationBySchemaIdTest() {
ConfigurationSchemaDto schema = generateConfSchemaDto(null, 1).get(0);
ConfigurationSchemaDto schema = generateConfSchemaDto(null, null, 1).get(0);
ConfigurationDto configuration = configurationService.findDefaultConfigurationBySchemaId(schema.getId());
Assert.assertNotNull(configuration);
Assert.assertEquals(UpdateStatus.ACTIVE, configuration.getStatus());
Expand All @@ -288,7 +289,7 @@ public void findDefaultConfigurationBySchemaIdTest() {

@Test
public void findConfigurationByEndpointGroupIdAndVersionTest() {
ConfigurationSchemaDto schema = generateConfSchemaDto(null, 1).get(0);
ConfigurationSchemaDto schema = generateConfSchemaDto(null, null, 1).get(0);
String groupId = generateEndpointGroupDto(schema.getApplicationId()).getId();
ConfigurationDto config = generateConfigurationDto(schema.getId(), groupId, 1, true, false).get(0);
ConfigurationDto configuration = configurationService.findConfigurationByEndpointGroupIdAndVersion(groupId, schema.getVersion());
Expand Down Expand Up @@ -318,7 +319,7 @@ public void deactivateConfigurationTest() {

@Test
public void deleteConfigurationRecordTest() {
ConfigurationSchemaDto schemaDto = generateConfSchemaDto(null, 1).get(0);
ConfigurationSchemaDto schemaDto = generateConfSchemaDto(null, null, 1).get(0);
EndpointGroupDto group = generateEndpointGroupDto(schemaDto.getApplicationId());
generateConfigurationDto(schemaDto.getId(), group.getId(), 1, true, false);
ChangeConfigurationNotification notification = configurationService.deleteConfigurationRecord(schemaDto.getId(), group.getId(),
Expand All @@ -335,7 +336,7 @@ public void deleteConfigurationRecordTest() {
@Test
public void findAllConfigurationRecordsByEndpointGroupIdTest() {
String id = generateApplicationDto().getId();
ConfigurationSchemaDto schema = generateConfSchemaDto(id, 1).get(0);
ConfigurationSchemaDto schema = generateConfSchemaDto(null, id, 1).get(0);
EndpointGroupDto group = generateEndpointGroupDto(id);
generateConfigurationDto(schema.getId(), group.getId(), 1, true, false);
List<ConfigurationRecordDto> records = (List<ConfigurationRecordDto>) configurationService
Expand All @@ -350,7 +351,7 @@ public void findAllConfigurationRecordsByEndpointGroupIdTest() {

@Test
public void findConfigurationRecordBySchemaIdAndEndpointGroupIdTest() {
ConfigurationSchemaDto schema = generateConfSchemaDto(null, 1).get(0);
ConfigurationSchemaDto schema = generateConfSchemaDto(null, null, 1).get(0);
EndpointGroupDto group = generateEndpointGroupDto(schema.getApplicationId());
ConfigurationDto activeConfig = generateConfigurationDto(schema.getId(), group.getId(), 1, true, false).get(0);
ConfigurationDto inactiveConfig = generateConfigurationDto(schema.getId(), group.getId(), 1, false, false).get(0);
Expand All @@ -363,7 +364,7 @@ public void findConfigurationRecordBySchemaIdAndEndpointGroupIdTest() {
@Test
public void findVacantSchemasByEndpointGroupIdTest() {
ApplicationDto application = generateApplicationDto();
List<ConfigurationSchemaDto> schemas = generateConfSchemaDto(application.getId(), 4);
List<ConfigurationSchemaDto> schemas = generateConfSchemaDto(null, application.getId(), 4);
EndpointGroupDto groupOne = generateEndpointGroupDto(application.getId());
ConfigurationSchemaDto schemaOne = schemas.get(0);
generateConfigurationDto(schemaOne.getId(), groupOne.getId(), 1, true, false);
Expand All @@ -380,7 +381,7 @@ public void findVacantSchemasByEndpointGroupIdTest() {

@Test
public void findConfigurationSchemaVersionsByAppIdTest() {
ConfigurationSchemaDto schemaDto = generateConfSchemaDto(null, 1).get(0);
ConfigurationSchemaDto schemaDto = generateConfSchemaDto(null, null, 1).get(0);
List<VersionDto> versions = configurationService.findConfigurationSchemaVersionsByAppId(schemaDto.getApplicationId());
Assert.assertFalse(versions.isEmpty());
Assert.assertEquals(2, versions.size());
Expand Down

0 comments on commit 0300d0b

Please sign in to comment.