Skip to content

Commit

Permalink
KAA-1423: code review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
agulenko committed Oct 6, 2016
1 parent fc715b4 commit 9e5c62a
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 34 deletions.
Expand Up @@ -529,15 +529,15 @@ public void editUserConfiguration(EndpointUserConfigurationDto endpointUserConfi
}

public EndpointSpecificConfigurationDto editEndpointSpecificConfiguration(EndpointSpecificConfigurationDto configuration) throws Exception {
return restTemplate.postForObject(restTemplate.getUrl() + "endpointConfiguration", configuration, EndpointSpecificConfigurationDto.class);
return restTemplate.postForObject(restTemplate.getUrl() + "endpointSpecificConfiguration", configuration, EndpointSpecificConfigurationDto.class);
}

public void deleteEndpointSpecificConfiguration(String endpointKeyHash) throws Exception {
restTemplate.delete(restTemplate.getUrl() + "endpointConfiguration/" + endpointKeyHash);
restTemplate.delete(restTemplate.getUrl() + "endpointSpecificConfiguration/" + endpointKeyHash);
}

public EndpointSpecificConfigurationDto findEndpointSpecificConfiguration(String endpointKeyHash) throws Exception {
return restTemplate.getForObject(restTemplate.getUrl() + "endpointConfiguration/" + endpointKeyHash, EndpointSpecificConfigurationDto.class);
return restTemplate.getForObject(restTemplate.getUrl() + "endpointSpecificConfiguration/" + endpointKeyHash, EndpointSpecificConfigurationDto.class);
}

