Skip to content

Commit

Permalink
KAA-25: Fixed search active profile filters by combination of schema …
Browse files Browse the repository at this point in the history
…versions.
  • Loading branch information
Igor Khanenko committed Dec 14, 2015
1 parent f7cfecf commit 62859fa
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 43 deletions.
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public interface ProfileService {
* @param serverSchemaVersion the schema version * @param serverSchemaVersion the schema version
* @return the list of profile filters * @return the list of profile filters
*/ */
List<ProfileFilterDto> findProfileFiltersByAppIdAndVersions(String appId, int endpointSchemaVersion, int serverSchemaVersion); List<ProfileFilterDto> findProfileFiltersByAppIdAndVersionsCombination(String appId, int endpointSchemaVersion, int serverSchemaVersion);


/** /**
* Find profile schema by application id and version. * Find profile schema by application id and version.
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public interface ProfileFilterDao<T> extends SqlDao<T> {
* @param serverSchemaVersion the server schema version * @param serverSchemaVersion the server schema version
* @return the list of profile filters * @return the list of profile filters
*/ */
List<T> findByAppIdAndSchemaVersions(String appId, int endpointSchemaVersion, int serverSchemaVersion); List<T> findByAppIdAndSchemaVersionsCombination(String appId, int endpointSchemaVersion, int serverSchemaVersion);


/** /**
* Find inactive profile filters. * Find inactive profile filters.
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public ProfileFilter findLatestDeprecated(String endpointProfileSchemaId, String
} }


@Override @Override
public List<ProfileFilter> findByAppIdAndSchemaVersions(String appId, int endpointSchemaVersion, int serverSchemaVersion) { public List<ProfileFilter> findByAppIdAndSchemaVersionsCombination(String appId, int endpointSchemaVersion, int serverSchemaVersion) {
List<ProfileFilter> filters = null; List<ProfileFilter> filters = null;
LOG.debug("Searching configuration by application id [{}] and schema version [{}]", appId, serverSchemaVersion); LOG.debug("Searching configuration by application id [{}] and schema version [{}]", appId, serverSchemaVersion);
if (isNotBlank(appId)) { if (isNotBlank(appId)) {
Expand All @@ -136,9 +136,17 @@ public List<ProfileFilter> findByAppIdAndSchemaVersions(String appId, int endpoi
criteria.createAlias(SERVER_PROFILE_SCHEMA_PROPERTY, SERVER_PROFILE_SCHEMA_ALIAS, JoinType.LEFT_OUTER_JOIN); criteria.createAlias(SERVER_PROFILE_SCHEMA_PROPERTY, SERVER_PROFILE_SCHEMA_ALIAS, JoinType.LEFT_OUTER_JOIN);
Criterion criterion = Restrictions.and( Criterion criterion = Restrictions.and(
Restrictions.eq(APPLICATION_REFERENCE, Long.valueOf(appId)), Restrictions.eq(APPLICATION_REFERENCE, Long.valueOf(appId)),
Restrictions.eq(ENDPOINT_PROFILE_SCHEMA_VERSION_REFERENCE, endpointSchemaVersion), Restrictions.eq(STATUS_PROPERTY, UpdateStatus.ACTIVE),
Restrictions.eq(SERVER_PROFILE_SCHEMA_VERSION_REFERENCE, serverSchemaVersion), Restrictions.or(Restrictions.and(
Restrictions.eq(STATUS_PROPERTY, UpdateStatus.ACTIVE)); Restrictions.eq(ENDPOINT_PROFILE_SCHEMA_VERSION_REFERENCE, endpointSchemaVersion),
Restrictions.eq(SERVER_PROFILE_SCHEMA_VERSION_REFERENCE, serverSchemaVersion)
), Restrictions.and(
Restrictions.eq(ENDPOINT_PROFILE_SCHEMA_VERSION_REFERENCE, endpointSchemaVersion),
Restrictions.isNull(SERVER_PROFILE_SCHEMA_VERSION_REFERENCE)
), Restrictions.and(
Restrictions.eq(SERVER_PROFILE_SCHEMA_VERSION_REFERENCE, serverSchemaVersion),
Restrictions.isNull(ENDPOINT_PROFILE_SCHEMA_VERSION_REFERENCE)
)));
criteria.add(criterion); criteria.add(criterion);
filters = findListByCriteria(criteria); filters = findListByCriteria(criteria);
} }
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import static org.kaaproject.kaa.server.common.dao.DaoConstants.PROFILE_FILTER_ENDPOINT_SCHEMA_ID; import static org.kaaproject.kaa.server.common.dao.DaoConstants.PROFILE_FILTER_ENDPOINT_SCHEMA_ID;
import static org.kaaproject.kaa.server.common.dao.DaoConstants.PROFILE_FILTER_SERVER_SCHEMA_ID; import static org.kaaproject.kaa.server.common.dao.DaoConstants.PROFILE_FILTER_SERVER_SCHEMA_ID;
import static org.kaaproject.kaa.server.common.dao.DaoConstants.PROFILE_FILTER_TABLE_NAME; import static org.kaaproject.kaa.server.common.dao.DaoConstants.PROFILE_FILTER_TABLE_NAME;
import static org.kaaproject.kaa.server.common.dao.model.sql.ModelUtils.getLongId;


@Entity @Entity
@Table(name = PROFILE_FILTER_TABLE_NAME) @Table(name = PROFILE_FILTER_TABLE_NAME)
Expand Down Expand Up @@ -61,11 +62,11 @@ public ProfileFilter(ProfileFilterDto dto) {
this.body = dto.getBody(); this.body = dto.getBody();
String endpointSchemaId = dto.getEndpointProfileSchemaId(); String endpointSchemaId = dto.getEndpointProfileSchemaId();
if (isNotBlank(endpointSchemaId)) { if (isNotBlank(endpointSchemaId)) {
this.endpointProfileSchema = new EndpointProfileSchema(ModelUtils.getLongId(endpointSchemaId)); this.endpointProfileSchema = new EndpointProfileSchema(getLongId(endpointSchemaId));
} }
String serverSchemaId = dto.getServerProfileSchemaId(); String serverSchemaId = dto.getServerProfileSchemaId();
if (isNotBlank(serverSchemaId)) { if (isNotBlank(serverSchemaId)) {
this.serverProfileSchema = new ServerProfileSchema(ModelUtils.getLongId(serverSchemaId)); this.serverProfileSchema = new ServerProfileSchema(getLongId(serverSchemaId));
} }
} }


Expand Down Expand Up @@ -105,21 +106,6 @@ public void setServerProfileSchema(ServerProfileSchema serverProfileSchema) {
this.serverProfileSchema = serverProfileSchema; this.serverProfileSchema = serverProfileSchema;
} }


@Override
public ProfileFilterDto toDto() {
ProfileFilterDto filterDto = super.toDto();
filterDto.setBody(body);
if (endpointProfileSchema != null) {
filterDto.setEndpointProfileSchemaId(endpointProfileSchema.getStringId());
filterDto.setEndpointProfileSchemaVersion(endpointProfileSchema.getVersion());
}
if (serverProfileSchema != null) {
filterDto.setServerProfileSchemaId(serverProfileSchema.getStringId());
filterDto.setServerProfileSchemaVersion(serverProfileSchema.getVersion());
}
return filterDto;
}

public String getGroupId() { public String getGroupId() {
return endpointGroup.getStringId(); return endpointGroup.getStringId();
} }
Expand Down Expand Up @@ -155,4 +141,52 @@ public String getServerProfileSchemaId() {
} }
return id; return id;
} }

@Override
public ProfileFilterDto toDto() {
ProfileFilterDto filterDto = super.toDto();
filterDto.setBody(body);
if (endpointProfileSchema != null) {
filterDto.setEndpointProfileSchemaId(endpointProfileSchema.getStringId());
filterDto.setEndpointProfileSchemaVersion(endpointProfileSchema.getVersion());
}
if (serverProfileSchema != null) {
filterDto.setServerProfileSchemaId(serverProfileSchema.getStringId());
filterDto.setServerProfileSchemaVersion(serverProfileSchema.getVersion());
}
return filterDto;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;

ProfileFilter that = (ProfileFilter) o;

if (body != null ? !body.equals(that.body) : that.body != null) return false;
if (endpointProfileSchema != null ? !endpointProfileSchema.equals(that.endpointProfileSchema) : that.endpointProfileSchema != null)
return false;
return serverProfileSchema != null ? serverProfileSchema.equals(that.serverProfileSchema) : that.serverProfileSchema == null;

}

@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + (body != null ? body.hashCode() : 0);
result = 31 * result + (endpointProfileSchema != null ? endpointProfileSchema.hashCode() : 0);
result = 31 * result + (serverProfileSchema != null ? serverProfileSchema.hashCode() : 0);
return result;
}

@Override
public String toString() {
return "ProfileFilter{" +
"body='" + body + '\'' +
", endpointProfileSchema=" + endpointProfileSchema +
", serverProfileSchema=" + serverProfileSchema +
"} " + super.toString();
}
} }
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -410,9 +410,9 @@ private ChangeNotificationDto createNotification(ProfileFilterDto profileFilter,
} }


@Override @Override
public List<ProfileFilterDto> findProfileFiltersByAppIdAndVersions(String appId, int endpointSchemaVersion, int serverSchemaVersion) { public List<ProfileFilterDto> findProfileFiltersByAppIdAndVersionsCombination(String appId, int endpointSchemaVersion, int serverSchemaVersion) {
validateId(appId, "Can't find profile filter. Invalid application id: " + appId); validateId(appId, "Can't find profile filter. Invalid application id: " + appId);
return convertDtoList(profileFilterDao.findByAppIdAndSchemaVersions(appId, endpointSchemaVersion, serverSchemaVersion)); return convertDtoList(profileFilterDao.findByAppIdAndSchemaVersionsCombination(appId, endpointSchemaVersion, serverSchemaVersion));
} }


@Override @Override
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -287,10 +287,14 @@ protected List<NotificationSchema> generateNotificationSchema(Application app, i
} }


protected List<ProfileFilter> generateFilter(EndpointProfileSchema schema, ServerProfileSchema srvSchema, EndpointGroup group, int count, UpdateStatus status) { protected List<ProfileFilter> generateFilter(EndpointProfileSchema schema, ServerProfileSchema srvSchema, EndpointGroup group, int count, UpdateStatus status) {
return generateFilter(generateApplication(null), schema, srvSchema, group, count, status);
}

protected List<ProfileFilter> generateFilter(Application app, EndpointProfileSchema schema, ServerProfileSchema srvSchema, EndpointGroup group, int count, UpdateStatus status) {
if (schema == null) { if (schema == null) {
schema = generateProfSchema(null, 1).get(0); schema = generateProfSchema(app, 1).get(0);
} }
Application app = schema.getApplication();
if (srvSchema == null) { if (srvSchema == null) {
srvSchema = new ServerProfileSchema(generateServerProfileSchema(app.getStringId(), app.getTenant().getStringId())); srvSchema = new ServerProfileSchema(generateServerProfileSchema(app.getStringId(), app.getTenant().getStringId()));
} }
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.kaaproject.kaa.server.common.dao.model.sql.Application; import org.kaaproject.kaa.server.common.dao.model.sql.Application;
import org.kaaproject.kaa.server.common.dao.model.sql.EndpointGroup; import org.kaaproject.kaa.server.common.dao.model.sql.EndpointGroup;
import org.kaaproject.kaa.server.common.dao.model.sql.EndpointProfileSchema; import org.kaaproject.kaa.server.common.dao.model.sql.EndpointProfileSchema;
import org.kaaproject.kaa.server.common.dao.model.sql.ModelUtils;
import org.kaaproject.kaa.server.common.dao.model.sql.ProfileFilter; import org.kaaproject.kaa.server.common.dao.model.sql.ProfileFilter;
import org.kaaproject.kaa.server.common.dao.model.sql.ServerProfileSchema; import org.kaaproject.kaa.server.common.dao.model.sql.ServerProfileSchema;
import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.annotation.DirtiesContext;
Expand Down Expand Up @@ -112,12 +111,17 @@ public void findLatestDeprecated() {


@Test @Test
public void findByAppIdAndSchemaVersion() { public void findByAppIdAndSchemaVersion() {
List<ProfileFilter> filters = generateFilter(null, null, null, 1, UpdateStatus.ACTIVE); Application app = generateApplication(null);
Assert.assertEquals(1, filters.size()); EndpointProfileSchema schema = generateProfSchema(app, 1).get(0);
ProfileFilter first = filters.get(0); ServerProfileSchemaDto srvSchema = generateServerProfileSchema(app.getStringId(), app.getTenant().getStringId(), 101);
Application app = first.getApplication();
List<ProfileFilter> found = profileFilterDao.findByAppIdAndSchemaVersions(app.getStringId(), first.getEndpointProfileSchemaVersion() generateFilter(schema, new ServerProfileSchema(srvSchema), null, 2, UpdateStatus.ACTIVE);
, first.getServerProfileSchemaVersion());
List<ProfileFilter> filters = generateFilterWithoutSchemaGeneration(schema, new ServerProfileSchema(srvSchema), null, 1, UpdateStatus.ACTIVE);
filters.addAll(generateFilterWithoutSchemaGeneration(null, new ServerProfileSchema(srvSchema), null, 1, UpdateStatus.ACTIVE));
filters.addAll(generateFilterWithoutSchemaGeneration(schema, null,null, 1, UpdateStatus.ACTIVE));

List<ProfileFilter> found = profileFilterDao.findByAppIdAndSchemaVersionsCombination(app.getStringId(), schema.getVersion(), srvSchema.getVersion());
Assert.assertEquals(filters, found); Assert.assertEquals(filters, found);
} }


Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public void activateProfileFilterNoProfileFilterTest() {
public void findProfileFilterByAppIdAndVersionTest() { public void findProfileFilterByAppIdAndVersionTest() {
List<ProfileFilterDto> generated = generateFilterDto(null, null, null, 1, true); List<ProfileFilterDto> generated = generateFilterDto(null, null, null, 1, true);
ProfileFilterDto first = generated.get(0); ProfileFilterDto first = generated.get(0);
List<ProfileFilterDto> filters = profileService.findProfileFiltersByAppIdAndVersions(first.getApplicationId(), first.getEndpointProfileSchemaVersion(), List<ProfileFilterDto> filters = profileService.findProfileFiltersByAppIdAndVersionsCombination(first.getApplicationId(), first.getEndpointProfileSchemaVersion(),
first.getServerProfileSchemaVersion()); first.getServerProfileSchemaVersion());
Assert.assertNotNull(filters); Assert.assertNotNull(filters);
Assert.assertFalse(filters.isEmpty()); Assert.assertFalse(filters.isEmpty());
Expand Down Expand Up @@ -222,7 +222,7 @@ public void saveMoreFiltersTest() {
ApplicationDto app = applicationService.findAppById(schema.getApplicationId()); ApplicationDto app = applicationService.findAppById(schema.getApplicationId());
ServerProfileSchemaDto serverSchema = generateServerProfileSchema(app.getId(), app.getTenantId(), 1); ServerProfileSchemaDto serverSchema = generateServerProfileSchema(app.getId(), app.getTenantId(), 1);
generateFilterDto(schema.getId(), serverSchema.getId(), null, 10, true); generateFilterDto(schema.getId(), serverSchema.getId(), null, 10, true);
List<ProfileFilterDto> filters = profileService.findProfileFiltersByAppIdAndVersions(schema.getApplicationId(), schema.getVersion(), serverSchema.getVersion()); List<ProfileFilterDto> filters = profileService.findProfileFiltersByAppIdAndVersionsCombination(schema.getApplicationId(), schema.getVersion(), serverSchema.getVersion());
Assert.assertFalse(filters.isEmpty()); Assert.assertFalse(filters.isEmpty());
Assert.assertEquals(filters.get(0).getStatus(), UpdateStatus.ACTIVE); Assert.assertEquals(filters.get(0).getStatus(), UpdateStatus.ACTIVE);
Assert.assertEquals(schema.getId(), filters.get(0).getEndpointProfileSchemaId()); Assert.assertEquals(schema.getId(), filters.get(0).getEndpointProfileSchemaId());
Expand Down Expand Up @@ -257,7 +257,7 @@ public void saveFiltersWithDiferentGroupsTest() {
ChangeProfileFilterNotification activeTwo = profileService.activateProfileFilter(savedTwo.getId(), null); ChangeProfileFilterNotification activeTwo = profileService.activateProfileFilter(savedTwo.getId(), null);
Assert.assertNotNull(activeTwo); Assert.assertNotNull(activeTwo);


List<ProfileFilterDto> activeList = profileService.findProfileFiltersByAppIdAndVersions(application.getId(), schema.getVersion(), serverSchema.getVersion()); List<ProfileFilterDto> activeList = profileService.findProfileFiltersByAppIdAndVersionsCombination(application.getId(), schema.getVersion(), serverSchema.getVersion());
Assert.assertFalse(activeList.isEmpty()); Assert.assertFalse(activeList.isEmpty());
Set<String> groupIds = new HashSet<>(); Set<String> groupIds = new HashSet<>();
for (ProfileFilterDto dto : activeList) { for (ProfileFilterDto dto : activeList) {
Expand Down Expand Up @@ -354,7 +354,7 @@ public void deactivateProfileFilterTest() {
ProfileFilterDto pf = filterList.get(0); ProfileFilterDto pf = filterList.get(0);
String appId = pf.getApplicationId(); String appId = pf.getApplicationId();


List<ProfileFilterDto> filters = profileService.findProfileFiltersByAppIdAndVersions(pf.getApplicationId(), List<ProfileFilterDto> filters = profileService.findProfileFiltersByAppIdAndVersionsCombination(pf.getApplicationId(),
pf.getEndpointProfileSchemaVersion(), pf.getServerProfileSchemaVersion()); pf.getEndpointProfileSchemaVersion(), pf.getServerProfileSchemaVersion());


Assert.assertNotNull(filters); Assert.assertNotNull(filters);
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ public List<ProfileFilterDto> getFilters(AppProfileVersionsKey key) {
public List<ProfileFilterDto> compute(AppProfileVersionsKey key) { public List<ProfileFilterDto> compute(AppProfileVersionsKey key) {
LOG.debug("Fetching result for getFilters"); LOG.debug("Fetching result for getFilters");
ApplicationDto appDto = applicationService.findAppByApplicationToken(key.getApplicationToken()); ApplicationDto appDto = applicationService.findAppByApplicationToken(key.getApplicationToken());
List<ProfileFilterDto> value = profileService.findProfileFiltersByAppIdAndVersions(appDto.getId(), List<ProfileFilterDto> value = profileService.findProfileFiltersByAppIdAndVersionsCombination(appDto.getId(),
key.getEndpointProfileSchemaVersion(), key.getServerProfileSchemaVersion()); key.getEndpointProfileSchemaVersion(), key.getServerProfileSchemaVersion());
return value; return value;
} }
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public List<HistoryDto> answer(InvocationOnMock invocation) throws Throwable {
}); });


when( when(
profileService.findProfileFiltersByAppIdAndVersions(APP_ID, TEST_GET_PROFILES_KEY.getEndpointProfileSchemaVersion(), profileService.findProfileFiltersByAppIdAndVersionsCombination(APP_ID, TEST_GET_PROFILES_KEY.getEndpointProfileSchemaVersion(),
TEST_GET_PROFILES_KEY.getServerProfileSchemaVersion())).then(new Answer<List<ProfileFilterDto>>() { TEST_GET_PROFILES_KEY.getServerProfileSchemaVersion())).then(new Answer<List<ProfileFilterDto>>() {
@Override @Override
public List<ProfileFilterDto> answer(InvocationOnMock invocation) throws Throwable { public List<ProfileFilterDto> answer(InvocationOnMock invocation) throws Throwable {
Expand Down Expand Up @@ -477,12 +477,12 @@ public void run() {
@Test @Test
public void testGetFilters() throws GetDeltaException { public void testGetFilters() throws GetDeltaException {
assertEquals(TEST_PROFILE_FILTER_LIST, cacheService.getFilters(TEST_GET_PROFILES_KEY)); assertEquals(TEST_PROFILE_FILTER_LIST, cacheService.getFilters(TEST_GET_PROFILES_KEY));
verify(profileService, times(1)).findProfileFiltersByAppIdAndVersions(APP_ID, TEST_GET_PROFILES_KEY.getEndpointProfileSchemaVersion(), verify(profileService, times(1)).findProfileFiltersByAppIdAndVersionsCombination(APP_ID, TEST_GET_PROFILES_KEY.getEndpointProfileSchemaVersion(),
TEST_GET_PROFILES_KEY.getServerProfileSchemaVersion()); TEST_GET_PROFILES_KEY.getServerProfileSchemaVersion());
reset(profileService); reset(profileService);


assertEquals(TEST_PROFILE_FILTER_LIST, cacheService.getFilters(TEST_GET_PROFILES_KEY)); assertEquals(TEST_PROFILE_FILTER_LIST, cacheService.getFilters(TEST_GET_PROFILES_KEY));
verify(profileService, times(0)).findProfileFiltersByAppIdAndVersions(APP_ID, TEST_GET_PROFILES_KEY.getEndpointProfileSchemaVersion(), verify(profileService, times(0)).findProfileFiltersByAppIdAndVersionsCombination(APP_ID, TEST_GET_PROFILES_KEY.getEndpointProfileSchemaVersion(),
TEST_GET_PROFILES_KEY.getServerProfileSchemaVersion()); TEST_GET_PROFILES_KEY.getServerProfileSchemaVersion());
reset(profileService); reset(profileService);
} }
Expand All @@ -497,7 +497,7 @@ public void run() {
} }
}); });


verify(profileService, atMost(2)).findProfileFiltersByAppIdAndVersions(APP_ID, TEST_GET_PROFILES_KEY.getEndpointProfileSchemaVersion(), verify(profileService, atMost(2)).findProfileFiltersByAppIdAndVersionsCombination(APP_ID, TEST_GET_PROFILES_KEY.getEndpointProfileSchemaVersion(),
TEST_GET_PROFILES_KEY.getServerProfileSchemaVersion()); TEST_GET_PROFILES_KEY.getServerProfileSchemaVersion());
reset(profileService); reset(profileService);
} }
Expand Down

0 comments on commit 62859fa

Please sign in to comment.