Skip to content

Commit

Permalink
Revert "KAA-1145: CTL in Notifications extension"
Browse files Browse the repository at this point in the history
  • Loading branch information
rasendubi committed Jul 6, 2016
1 parent 4f9fb23 commit c22a8b7
Show file tree
Hide file tree
Showing 29 changed files with 223 additions and 282 deletions.
Expand Up @@ -251,9 +251,15 @@ public ServerProfileSchemaDto saveServerProfileSchema(ServerProfileSchemaDto ser
return restTemplate.postForObject(restTemplate.getUrl() + "saveServerProfileSchema", serverProfileSchema, ServerProfileSchemaDto.class);
}

public NotificationSchemaDto createNotificationSchema(NotificationSchemaDto notificationSchema) throws Exception {
public NotificationSchemaDto createNotificationSchema(NotificationSchemaDto notificationSchema, String schemaResource) throws Exception {
return createNotificationSchema(notificationSchema, getFileResource(schemaResource));
}

public NotificationSchemaDto createNotificationSchema(NotificationSchemaDto notificationSchema, ByteArrayResource schemaResource)
throws Exception {
MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
params.add("notificationSchema", notificationSchema);
params.add("file", schemaResource);
return restTemplate.postForObject(restTemplate.getUrl() + "createNotificationSchema", params, NotificationSchemaDto.class);
}

Expand Down
Expand Up @@ -30,7 +30,7 @@
@Entity
@Table(name = NOTIFICATION_SCHEMA_TABLE_NAME)
@OnDelete(action = OnDeleteAction.CASCADE)
public class NotificationSchema extends BaseSchema<NotificationSchemaDto> implements Serializable {
public class NotificationSchema extends Schema<NotificationSchemaDto> implements Serializable {

private static final long serialVersionUID = 6585856417466958172L;

Expand Down
Expand Up @@ -270,18 +270,19 @@ private ServerProfileSchemaDto createDefaultServerProfileSchema(String appId, St
}

private NotificationSchemaDto createDefaultNotificationSchema(String appId, String createdUsername) {
NotificationSchemaDto notificationSchemaDto = new NotificationSchemaDto();
notificationSchemaDto.setApplicationId(appId);
CTLSchemaDto ctlSchema = ctlService.getOrCreateEmptySystemSchema(createdUsername);
notificationSchemaDto.setCtlSchemaId(ctlSchema.getId());
notificationSchemaDto.setName(DEFAULT_SCHEMA_NAME);
notificationSchemaDto.setCreatedUsername(createdUsername);
notificationSchemaDto.setType(NotificationTypeDto.USER);
notificationSchemaDto = notificationService.saveNotificationSchema(notificationSchemaDto);
if (notificationSchemaDto == null) {
throw new RuntimeException("Can't save default profile schema "); //NOSONAR
NotificationSchemaDto schema = new NotificationSchemaDto();
schema.setApplicationId(appId);
DataSchema defSchema = new KaaSchemaFactoryImpl().createDataSchema(getStringFromFile(DEFAULT_NOTIFICATION_SCHEMA_FILE, ApplicationServiceImpl.class));
if (!defSchema.isEmpty()) {
schema.setSchema(defSchema.getRawSchema());
} else {
throw new RuntimeException("Can't read default notification schema."); //NOSONAR
}
return notificationSchemaDto;
schema.setType(NotificationTypeDto.USER);
schema.setName(DEFAULT_SCHEMA_NAME);
schema.setCreatedUsername(createdUsername);

return notificationService.saveNotificationSchema(schema);
}

private LogSchemaDto createDefaultLogSchema(String appId, String createdUsername) {
Expand Down
Expand Up @@ -170,7 +170,7 @@ public NotificationDto saveNotificationAndIncTopicSecNum(NotificationDto dto) {
}

private byte[] serializeNotificationBody(NotificationDto nf, NotificationSchema nfSchema) throws IOException {
GenericAvroConverter<GenericRecord> converter = new GenericAvroConverter<>(nfSchema.getCtlSchema().getBody());
GenericAvroConverter<GenericRecord> converter = new GenericAvroConverter<>(nfSchema.getSchema());
String notificationJson = new String(nf.getBody(), Charset.forName("UTF8"));
GenericRecord notificationAvro = converter.decodeJson(notificationJson);
return converter.encode(notificationAvro);
Expand Down
Expand Up @@ -83,7 +83,6 @@
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.exception.DatabaseProcessingException;
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 @@ -568,24 +567,20 @@ protected TopicDto generateTopicDto(String appId, TopicTypeDto type) {

protected NotificationSchemaDto generateNotificationSchemaDto(String appId, NotificationTypeDto type) {
NotificationSchemaDto schema = new NotificationSchemaDto();
ApplicationDto app = null;
if (isBlank(appId)) {
app = generateApplicationDto();
appId = app.getId();
} else {
app = applicationService.findAppById(appId);
appId = generateApplicationDto().getId();
}
schema.setApplicationId(appId);
schema.setName(NOTIFICATION_SCHEMA_NAME);
schema.setType(type != null ? type : NotificationTypeDto.USER);
CTLSchemaDto ctlSchema = null;
String schemaBody = null;
try {
ctlSchema = ctlService.saveCTLSchema(generateCTLSchemaDto(app.getTenantId()));
} catch (DatabaseProcessingException e){
ctlSchema = ctlService.getOrCreateEmptySystemSchema(USER_NAME);

schemaBody = readSchemaFileAsString("dao/schema/testBaseSchema.json");
} catch (IOException e) {
e.printStackTrace();
Assert.fail(e.getMessage());
}
schema.setCtlSchemaId(ctlSchema.getId());
schema.setSchema(new KaaSchemaFactoryImpl().createDataSchema(schemaBody).getRawSchema());
schema.setType(type != null ? type : NotificationTypeDto.USER);
return notificationService.saveNotificationSchema(schema);
}

Expand Down
Expand Up @@ -258,30 +258,29 @@ protected CTLSchema generateCTLSchema(String fqn, int version, Tenant tenant, CT
return ctlSchema;
}

protected List<NotificationSchema> generateNotificationSchema(Application app, int ctlVersion, int count, NotificationTypeDto type) {
protected List<NotificationSchema> generateNotificationSchema(Application app, int count, NotificationTypeDto type) {
List<NotificationSchema> schemas = Collections.emptyList();
try {
if (app == null) {
app = generateApplication(null);
}
CTLSchema ctlSchema = generateCTLSchema(DEFAULT_FQN, ctlVersion, app.getTenant(), null);
NotificationSchema schemaDto;
NotificationSchema notificationSchema;
schemas = new ArrayList<>(count);
for (int i = 0; i < count; i++) {
schemaDto = new NotificationSchema();
schemaDto.setApplication(app);
schemaDto.setCreatedUsername("Test User");
schemaDto.setCtlSchema(ctlSchema);
schemaDto.setVersion(i + 1);
schemaDto.setName("Test Name");
schemaDto.setType(type);
schemaDto = notificationSchemaDao.save(schemaDto);
Assert.assertNotNull(schemaDto);
schemas.add(schemaDto);
notificationSchema = new NotificationSchema();
notificationSchema.setApplication(app);
notificationSchema.setSchema(readSchemaFileAsString("dao/schema/testDataSchema.json"));
notificationSchema.setCreatedUsername("Test User");
notificationSchema.setVersion(i + 1);
notificationSchema.setName("Test Name");
notificationSchema.setType(type == null ? NotificationTypeDto.SYSTEM : type);
notificationSchema = notificationSchemaDao.save(notificationSchema);
Assert.assertNotNull(notificationSchema);
schemas.add(notificationSchema);
}
} catch (Exception e) {
LOG.error("Can't generate profile schema {}", e);
Assert.fail("Can't generate profile schema." + e.getMessage());
} catch (IOException e) {
LOG.error("Can't generate notification schema {}", e);
Assert.fail("Can't generate notification schema." + e.getMessage());
}
return schemas;
}
Expand Down
Expand Up @@ -16,7 +16,6 @@

package org.kaaproject.kaa.server.common.dao.impl.sql;

import org.junit.After;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -32,7 +31,6 @@
import javax.transaction.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "/common-dao-test-context.xml")
Expand All @@ -46,15 +44,15 @@ public class HibernateNotificationSchemaDaoTest extends HibernateAbstractTest {
@Test
public void testFindNotificationSchemasByAppId() throws Exception {
Application application = generateApplication(null);
List<NotificationSchema> schemas = generateNotificationSchema(application, 1, 1, null);
List<NotificationSchema> schemas = generateNotificationSchema(application, 1, null);
List<NotificationSchema> found = notificationSchemaDao.findNotificationSchemasByAppId(application.getStringId());
Assert.assertEquals(schemas, found);
}

@Test
public void testRemoveNotificationSchemasByAppId() throws Exception {
Application application = generateApplication(null);
generateNotificationSchema(application, 1, 1, null);
generateNotificationSchema(application, 1, null);
notificationSchemaDao.removeNotificationSchemasByAppId(application.getStringId());
List<NotificationSchema> found = notificationSchemaDao.findNotificationSchemasByAppId(application.getStringId());
Assert.assertTrue(found.isEmpty());
Expand All @@ -63,17 +61,17 @@ public void testRemoveNotificationSchemasByAppId() throws Exception {
@Test
public void testFindNotificationSchemasByAppIdAndType() throws Exception {
Application application = generateApplication(null);
List<NotificationSchema> userSchemas = generateNotificationSchema(application, 1, 2, NotificationTypeDto.USER);
generateNotificationSchema(application, 2, 3, NotificationTypeDto.SYSTEM);
List<NotificationSchema> userSchemas = generateNotificationSchema(application, 2, NotificationTypeDto.USER);
generateNotificationSchema(application, 3, NotificationTypeDto.SYSTEM);
List<NotificationSchema> found = notificationSchemaDao.findNotificationSchemasByAppIdAndType(application.getStringId(), NotificationTypeDto.USER);
Assert.assertEquals(userSchemas, found);
}

@Test
public void testFindNotificationSchemasByAppIdAndTypeAndVersion() throws Exception {
Application application = generateApplication(null);
generateNotificationSchema(application, 1, 1, NotificationTypeDto.SYSTEM);
List<NotificationSchema> userSchemas = generateNotificationSchema(application, 2, 3, NotificationTypeDto.USER);
generateNotificationSchema(application, 1, NotificationTypeDto.SYSTEM);
List<NotificationSchema> userSchemas = generateNotificationSchema(application, 3, NotificationTypeDto.USER);
NotificationSchema expected = userSchemas.get(2);
NotificationSchema found = notificationSchemaDao.findNotificationSchemasByAppIdAndTypeAndVersion(application.getStringId(), NotificationTypeDto.USER, expected.getVersion());
Assert.assertEquals(expected, found);
Expand All @@ -82,8 +80,8 @@ public void testFindNotificationSchemasByAppIdAndTypeAndVersion() throws Excepti
@Test
public void testFindLatestNotificationSchemaByAppId() throws Exception {
Application application = generateApplication(null);
List<NotificationSchema> userSchemas = generateNotificationSchema(application, 1, 3, NotificationTypeDto.USER);
List<NotificationSchema> systemSchemas = generateNotificationSchema(application, 2, 3, NotificationTypeDto.SYSTEM);
List<NotificationSchema> userSchemas = generateNotificationSchema(application, 3, NotificationTypeDto.USER);
List<NotificationSchema> systemSchemas = generateNotificationSchema(application, 3, NotificationTypeDto.SYSTEM);
NotificationSchema found = notificationSchemaDao.findLatestNotificationSchemaByAppId(application.getStringId(), NotificationTypeDto.USER);
Assert.assertEquals(userSchemas.get(2), found);
}
Expand Down
Expand Up @@ -17,12 +17,13 @@
package org.kaaproject.kaa.common.dto;


public class NotificationSchemaDto extends BaseSchemaDto {
public class NotificationSchemaDto extends AbstractSchemaDto {

private static final long serialVersionUID = -2514664251184915862L;

private NotificationTypeDto type;


public NotificationTypeDto getType() {
return type;
}
Expand All @@ -48,7 +49,7 @@ public boolean equals(Object o) {
if (applicationId != null ? !applicationId.equals(that.applicationId) : that.applicationId != null) {
return false;
}
if (ctlSchemaId != null ? !ctlSchemaId.equals(that.ctlSchemaId) : that.ctlSchemaId != null) {
if (schema != null ? !schema.equals(that.schema) : that.schema != null) {
return false;
}
if (type != that.type) {
Expand All @@ -63,7 +64,7 @@ public int hashCode() {
int result = applicationId != null ? applicationId.hashCode() : 0;
result = 31 * result + version;
result = 31 * result + (type != null ? type.hashCode() : 0);
result = 31 * result + (ctlSchemaId != null ? ctlSchemaId.hashCode() : 0);
result = 31 * result + (schema != null ? schema.hashCode() : 0);
return result;
}

Expand All @@ -74,7 +75,7 @@ public String toString() {
", applicationId='" + applicationId + '\'' +
", majorVersion=" + version +
", type=" + type +
", ctlSchemaId='" + ctlSchemaId + '\'' +
", schema='" + schema + '\'' +
'}';
}

Expand Down
Expand Up @@ -31,8 +31,15 @@
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.kaaproject.kaa.common.dto.*;
import org.kaaproject.kaa.common.dto.ctl.CTLSchemaDto;
import org.kaaproject.kaa.common.dto.EndpointNotificationDto;
import org.kaaproject.kaa.common.dto.EndpointProfileDto;
import org.kaaproject.kaa.common.dto.NotificationDto;
import org.kaaproject.kaa.common.dto.NotificationSchemaDto;
import org.kaaproject.kaa.common.dto.NotificationTypeDto;
import org.kaaproject.kaa.common.dto.TopicDto;
import org.kaaproject.kaa.common.dto.TopicTypeDto;
import org.kaaproject.kaa.common.dto.UpdateNotificationDto;
import org.kaaproject.kaa.common.dto.VersionDto;
import org.kaaproject.kaa.server.common.core.schema.KaaSchemaFactoryImpl;
import org.kaaproject.kaa.server.common.dao.exception.IncorrectParameterException;
import org.slf4j.Logger;
Expand Down Expand Up @@ -67,22 +74,17 @@ public void afterTest() {
@Ignore
@Test
public void findNotificationsByIdWithExpiredTimeTest() {
ApplicationDto app = generateApplicationDto();

NotificationSchemaDto notificationSchemaDto = new NotificationSchemaDto();
notificationSchemaDto.setApplicationId(app.getId());
CTLSchemaDto ctlSchema = ctlService.saveCTLSchema(generateCTLSchemaDto(app.getTenantId()));
notificationSchemaDto.setCtlSchemaId(ctlSchema.getId());
if (notificationSchemaDto == null) {
throw new RuntimeException("Can't save default profile schema "); //NOSONAR
}
String appId = generateApplicationDto().getId();

notificationSchemaDto.setCtlSchemaId(ctlSchema.getId());
notificationSchemaDto.setType(NotificationTypeDto.USER);
NotificationSchemaDto savedSchema = notificationService.saveNotificationSchema(notificationSchemaDto);
NotificationSchemaDto schema = new NotificationSchemaDto();
schema.setApplicationId(appId);
String schemaBody = "{\"type\":\"record\",\"name\":\"BasicSystemNotification\",\"namespace\":\"org.kaaproject.kaa.common.endpoint.gen\",\"fields\":[{\"name\":\"notificationBody\",\"type\":{\"type\":\"string\",\"avro.java.string\":\"String\"}},{\"name\":\"systemNotificationParam1\",\"type\":\"int\"},{\"name\":\"systemNotificationParam2\",\"type\":\"int\"}]}";
schema.setSchema(new KaaSchemaFactoryImpl().createDataSchema(schemaBody).getRawSchema());
schema.setType(NotificationTypeDto.USER);
NotificationSchemaDto savedSchema = notificationService.saveNotificationSchema(schema);

TopicDto topicDto = new TopicDto();
topicDto.setApplicationId(app.getId());
topicDto.setApplicationId(appId);
topicDto.setName("New Topic");
topicDto.setType(TopicTypeDto.MANDATORY);

Expand Down
Expand Up @@ -105,8 +105,8 @@ public interface ClientFactory {
BaseSchemaView getCreateConfigurationSchemaView();

BaseListView<NotificationSchemaDto> getNotificationSchemasView();
BaseCtlSchemaView getNotificationSchemaView();
BaseCtlSchemaView getCreateNotificationSchemaView();
BaseSchemaView getNotificationSchemaView();
BaseSchemaView getCreateNotificationSchemaView();

BaseListView<LogSchemaDto> getLogSchemasView();
BaseSchemaView getLogSchemaView();
Expand Down
Expand Up @@ -157,8 +157,8 @@ public class ClientFactoryImpl implements ClientFactory {
private final BaseSchemaView createConfigurationSchemaView = new ConfigurationSchemaViewImpl(true);

private final BaseListView<NotificationSchemaDto> notificationSchemasView = new NotificationSchemasViewImpl();
private final BaseCtlSchemaView notificationSchemaView = new NotificationSchemaViewImpl(false);
private final BaseCtlSchemaView createNotificationSchemaView = new NotificationSchemaViewImpl(true);
private final BaseSchemaView notificationSchemaView = new NotificationSchemaViewImpl(false);
private final BaseSchemaView createNotificationSchemaView = new NotificationSchemaViewImpl(true);

private final BaseListView<LogSchemaDto> logSchemasView = new LogSchemasViewImpl();
private final BaseSchemaView logSchemaView = new LogSchemaViewImpl(false);
Expand Down Expand Up @@ -361,12 +361,12 @@ public BaseListView<NotificationSchemaDto> getNotificationSchemasView() {
}

@Override
public BaseCtlSchemaView getNotificationSchemaView() {
public BaseSchemaView getNotificationSchemaView() {
return notificationSchemaView;
}

@Override
public BaseCtlSchemaView getCreateNotificationSchemaView() {
public BaseSchemaView getCreateNotificationSchemaView() {
return createNotificationSchemaView;
}

Expand Down

0 comments on commit c22a8b7

Please sign in to comment.