Skip to content

Commit

Permalink
Changed promoteScopeToTenant method signature and related tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirill380 committed Jun 3, 2016
1 parent debc0f1 commit c4989b1
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 65 deletions.
Expand Up @@ -1275,8 +1275,11 @@ public boolean checkFqnExistsWithAppToken(String fqn, String tenantId, String ap
}
}

public CTLSchemaMetaInfoDto promoteScopeToTenant(CTLSchemaMetaInfoDto ctlSchemaMetaInfo) {
return restTemplate.postForObject(restTemplate.getUrl() + "CTL/promoteScopeToTenant", ctlSchemaMetaInfo, CTLSchemaMetaInfoDto.class);
public CTLSchemaMetaInfoDto promoteScopeToTenant(String applicationId, String fqn) {
MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
params.add("applicationId", applicationId);
params.add("fqn", fqn);
return restTemplate.postForObject(restTemplate.getUrl() + "CTL/promoteScopeToTenant", params, CTLSchemaMetaInfoDto.class);
}

public List<CTLSchemaMetaInfoDto> getSystemLevelCTLSchemas() {
Expand Down
Expand Up @@ -98,7 +98,8 @@ public void onClick(ClickEvent event) {
@Override
public void onClick(ClickEvent event) {
CTLSchemaMetaInfoDto metaInfo = entity.getMetaInfo();
KaaAdmin.getDataSource().promoteScopeToTenant(metaInfo, new BusyAsyncCallback<CTLSchemaMetaInfoDto>() {

KaaAdmin.getDataSource().promoteScopeToTenant(metaInfo.getApplicationId(), metaInfo.getFqn(), new BusyAsyncCallback<CTLSchemaMetaInfoDto>() {
@Override
public void onFailureImpl(Throwable caught) {
Utils.handleException(caught, detailsView);
Expand Down
Expand Up @@ -856,9 +856,9 @@ protected void onResult(Boolean result) {
});
}

public void promoteScopeToTenant(CTLSchemaMetaInfoDto metaInfo,
public void promoteScopeToTenant(String applicationId, String fqn,
final AsyncCallback<CTLSchemaMetaInfoDto> callback) {
rpcService.promoteScopeToTenant(metaInfo,
rpcService.promoteScopeToTenant(applicationId, fqn,
new DataCallback<CTLSchemaMetaInfoDto>(callback) {
@Override
protected void onResult(CTLSchemaMetaInfoDto result) {
Expand Down
Expand Up @@ -994,8 +994,6 @@ public boolean checkFqnExistsWithAppToken(@RequestParam String fqn,
/**
* Promote existing CTL schema meta info from application to tenant scope
*
* @param ctlSchemaMetaInfo
* the CTL schema meta info object.
*
* @throws KaaAdminServiceException
* the kaa admin service exception
Expand All @@ -1004,9 +1002,9 @@ public boolean checkFqnExistsWithAppToken(@RequestParam String fqn,
*/
@RequestMapping(value = "CTL/promoteScopeToTenant", method = RequestMethod.POST)
@ResponseBody
public CTLSchemaMetaInfoDto promoteScopeToTenant(@RequestBody CTLSchemaMetaInfoDto ctlSchemaMetaInfo)
public CTLSchemaMetaInfoDto promoteScopeToTenant(@RequestParam String applicationId, @RequestParam String fqn)
throws KaaAdminServiceException {
return kaaAdminService.promoteScopeToTenant(ctlSchemaMetaInfo);
return kaaAdminService.promoteScopeToTenant(applicationId, fqn);
}

/**
Expand Down
Expand Up @@ -3343,24 +3343,30 @@ public boolean checkFqnExistsWithAppToken(String fqn, String tenantId, String ap


@Override
public CTLSchemaMetaInfoDto promoteScopeToTenant(CTLSchemaMetaInfoDto ctlSchemaMetaInfo)
public CTLSchemaMetaInfoDto promoteScopeToTenant(String applicationId, String fqn)
throws KaaAdminServiceException {
checkAuthority(KaaAuthorityDto.values());
checkCTLSchemaEditScope(ctlSchemaMetaInfo.getTenantId(), ctlSchemaMetaInfo.getApplicationId());
final String tenantId = getTenantId();
checkCTLSchemaEditScope(tenantId, applicationId);

final String fqn = ctlSchemaMetaInfo.getFqn();
final String tenantId = ctlSchemaMetaInfo.getTenantId();
final String applicationId = ctlSchemaMetaInfo.getApplicationId();
List<Integer> versions = ctlSchemaMetaInfo.getVersions();
ctlSchemaMetaInfo.setApplicationId(null); // promote scope to tenant
try {
Set<CTLSchemaDto> dependencies = new HashSet<>();
List<Integer> versions = controlService.getAllCTLSchemaVersionsByFqnTenantIdAndApplicationId(fqn, tenantId, applicationId);
ApplicationDto application = controlService.getApplication(applicationId);
checkApplicationToken(application.getApplicationToken());

if(versions.isEmpty()) {
throw new KaaAdminServiceException("The requested item was not found!", ServiceErrorCode.ITEM_NOT_FOUND);
}

// meta info same for all versions
CTLSchemaMetaInfoDto ctlSchemaMetaInfo = controlService.getCTLSchemaByFqnVersionTenantIdAndApplicationId(fqn, versions.get(0), tenantId, applicationId).getMetaInfo();
ctlSchemaMetaInfo.setApplicationId(null); //promote to tenant

// get dep of all versions
for (Integer version : versions) {
CTLSchemaDto schemaFound = controlService.getCTLSchemaByFqnVersionTenantIdAndApplicationId(fqn, version, tenantId, applicationId);
Utils.checkNotNull(schemaFound);
Set<CTLSchemaDto> schemaDependents = schemaFound.getDependencySet();
CTLSchemaDto schema = controlService.getCTLSchemaByFqnVersionTenantIdAndApplicationId(fqn, version, tenantId, applicationId);
Set<CTLSchemaDto> schemaDependents = schema.getDependencySet();
dependencies.addAll(schemaDependents.stream()
.filter(dep -> dep.getMetaInfo().getScope() == CTLSchemaScopeDto.APPLICATION)
.collect(Collectors.toList()));
Expand All @@ -3373,7 +3379,6 @@ public CTLSchemaMetaInfoDto promoteScopeToTenant(CTLSchemaMetaInfoDto ctlSchemaM
throw new KaaAdminServiceException(message, ServiceErrorCode.GENERAL_ERROR);
}


return controlService.updateCTLSchemaMetaInfoScope(ctlSchemaMetaInfo);
} catch (Exception cause) {
throw Utils.handleException(cause);
Expand Down
Expand Up @@ -409,7 +409,7 @@ public interface KaaAdminService extends RemoteService {

boolean checkFqnExistsWithAppToken(String fqn, String tenantId, String applicationToken) throws KaaAdminServiceException;

CTLSchemaMetaInfoDto promoteScopeToTenant(CTLSchemaMetaInfoDto ctlSchemaMetaInfo) throws KaaAdminServiceException;
CTLSchemaMetaInfoDto promoteScopeToTenant(String applicationToken, String fqn) throws KaaAdminServiceException;

List<CTLSchemaMetaInfoDto> getSystemLevelCTLSchemas() throws KaaAdminServiceException;

Expand Down
Expand Up @@ -36,7 +36,6 @@

/**
* @author Bohdan Khablenko
*
* @since v0.8.0
*/
public class ControlServerCTLSchemaIT extends AbstractTestControlServer {
Expand Down Expand Up @@ -167,7 +166,7 @@ public void executeRestCall() throws Exception {
@Test
public void deleteCTLSchemaWithDependents() throws Exception {
this.loginTenantDeveloper(tenantDeveloperUser);
final CTLSchemaDto dependency = this.createCTLSchema(this.ctlRandomFieldType(), CTL_DEFAULT_NAMESPACE, 1, tenantDeveloperDto.getTenantId(), null, null, null);
final CTLSchemaDto dependency = this.createCTLSchema(this.ctlRandomFieldType(), CTL_DEFAULT_NAMESPACE, 1, tenantDeveloperDto.getTenantId(), null, null, null);

Set<FqnVersion> dependencies = new HashSet<>();
dependencies.add(new FqnVersion(dependency.getMetaInfo().getFqn(), dependency.getVersion()));
Expand All @@ -192,7 +191,7 @@ public void executeRestCall() throws Exception {
public void deleteCTLSchemaByAppTokenWithDependents() throws Exception {
this.loginTenantDeveloper(tenantDeveloperUser);
final CTLSchemaDto dependency = this.createCTLSchema(this.ctlRandomFieldType(), CTL_DEFAULT_NAMESPACE, 1, tenantDeveloperDto.getTenantId(),
null, null, null);
null, null, null);

Set<FqnVersion> dependencies = new HashSet<>();
dependencies.add(new FqnVersion(dependency.getMetaInfo().getFqn(), dependency.getVersion()));
Expand Down Expand Up @@ -252,7 +251,7 @@ public void executeRestCall() throws Exception {
@Test
public void getCTLSchemaByFqnAndVersionTest() throws Exception {
this.loginTenantDeveloper(tenantDeveloperUser);
CTLSchemaDto saved = this.createCTLSchema(this.ctlRandomFieldType(), CTL_DEFAULT_NAMESPACE, 1, tenantDeveloperDto.getTenantId(), null, null, null);
CTLSchemaDto saved = this.createCTLSchema(this.ctlRandomFieldType(), CTL_DEFAULT_NAMESPACE, 1, tenantDeveloperDto.getTenantId(), null, null, null);
CTLSchemaDto loaded = client.getCTLSchemaByFqnVersionTenantIdAndApplicationId(saved.getMetaInfo().getFqn(), saved.getVersion(), tenantDeveloperDto.getTenantId(), null);
Assert.assertNotNull(loaded);
Assert.assertEquals(saved, loaded);
Expand All @@ -266,7 +265,7 @@ public void getCTLSchemaByFqnAndVersionTest() throws Exception {
@Test
public void getCTLSchemaWithAppTokenByFqnAndVersionTest() throws Exception {
this.loginTenantDeveloper(tenantDeveloperUser);
CTLSchemaDto saved = this.createCTLSchema(this.ctlRandomFieldType(), CTL_DEFAULT_NAMESPACE, 1, tenantDeveloperDto.getTenantId(), null, null, null);
CTLSchemaDto saved = this.createCTLSchema(this.ctlRandomFieldType(), CTL_DEFAULT_NAMESPACE, 1, tenantDeveloperDto.getTenantId(), null, null, null);
CTLSchemaDto loaded = client.getCTLSchemaByFqnVersionTenantIdAndApplicationToken(saved.getMetaInfo().getFqn(), saved.getVersion(),
tenantDeveloperDto.getTenantId(), null);
Assert.assertNotNull(loaded);
Expand Down Expand Up @@ -313,7 +312,7 @@ public void getSystemCTLSchemaWithAppTokenByFqnAndVersionTest() throws Exception
@Test
public void getCTLSchemaByIdTest() throws Exception {
this.loginTenantDeveloper(tenantDeveloperUser);
CTLSchemaDto saved = this.createCTLSchema(this.ctlRandomFieldType(), CTL_DEFAULT_NAMESPACE, 1, tenantDeveloperDto.getTenantId(), null, null, null);
CTLSchemaDto saved = this.createCTLSchema(this.ctlRandomFieldType(), CTL_DEFAULT_NAMESPACE, 1, tenantDeveloperDto.getTenantId(), null, null, null);
CTLSchemaDto loaded = client.getCTLSchemaById(saved.getId());
Assert.assertNotNull(loaded);
Assert.assertEquals(saved, loaded);
Expand All @@ -328,7 +327,7 @@ public void getCTLSchemaByIdTest() throws Exception {
public void downloadCtlSchemaTest() throws Exception {
this.loginTenantDeveloper(tenantDeveloperUser);
String name = this.ctlRandomFieldType();
CTLSchemaDto saved = this.createCTLSchema(name, CTL_DEFAULT_NAMESPACE, 1, tenantDeveloperDto.getTenantId(), null, null, null);
CTLSchemaDto saved = this.createCTLSchema(name, CTL_DEFAULT_NAMESPACE, 1, tenantDeveloperDto.getTenantId(), null, null, null);
FileData fd = client.downloadCtlSchema(client.getCTLSchemaById(saved.getId()), CTLSchemaExportMethod.FLAT);
Assert.assertNotNull(fd);
Schema loaded = new Parser().parse(new String(fd.getFileData()));
Expand All @@ -345,7 +344,7 @@ public void downloadCtlSchemaByAppTokenTest() throws Exception {
ApplicationDto application = createApplication(tenantAdminDto);
this.loginTenantDeveloper(tenantDeveloperUser);
String name = this.ctlRandomFieldType();
CTLSchemaDto saved = this.createCTLSchema(name, CTL_DEFAULT_NAMESPACE, 1, tenantDeveloperDto.getTenantId(), application.getId(), null, null);
CTLSchemaDto saved = this.createCTLSchema(name, CTL_DEFAULT_NAMESPACE, 1, tenantDeveloperDto.getTenantId(), application.getId(), null, null);
FileData fd = client.downloadCtlSchemaByAppToken(client.getCTLSchemaById(saved.getId()), CTLSchemaExportMethod.FLAT, application.getApplicationToken());
Assert.assertNotNull(fd);
Schema loaded = new Parser().parse(new String(fd.getFileData()));
Expand All @@ -360,29 +359,29 @@ public void downloadCtlSchemaByAppTokenTest() throws Exception {
@Test
public void checkCTLFqnExistsTest() throws Exception {
this.loginTenantDeveloper(tenantDeveloperUser);
CTLSchemaDto saved = this.createCTLSchema(this.ctlRandomFieldType(), CTL_DEFAULT_NAMESPACE, 1, tenantDeveloperDto.getTenantId(), null, null, null);
CTLSchemaDto saved = this.createCTLSchema(this.ctlRandomFieldType(), CTL_DEFAULT_NAMESPACE, 1, tenantDeveloperDto.getTenantId(), null, null, null);
String fqn = saved.getMetaInfo().getFqn();
boolean result = client.checkFqnExists(fqn, tenantDeveloperDto.getTenantId(), null);
Assert.assertFalse(result);
result = client.checkFqnExists(fqn, tenantDeveloperDto.getTenantId(), "123");
Assert.assertFalse(result);
result = client.checkFqnExists(fqn, null, null);
Assert.assertFalse(result);

ApplicationDto application1 = createApplication(tenantAdminDto);
ApplicationDto application2 = createApplication(tenantAdminDto);
this.loginTenantDeveloper(tenantDeveloperUser);
CTLSchemaDto schema1 = this.createCTLSchema("TestAppFqn1", CTL_DEFAULT_NAMESPACE, 1, tenantDeveloperDto.getTenantId(), application1.getId(), null, null);
this.createCTLSchema("TestAppFqn1", CTL_DEFAULT_NAMESPACE, 1, tenantDeveloperDto.getTenantId(), application2.getId(), null, null);
CTLSchemaDto schema1 = this.createCTLSchema("TestAppFqn1", CTL_DEFAULT_NAMESPACE, 1, tenantDeveloperDto.getTenantId(), application1.getId(), null, null);
this.createCTLSchema("TestAppFqn1", CTL_DEFAULT_NAMESPACE, 1, tenantDeveloperDto.getTenantId(), application2.getId(), null, null);

fqn = schema1.getMetaInfo().getFqn();

result = client.checkFqnExists(fqn, tenantDeveloperDto.getTenantId(), application1.getId());
Assert.assertTrue(result);

result = client.checkFqnExists(fqn, tenantDeveloperDto.getTenantId(), application2.getId());
Assert.assertTrue(result);

result = client.checkFqnExists(fqn, tenantDeveloperDto.getTenantId(), null);
Assert.assertFalse(result);
}
Expand All @@ -395,7 +394,7 @@ public void checkCTLFqnExistsTest() throws Exception {
@Test
public void checkCTLFqnExistsWithAppTokenTest() throws Exception {
this.loginTenantDeveloper(tenantDeveloperUser);
CTLSchemaDto saved = this.createCTLSchema(this.ctlRandomFieldType(), CTL_DEFAULT_NAMESPACE, 1, tenantDeveloperDto.getTenantId(), null, null, null);
CTLSchemaDto saved = this.createCTLSchema(this.ctlRandomFieldType(), CTL_DEFAULT_NAMESPACE, 1, tenantDeveloperDto.getTenantId(), null, null, null);
String fqn = saved.getMetaInfo().getFqn();
boolean result = client.checkFqnExistsWithAppToken(fqn, tenantDeveloperDto.getTenantId(), null);
Assert.assertFalse(result);
Expand All @@ -405,8 +404,8 @@ public void checkCTLFqnExistsWithAppTokenTest() throws Exception {
ApplicationDto application1 = createApplication(tenantAdminDto);
ApplicationDto application2 = createApplication(tenantAdminDto);
this.loginTenantDeveloper(tenantDeveloperUser);
CTLSchemaDto schema1 = this.createCTLSchema("TestAppFqn1", CTL_DEFAULT_NAMESPACE, 1, tenantDeveloperDto.getTenantId(), application1.getId(), null, null);
this.createCTLSchema("TestAppFqn1", CTL_DEFAULT_NAMESPACE, 1, tenantDeveloperDto.getTenantId(), application2.getId(), null, null);
CTLSchemaDto schema1 = this.createCTLSchema("TestAppFqn1", CTL_DEFAULT_NAMESPACE, 1, tenantDeveloperDto.getTenantId(), application1.getId(), null, null);
this.createCTLSchema("TestAppFqn1", CTL_DEFAULT_NAMESPACE, 1, tenantDeveloperDto.getTenantId(), application2.getId(), null, null);

fqn = schema1.getMetaInfo().getFqn();

Expand All @@ -424,10 +423,9 @@ public void checkCTLFqnExistsWithAppTokenTest() throws Exception {
public void updateCTLSchemaScopeTest() throws Exception {
ApplicationDto application = createApplication(tenantAdminDto);
this.loginTenantDeveloper(tenantDeveloperUser);
CTLSchemaDto saved = this.createCTLSchema(this.ctlRandomFieldType(), CTL_DEFAULT_NAMESPACE, 1, tenantDeveloperDto.getTenantId(), application.getId(), null, null);
CTLSchemaDto saved = this.createCTLSchema(this.ctlRandomFieldType(), CTL_DEFAULT_NAMESPACE, 1, tenantDeveloperDto.getTenantId(), application.getId(), null, null);
CTLSchemaMetaInfoDto metaInfo = saved.getMetaInfo();
metaInfo.setVersions(Arrays.asList(1));
CTLSchemaMetaInfoDto updatedMetaInfo = client.promoteScopeToTenant(metaInfo);
CTLSchemaMetaInfoDto updatedMetaInfo = client.promoteScopeToTenant(metaInfo.getApplicationId(), metaInfo.getFqn());
Assert.assertNull(updatedMetaInfo.getApplicationId());
Assert.assertNotNull(updatedMetaInfo.getTenantId());
Assert.assertEquals(tenantDeveloperDto.getTenantId(), updatedMetaInfo.getTenantId());
Expand All @@ -438,47 +436,31 @@ public void updateCTLSchemaScopeTest() throws Exception {
public void promoteScopeToTenantForbiddenTest() throws Exception {
ApplicationDto application = createApplication(tenantAdminDto);
this.loginTenantDeveloper(tenantDeveloperUser);
CTLSchemaDto saved = this.createCTLSchema(this.ctlRandomFieldType(), CTL_DEFAULT_NAMESPACE, 1, tenantDeveloperDto.getTenantId(), application.getId(), null, null);
final CTLSchemaMetaInfoDto metaInfo = saved.getMetaInfo();

metaInfo.setVersions(Arrays.asList(1));
metaInfo.setTenantId(null);

this.checkForbidden(new TestRestCall() {
@Override
public void executeRestCall() throws Exception {
client.promoteScopeToTenant(metaInfo);
}
});

saved = this.createCTLSchema(this.ctlRandomFieldType(), CTL_DEFAULT_NAMESPACE, 1, tenantDeveloperDto.getTenantId(), null, null, null);
CTLSchemaDto saved = this.createCTLSchema(this.ctlRandomFieldType(), CTL_DEFAULT_NAMESPACE, 1, tenantDeveloperDto.getTenantId(), null, null, null);
final CTLSchemaMetaInfoDto metaInfo2 = saved.getMetaInfo();

metaInfo2.setVersions(Arrays.asList(1));
Assert.assertNull(metaInfo2.getApplicationId());
metaInfo2.setApplicationId(application.getId());

this.checkRestErrorStatusCode(new TestRestCall() {
@Override
public void executeRestCall() throws Exception {
client.promoteScopeToTenant(metaInfo2);
client.promoteScopeToTenant(metaInfo2.getApplicationId(), metaInfo2.getFqn());
}
}, HttpStatus.NOT_FOUND);

this.loginTenantAdmin(tenantAdminUser);
saved = this.createCTLSchema(this.ctlRandomFieldType(), CTL_DEFAULT_NAMESPACE, 1, tenantAdminDto.getTenantId(), null, null, null);
saved = this.createCTLSchema(this.ctlRandomFieldType(), CTL_DEFAULT_NAMESPACE, 1, tenantAdminDto.getTenantId(), null, null, null);
final CTLSchemaMetaInfoDto metaInfo3 = saved.getMetaInfo();

metaInfo3.setVersions(Arrays.asList(1));
Assert.assertNull(metaInfo3.getApplicationId());
metaInfo3.setApplicationId(application.getId());

this.checkForbidden(new TestRestCall() {
@Override
public void executeRestCall() throws Exception {
client.promoteScopeToTenant(metaInfo3);
client.promoteScopeToTenant(metaInfo3.getApplicationId(), metaInfo3.getFqn());
}
});
}

}

0 comments on commit c4989b1

Please sign in to comment.