public ProfileFilterDto editProfileFilter(ProfileFilterDto profileFilter) throws Exception {
Expand Down
Expand Up @@ -192,7 +192,14 @@ public interface ConfigurationService {
*/
ConfigurationSchemaDto saveConfSchema(ConfigurationSchemaDto configurationSchema);

String validateConfiguration(String appId, int schemaVersion, String configurationBody);
/**
* Validate configuration according to override configuration schema.
*
* @param appId application ID
* @param schemaVersion configuration schema version
* @param configurationBody configuration to validate
*/
String validateOverrideConfigurationBody(String appId, int schemaVersion, String configurationBody);

/**
* Find configuration schema by id.
Expand Down
Expand Up @@ -469,7 +469,7 @@ public ConfigurationSchemaDto saveConfSchema(ConfigurationSchemaDto configuratio
}

@Override
public String validateConfiguration(String appId, int schemaVersion, String configurationBody) {
public String validateOverrideConfigurationBody(String appId, int schemaVersion, String configurationBody) {
ConfigurationSchemaDto schemaDto = this.findConfSchemaByAppIdAndVersion(appId, schemaVersion);
if (schemaDto != null) {
OverrideSchema overrideSchema = new OverrideSchema(schemaDto.getOverrideSchema());
Expand Down
Expand Up @@ -78,7 +78,7 @@ public Optional<EndpointSpecificConfigurationDto> deleteByEndpointKeyHash(String
public EndpointSpecificConfigurationDto save(EndpointSpecificConfigurationDto configurationDto) {
EndpointProfileDto profileDto = getEndpointProfileDto(configurationDto.getEndpointKeyHash());
configurationDto.setConfigurationVersion(profileDto.getConfigurationVersion());
validateConfigurationBody(configurationDto, profileDto);
validateEndpointSpecificConfiguration(configurationDto, profileDto);
return endpointSpecificConfigurationDao.save(configurationDto).toDto();
}

Expand All @@ -88,13 +88,12 @@ private EndpointProfileDto getEndpointProfileDto(String endpointKeyHash) {
}


private void validateConfigurationBody(EndpointSpecificConfigurationDto configurationDto, EndpointProfileDto ep) {
private void validateEndpointSpecificConfiguration(EndpointSpecificConfigurationDto configurationDto, EndpointProfileDto ep) {
validateString(configurationDto.getConfiguration(), "Endpoint specific configuration body is required");
int configurationVersion = configurationDto.getConfigurationVersion();
String appId = ep.getApplicationId();
String configurationBody = configurationDto.getConfiguration();
configurationBody = configurationService.validateConfiguration(appId, configurationVersion, configurationBody);
validateString(configurationBody, "Provided configuration body is invalid");
configurationBody = configurationService.validateOverrideConfigurationBody(appId, configurationVersion, configurationBody);
configurationDto.setConfiguration(configurationBody);
}

Expand Down
Expand Up @@ -62,7 +62,7 @@ public EndpointUserConfigurationDto saveUserConfiguration(EndpointUserConfigurat
ApplicationDto applicationDto = applicationService.findAppByApplicationToken(appToken);
if (applicationDto != null) {
int schemaVersion = userConfig.getSchemaVersion();
userConfig.setBody(configurationService.validateConfiguration(applicationDto.getId(), schemaVersion, userConfigBody));
userConfig.setBody(configurationService.validateOverrideConfigurationBody(applicationDto.getId(), schemaVersion, userConfigBody));
userConfigurationDto = getDto(endpointUserConfigurationDao.save(userConfig));
} else {
LOG.warn("Can't find application with token {} for endpoint user configuration.", appToken);
Expand Down
Expand Up @@ -60,25 +60,25 @@ public void shouldSave() {
dto.setEndpointKeyHash(KEY);
dto.setConfiguration(CONFIG_BODY);
when(endpointServiceMock.findEndpointProfileByKeyHash(DECODED_KEY)).thenReturn(generateProfile());
when(configurationServiceMock.validateConfiguration(APP_ID, CONF_VERSION, CONFIG_BODY)).thenReturn("valid body");
when(configurationServiceMock.validateOverrideConfigurationBody(APP_ID, CONF_VERSION, CONFIG_BODY)).thenReturn("valid body");
when(daoMock.save(dto)).thenReturn(configuration);
when(configuration.toDto()).thenReturn(new EndpointSpecificConfigurationDto());
Assert.assertTrue(SERVICE.save(dto) != null);
verify(configurationServiceMock).validateConfiguration(APP_ID, CONF_VERSION, CONFIG_BODY);
verify(configurationServiceMock).validateOverrideConfigurationBody(APP_ID, CONF_VERSION, CONFIG_BODY);
verify(endpointServiceMock).findEndpointProfileByKeyHash(DECODED_KEY);
verify(daoMock).save(dto);
}

@Test
public void shouldFoundByEndpointProfile() {
public void testShouldFoundByEndpointProfile() {
when(daoMock.findByEndpointKeyHashAndConfigurationVersion(KEY, CONF_VERSION)).thenReturn(configuration);
when(configuration.toDto()).thenReturn(new EndpointSpecificConfigurationDto());
Assert.assertTrue(SERVICE.findByEndpointProfile(generateProfile()).isPresent());
verify(daoMock).findByEndpointKeyHashAndConfigurationVersion(KEY, CONF_VERSION);
}

@Test
public void shouldFoundByEndpointKeyHash() {
public void testShouldFoundByEndpointKeyHash() {
when(endpointServiceMock.findEndpointProfileByKeyHash(DECODED_KEY)).thenReturn(generateProfile());
when(daoMock.findByEndpointKeyHashAndConfigurationVersion(KEY, CONF_VERSION)).thenReturn(configuration);
when(configuration.toDto()).thenReturn(new EndpointSpecificConfigurationDto());
Expand All @@ -88,26 +88,25 @@ public void shouldFoundByEndpointKeyHash() {
}

@Test
public void shouldNotFoundByEndpointKeyHashWhenProfileNotFound() {
public void testShouldNotFoundByEndpointKeyHashWhenProfileNotFound() {
when(endpointServiceMock.findEndpointProfileByKeyHash(DECODED_KEY)).thenReturn(null);
Assert.assertFalse(SERVICE.findByEndpointKeyHash(KEY).isPresent());
verify(endpointServiceMock).findEndpointProfileByKeyHash(DECODED_KEY);
}

@Test
public void shouldDeleteByEndpointKeyHash() {
public void testShouldDeleteByEndpointKeyHash() {
when(endpointServiceMock.findEndpointProfileByKeyHash(DECODED_KEY)).thenReturn(generateProfile());
when(daoMock.findByEndpointKeyHashAndConfigurationVersion(KEY, CONF_VERSION)).thenReturn(configuration);
when(configuration.toDto()).thenReturn(new EndpointSpecificConfigurationDto());
Assert.assertTrue(SERVICE.deleteByEndpointKeyHash(KEY).isPresent());
verify(endpointServiceMock).findEndpointProfileByKeyHash(DECODED_KEY);
verify(daoMock).findByEndpointKeyHashAndConfigurationVersion(KEY, CONF_VERSION);
verify(daoMock).removeByEndpointKeyHash(KEY);

}

@Test
public void shouldNotDeleteByEndpointKeyHashWhenProfileNotFound() {
public void testShouldNotDeleteByEndpointKeyHashWhenProfileNotFound() {
when(endpointServiceMock.findEndpointProfileByKeyHash(DECODED_KEY)).thenReturn(generateProfile());
when(daoMock.findByEndpointKeyHashAndConfigurationVersion(KEY, CONF_VERSION)).thenReturn(null);
Assert.assertFalse(SERVICE.deleteByEndpointKeyHash(KEY).isPresent());
Expand Down
Expand Up @@ -301,7 +301,7 @@ public void editUserConfiguration(
@ApiResponse(code = 404, message = "Specified endpoint does not exist"),
@ApiResponse(code = 409, message = "Can't update entity with provided version. Entity already changed"),
@ApiResponse(code = 500, message = "An unexpected error occurred on the server side")})
@RequestMapping(value = "endpointConfiguration", method = RequestMethod.POST)
@RequestMapping(value = "endpointSpecificConfiguration", method = RequestMethod.POST)
@ResponseStatus(value = HttpStatus.OK)
@ResponseBody
public EndpointSpecificConfigurationDto editEndpointSpecificConfiguration(
Expand All @@ -327,7 +327,7 @@ public EndpointSpecificConfigurationDto editEndpointSpecificConfiguration(
"of the application does not match the Tenant ID of the authenticated user"),
@ApiResponse(code = 404, message = "Specified endpoint does not exist"),
@ApiResponse(code = 500, message = "An unexpected error occurred on the server side")})
@RequestMapping(value = "endpointConfiguration/{endpointKeyHash}", method = RequestMethod.GET)
@RequestMapping(value = "endpointSpecificConfiguration/{endpointKeyHash}", method = RequestMethod.GET)
@ResponseStatus(value = HttpStatus.OK)
@ResponseBody
public EndpointSpecificConfigurationDto findEndpointSpecificConfiguration(
Expand All @@ -352,7 +352,7 @@ public EndpointSpecificConfigurationDto findEndpointSpecificConfiguration(
"of the application does not match the Tenant ID of the authenticated user"),
@ApiResponse(code = 404, message = "Specified endpoint does not exist"),
@ApiResponse(code = 500, message = "An unexpected error occurred on the server side")})
@RequestMapping(value = "endpointConfiguration/{endpointKeyHash}", method = RequestMethod.DELETE)
@RequestMapping(value = "endpointSpecificConfiguration/{endpointKeyHash}", method = RequestMethod.DELETE)
@ResponseStatus(value = HttpStatus.OK)
public void deleteEndpointSpecificConfiguration(
@PathVariable String endpointKeyHash) throws KaaAdminServiceException {
Expand Down
Expand Up @@ -260,6 +260,7 @@ public EndpointSpecificConfigurationDto findEndpointSpecificConfiguration(String

@Override
public EndpointSpecificConfigurationDto deleteEndpointSpecificConfiguration(String endpointKeyHash) throws KaaAdminServiceException {
checkAuthority(KaaAuthorityDto.TENANT_DEVELOPER, KaaAuthorityDto.TENANT_USER);
try {
checkEndpointProfile(endpointKeyHash);
EndpointSpecificConfigurationDto configuration = controlService.deleteEndpointSpecificConfiguration(endpointKeyHash);
Expand Down
Expand Up @@ -95,17 +95,13 @@ private void processServerProfileUpdateMsg(ActorContext context, ThriftServerPro
operationsService.syncServerProfile(appToken, endpointKey, key);
ThriftServerProfileUpdateMessage localMsg = new ThriftServerProfileUpdateMessage(thriftMsg);
localMsg.setActorClassifier(ThriftActorClassifier.LOCAL);
dispatchMsg(context, localMsg, (nodeId, msg) -> {
clusterService.sendServerProfileUpdateMessage(nodeId, msg);
});
dispatchMsg(context, localMsg, clusterService::sendServerProfileUpdateMessage);
}

private void processUnicastNotificationMsg(ActorContext context, ThriftUnicastNotificationMessage thriftMsg) {
ThriftUnicastNotificationMessage localMsg = new ThriftUnicastNotificationMessage(thriftMsg);
localMsg.setActorClassifier(ThriftActorClassifier.LOCAL);
dispatchMsg(context, localMsg, (nodeId, msg) -> {
clusterService.sendUnicastNotificationMessage(nodeId, msg);
});
dispatchMsg(context, localMsg, clusterService::sendUnicastNotificationMessage);
}

private void processEndpointConfigurationRefreshMsg(ActorContext context, ThriftEndpointConfigurationRefreshMessage thriftMsg) {
Expand Down
Expand Up @@ -197,14 +197,22 @@ private void logHashMismatch(GetDeltaRequest request, EndpointProfileDto profile
*/
private ConfigurationCacheEntry getDelta(final String endpointId, final String userId, DeltaCacheKey deltaKey, boolean useConfigurationRawSchema) throws GetDeltaException {
EndpointUserConfigurationDto userConfiguration = findLatestUserConfiguration(userId, deltaKey);
Optional<EndpointSpecificConfigurationDto> epsConfigOpt = Optional.empty();
if (endpointId != null) {
epsConfigOpt = endpointSpecificConfigurationService.findByEndpointKeyHash(endpointId);
}
EndpointSpecificConfigurationDto epsConfig = epsConfigOpt.orElse(null);

EndpointSpecificConfigurationDto epsConfig = Optional.ofNullable(endpointId)
.flatMap(endpointSpecificConfigurationService::findByEndpointKeyHash)
.orElse(null);

EndpointObjectHash epsConfHash = Optional.ofNullable(epsConfig)
.map(EndpointSpecificConfigurationDto::getConfiguration)
.map(EndpointObjectHash::fromString)
.orElse(null);

EndpointObjectHash userConfHash = Optional.ofNullable(userConfiguration)
.map(EndpointUserConfigurationDto::getBody)
.map(EndpointObjectHash::fromString)
.orElse(null);

final DeltaCacheKey newKey;
EndpointObjectHash userConfHash = userConfiguration != null ? EndpointObjectHash.fromString(userConfiguration.getBody()) : null;
EndpointObjectHash epsConfHash = epsConfigOpt.map(conf -> EndpointObjectHash.fromString(conf.getConfiguration())).orElse(null);
if (userConfiguration != null || epsConfig != null) {
newKey = new DeltaCacheKey(
deltaKey.getAppConfigVersionKey(),
Expand Down

0 comments on commit 9e5c62a

Please sign in to comment.