From 375e225ed6f681e356f7a3eaa9213e70b4115ff6 Mon Sep 17 00:00:00 2001 From: pyshankov Date: Tue, 19 Jul 2016 14:21:43 +0300 Subject: [PATCH] KAA-1256, added user email validation to GWT module,user management tests were fixed --- .../kaaproject/kaa/common/dto/TenantDto.java | 4 + .../resources/common-dao-mongodb.properties | 24 +++ .../client/mvp/view/user/UserViewImpl.java | 10 +- .../admin/services/KaaAdminServiceImpl.java | 17 -- .../control/AbstractTestControlServer.java | 39 ++-- .../ControlServerApplicationEventMapIT.java | 157 ++++++++++++++++ .../ControlServerEventClassFamilyIT.java | 6 +- .../control/ControlServerProfileSchemaIT.java | 28 +-- .../server/control/ControlServerTenantIT.java | 173 +++++++++--------- .../server/control/ControlServerUserIT.java | 5 +- 10 files changed, 326 insertions(+), 137 deletions(-) create mode 100644 server/common/nosql/mongo-dao/src/main/resources/common-dao-mongodb.properties create mode 100644 server/node/src/test/java/org/kaaproject/kaa/server/control/ControlServerApplicationEventMapIT.java diff --git a/server/common/dto/src/main/java/org/kaaproject/kaa/common/dto/TenantDto.java b/server/common/dto/src/main/java/org/kaaproject/kaa/common/dto/TenantDto.java index ebf9c621a9..abb34e0c3b 100644 --- a/server/common/dto/src/main/java/org/kaaproject/kaa/common/dto/TenantDto.java +++ b/server/common/dto/src/main/java/org/kaaproject/kaa/common/dto/TenantDto.java @@ -16,6 +16,8 @@ package org.kaaproject.kaa.common.dto; +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Size; import java.io.Serializable; public class TenantDto implements HasId, Serializable { @@ -23,6 +25,8 @@ public class TenantDto implements HasId, Serializable { private static final long serialVersionUID = 330532156388681820L; private String id; + @Size(min = 2,max = 255) + @NotNull(message="tenant name can't be null") private String name; public String getId() { diff --git a/server/common/nosql/mongo-dao/src/main/resources/common-dao-mongodb.properties b/server/common/nosql/mongo-dao/src/main/resources/common-dao-mongodb.properties new file mode 100644 index 0000000000..23f6948096 --- /dev/null +++ b/server/common/nosql/mongo-dao/src/main/resources/common-dao-mongodb.properties @@ -0,0 +1,24 @@ +# Mongodb configurations +# list of mongodb nodes, possible to use multiply servers +servers=localhost:27017 + +# mongodb database name +db_name=kaa + +# write concern for mongodb write operations +write_concern=acknowledged + +#The number of connections allowed per host (the pool size, per host) +connections_per_host=100 + +# The max wait time for a blocking thread for a connection from the pool in ms_ +max_wait_time=120000 + +# The connection timeout in milliseconds +connection_timeout=5000 + +# The socket timeout_ A timeout of zero is interpreted as an infinite timeout. +socket_timeout=0 + +# This controls whether or not to have socket keep alive turned on +socket_keepalive=false \ No newline at end of file diff --git a/server/node/src/main/java/org/kaaproject/kaa/server/admin/client/mvp/view/user/UserViewImpl.java b/server/node/src/main/java/org/kaaproject/kaa/server/admin/client/mvp/view/user/UserViewImpl.java index 833591c836..b0736ebe91 100644 --- a/server/node/src/main/java/org/kaaproject/kaa/server/admin/client/mvp/view/user/UserViewImpl.java +++ b/server/node/src/main/java/org/kaaproject/kaa/server/admin/client/mvp/view/user/UserViewImpl.java @@ -20,6 +20,7 @@ import java.util.ArrayList; import java.util.List; +import com.google.gwt.regexp.shared.RegExp; import org.kaaproject.avro.ui.gwt.client.widget.SizedTextBox; import org.kaaproject.kaa.common.dto.KaaAuthorityDto; import org.kaaproject.kaa.server.admin.client.mvp.view.UserView; @@ -133,8 +134,15 @@ protected void resetImpl() { @Override protected boolean validate() { + String pattern = "^(?:[a-zA-Z0-9_'^&/+-])+(?:\\.(?:[a-zA-Z0-9_'^&/+-])+)\" +\n" + + " \"*@(?:(?:\\\\[?(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))\\\\.)\" +\n" + + " \"{3}(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\\\]?)|(?:[a-zA-Z0-9-]+\\\\.)\" +\n" + + " \"+(?:[a-zA-Z]){2,}\\.?)$"; + + RegExp regExp=RegExp.compile(pattern); + boolean result = userName.getValue().length()>0; - result &= email.getValue().length()>0; + result &= regExp.test(email.getValue()); result &= authority.getValue() != null; return result; } diff --git a/server/node/src/main/java/org/kaaproject/kaa/server/admin/services/KaaAdminServiceImpl.java b/server/node/src/main/java/org/kaaproject/kaa/server/admin/services/KaaAdminServiceImpl.java index 8b136e0e88..a090835a34 100644 --- a/server/node/src/main/java/org/kaaproject/kaa/server/admin/services/KaaAdminServiceImpl.java +++ b/server/node/src/main/java/org/kaaproject/kaa/server/admin/services/KaaAdminServiceImpl.java @@ -633,7 +633,6 @@ public org.kaaproject.kaa.common.dto.admin.UserDto editUser(org.kaaproject.kaa.c checkTenantId(storedUser.getTenantId()); } Long userId = saveUser(user); - user.setId(userId.toString()); UserDto userDto = new UserDto(); userDto.setId(user.getId()); userDto.setUsername(user.getUsername()); @@ -2854,22 +2853,6 @@ public String getRecordDataByApplicationIdAndSchemaVersion(String applicationId, } } -// private TenantUserDto toTenantUser(TenantAdminDto tenantAdmin) { -// User user = userFacade.findById(Long.valueOf(tenantAdmin.getExternalUid())); -// LOG.debug("Convert tenant admin to tenant user {}.", user); -// TenantUserDto tenantUser = null; -// if (user != null) { -// tenantUser = new TenantUserDto(user.getId().toString(), user.getUsername(), user.getFirstName(), user.getLastName(), -// user.getMail(), KaaAuthorityDto.valueOf(user.getAuthorities().iterator().next().getAuthority())); -// tenantUser.setId(tenantAdmin.getUserId()); -// tenantUser.setTenantId(tenantAdmin.getTenant().getId()); -// tenantUser.setTenantName(tenantAdmin.getTenant().getName()); -// } else { -// LOG.debug("Can't find tenant user by external id {}.", tenantAdmin.getExternalUid()); -// } -// return tenantUser; -// } - private org.kaaproject.kaa.common.dto.admin.UserDto toUser(UserDto tenantUser) { User user = userFacade.findById(Long.valueOf(tenantUser.getExternalUid())); org.kaaproject.kaa.common.dto.admin.UserDto result = new org.kaaproject.kaa.common.dto.admin.UserDto(user.getId().toString(), diff --git a/server/node/src/test/java/org/kaaproject/kaa/server/control/AbstractTestControlServer.java b/server/node/src/test/java/org/kaaproject/kaa/server/control/AbstractTestControlServer.java index 465d16324d..59b7e9eaf8 100644 --- a/server/node/src/test/java/org/kaaproject/kaa/server/control/AbstractTestControlServer.java +++ b/server/node/src/test/java/org/kaaproject/kaa/server/control/AbstractTestControlServer.java @@ -53,6 +53,7 @@ import org.kaaproject.kaa.server.common.dao.AbstractTest; import org.kaaproject.kaa.server.common.dao.impl.sql.H2DBTestRunner; import org.kaaproject.kaa.server.common.dao.impl.sql.PostgreDBTestRunner; +import org.kaaproject.kaa.server.common.dao.model.sql.Tenant; import org.kaaproject.kaa.server.common.nosql.mongo.dao.MongoDBTestRunner; import org.kaaproject.kaa.server.node.service.initialization.KaaNodeInitializationService; import org.slf4j.Logger; @@ -358,10 +359,8 @@ protected boolean createTenantDeveloperNeeded() { private void createUsers() throws Exception { LOG.info("Creating users..."); client.createKaaAdmin(kaaAdminUser, kaaAdminPassword); - loginTenantAdmin(tenantAdminUser); if (createTenantAdminNeeded()) { - tenantAdminDto = createTenant(tenantAdminUser); - + tenantAdminDto = createTenantAdmin(tenantAdminUser); loginTenantAdmin(tenantAdminUser); if (createTenantDeveloperNeeded()) { tenantDeveloperDto = createTenantDeveloper(tenantDeveloperUser); @@ -487,14 +486,24 @@ public int compare(HasId o1, HasId o2) { } } + protected TenantDto createTenant() throws Exception{ + loginKaaAdmin(); + TenantDto tenantDto = new TenantDto(); + tenantDto.setName(generateString(TENANT_NAME)); + tenantDto= client.editTenant(tenantDto); + return tenantDto; + } + + + /** * Creates the tenant. * * @return the tenant user dto * @throws Exception the exception */ - protected TenantAdminDto createTenant() throws Exception { - return createTenant(null); + protected TenantAdminDto createTenantAdmin() throws Exception { + return createTenantAdmin(null); } /** @@ -504,17 +513,21 @@ protected TenantAdminDto createTenant() throws Exception { * @return the tenant user dto * @throws Exception the exception */ - protected TenantAdminDto createTenant(String username) throws Exception { + protected TenantAdminDto createTenantAdmin(String username) throws Exception { loginKaaAdmin(); if (username == null) { username = generateString(username); } - TenantDto tenantDto=null; + TenantDto tenantDto = new TenantDto(); if(client.getTenants().isEmpty()) { - tenantDto = new TenantDto(); tenantDto.setName(TENANT_NAME); tenantDto = client.editTenant(tenantDto); - }else tenantDto=client.getTenant("1"); + }else { + for(TenantDto t :client.getTenants()){ + if(t.getName().equals(TENANT_NAME)) + tenantDto=t; + } + } org.kaaproject.kaa.common.dto.admin.UserDto tenAdmin = new org.kaaproject.kaa.common.dto.admin.UserDto(); tenAdmin.setAuthority(KaaAuthorityDto.TENANT_ADMIN); @@ -554,6 +567,7 @@ private org.kaaproject.kaa.common.dto.admin.UserDto createTenantDeveloper(String tenantDeveloper.setMail(username + "@demoproject.org"); tenantDeveloper.setFirstName("Tenant"); tenantDeveloper.setLastName("Developer"); + tenantDeveloper.setTenantId(tenantAdminDto.getTenant().getId()); tenantDeveloper = client.editUser(tenantDeveloper); if (StringUtils.isNotBlank(tenantDeveloper.getTempPassword())) { @@ -591,8 +605,9 @@ protected UserDto createUser(TenantAdminDto tenant, KaaAuthorityDto authority) t user.setLastName(generateString("User")); user.setAuthority(authority); if (tenant == null) { - tenant = createTenant(); + tenant = createTenantAdmin(); } + user.setTenantId(tenantAdminDto.getTenant().getId()); loginTenantAdmin(tenant.getUsername()); UserDto savedUser = client.editUser(user); return savedUser; @@ -619,7 +634,7 @@ protected ApplicationDto createApplication(TenantAdminDto tenant) throws Excepti ApplicationDto application = new ApplicationDto(); application.setName(generateString(APPLICATION)); if (tenant == null) { - tenant = createTenant(); + tenant = createTenantAdmin(); } loginTenantAdmin(tenant.getUsername()); ApplicationDto savedApplication = client @@ -1159,7 +1174,7 @@ protected EventClassFamilyDto createEventClassFamily(String tenantId, String cla } eventClassFamily.setClassName(className); if (strIsEmpty(tenantId)) { - TenantAdminDto tenant = createTenant(tenantAdminUser); + TenantAdminDto tenant = createTenantAdmin(tenantAdminUser); eventClassFamily.setTenantId(tenant.getId()); } else { diff --git a/server/node/src/test/java/org/kaaproject/kaa/server/control/ControlServerApplicationEventMapIT.java b/server/node/src/test/java/org/kaaproject/kaa/server/control/ControlServerApplicationEventMapIT.java new file mode 100644 index 0000000000..5027ca6e56 --- /dev/null +++ b/server/node/src/test/java/org/kaaproject/kaa/server/control/ControlServerApplicationEventMapIT.java @@ -0,0 +1,157 @@ +/* + * Copyright 2014-2016 CyberVision, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.kaaproject.kaa.server.control; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import org.junit.Assert; +import org.junit.Test; +import org.kaaproject.kaa.common.dto.ApplicationDto; +import org.kaaproject.kaa.common.dto.event.AefMapInfoDto; +import org.kaaproject.kaa.common.dto.event.ApplicationEventFamilyMapDto; +import org.kaaproject.kaa.common.dto.event.EcfInfoDto; +import org.kaaproject.kaa.common.dto.event.EventClassFamilyDto; +import org.springframework.web.client.ResourceAccessException; + +/** + * The Class ControlServerApplicationEventMapIT. + */ +public class ControlServerApplicationEventMapIT extends AbstractTestControlServer { + + /** + * Test create application event family map. + * + * @throws Exception the exception + */ + @Test + public void testCreateApplicationEventFamilyMap() throws Exception { + ApplicationEventFamilyMapDto applicationEventFamilyMap = createApplicationEventFamilyMap(); + Assert.assertFalse(strIsEmpty(applicationEventFamilyMap.getId())); + } + + /** + * Test get application event family map. + * + * @throws Exception the exception + */ + @Test + public void testGetApplicationEventFamilyMap() throws Exception { + ApplicationEventFamilyMapDto applicationEventFamilyMap = createApplicationEventFamilyMap(); + + ApplicationEventFamilyMapDto storedApplicationEventFamilyMap = client.getApplicationEventFamilyMap(applicationEventFamilyMap.getId()); + + Assert.assertNotNull(storedApplicationEventFamilyMap); + Assert.assertEquals(applicationEventFamilyMap, storedApplicationEventFamilyMap); + } + + /** + * Test get application event family maps by application token. + * + * @throws Exception the exception + */ + @Test + public void testGetApplicationEventFamilyMapsByApplicationToken() throws Exception { + List applicationEventFamilyMaps = new ArrayList<>(10); + ApplicationDto application = createApplication(tenantAdminDto); + EventClassFamilyDto eventClassFamily = createEventClassFamily(application.getTenantId()); + for (int i=0;i<10;i++) { + ApplicationEventFamilyMapDto applicationEventFamilyMap = createApplicationEventFamilyMap( + application.getApplicationToken(), eventClassFamily.getId(), (i+1)); + applicationEventFamilyMaps.add(applicationEventFamilyMap); + } + + Collections.sort(applicationEventFamilyMaps, new IdComparator()); + + loginTenantDeveloper(tenantDeveloperUser); + + List storedApplicationEventFamilyMaps = client.getApplicationEventFamilyMapsByApplicationToken( + application.getApplicationToken()); + + Collections.sort(storedApplicationEventFamilyMaps, new IdComparator()); + + Assert.assertEquals(applicationEventFamilyMaps, storedApplicationEventFamilyMaps); + } + + /** + * Test update application event family map. + * + * @throws Exception the exception + */ + @Test + public void testUpdateApplicationEventFamilyMap() throws Exception { + final ApplicationEventFamilyMapDto applicationEventFamilyMap = createApplicationEventFamilyMap(); + checkBadRequest(new TestRestCall() { + @Override + public void executeRestCall() throws Exception { + client.editApplicationEventFamilyMap(applicationEventFamilyMap); + } + }); + } + + /** + * Test get vacant event class families by application token. + * + * @throws Exception the exception + */ + @Test + public void testGetVacantEventClassFamiliesByApplicationToken() throws Exception { + ApplicationDto application = createApplication(tenantAdminDto); + EventClassFamilyDto eventClassFamily = createEventClassFamily(application.getTenantId()); + createApplicationEventFamilyMap(application.getApplicationToken(), eventClassFamily.getId(), 1); + + loginTenantDeveloper(tenantDeveloperUser); + List vacantEcfs = client.getVacantEventClassFamiliesByApplicationToken(application.getApplicationToken()); + Assert.assertNotNull(vacantEcfs); + Assert.assertEquals(0, vacantEcfs.size()); + + loginTenantAdmin(tenantAdminUser); + client.addEventClassFamilySchema(eventClassFamily.getId(), TEST_EVENT_CLASS_FAMILY_SCHEMA); + + loginTenantDeveloper(tenantDeveloperUser); + vacantEcfs = client.getVacantEventClassFamiliesByApplicationToken(application.getApplicationToken()); + Assert.assertNotNull(vacantEcfs); + Assert.assertEquals(1, vacantEcfs.size()); + Assert.assertNotNull(vacantEcfs.get(0)); + Assert.assertEquals(eventClassFamily.getId(), vacantEcfs.get(0).getEcfId()); + Assert.assertEquals(eventClassFamily.getName(), vacantEcfs.get(0).getEcfName()); + Assert.assertEquals(2, vacantEcfs.get(0).getVersion()); + } + + /** + * Test get event class families by application token. + * + * @throws Exception the exception + */ + @Test + public void testGetEventClassFamiliesByApplicationToken() throws Exception { + ApplicationDto application = createApplication(tenantAdminDto); + EventClassFamilyDto eventClassFamily = createEventClassFamily(application.getTenantId()); + createApplicationEventFamilyMap(application.getApplicationToken(), eventClassFamily.getId(), 1); + + loginTenantDeveloper(tenantDeveloperUser); + + List applicationEcfs = client.getEventClassFamiliesByApplicationToken(application.getApplicationToken()); + Assert.assertNotNull(applicationEcfs); + Assert.assertEquals(1, applicationEcfs.size()); + Assert.assertNotNull(applicationEcfs.get(0)); + Assert.assertEquals(eventClassFamily.getId(), applicationEcfs.get(0).getEcfId()); + Assert.assertEquals(eventClassFamily.getName(), applicationEcfs.get(0).getEcfName()); + Assert.assertEquals(1, applicationEcfs.get(0).getVersion()); + } +} diff --git a/server/node/src/test/java/org/kaaproject/kaa/server/control/ControlServerEventClassFamilyIT.java b/server/node/src/test/java/org/kaaproject/kaa/server/control/ControlServerEventClassFamilyIT.java index 09c9bdd73b..08042ae66d 100644 --- a/server/node/src/test/java/org/kaaproject/kaa/server/control/ControlServerEventClassFamilyIT.java +++ b/server/node/src/test/java/org/kaaproject/kaa/server/control/ControlServerEventClassFamilyIT.java @@ -84,7 +84,7 @@ public void testGetEventClassFamily() throws Exception { @Test public void testGetEventClassFamiliesByTenantId() throws Exception { List eventClassFamilies = new ArrayList<>(10); - TenantAdminDto tenant = createTenant(tenantAdminUser); + TenantAdminDto tenant = createTenantAdmin(tenantAdminUser); loginTenantAdmin(tenantAdminUser); for (int i=0;i<10;i++) { EventClassFamilyDto eventClassFamily = createEventClassFamily(tenant.getId(), ""+i); @@ -147,7 +147,7 @@ public void testGetEventClassesByFamilyIdVersionAndType() throws Exception { */ @Test public void testDuplicateEventClassFamilyName() throws Exception { - TenantAdminDto tenant = createTenant(tenantAdminUser); + TenantAdminDto tenant = createTenantAdmin(tenantAdminUser); loginTenantAdmin(tenantAdminUser); EventClassFamilyDto eventClassFamily = createEventClassFamily(tenant.getId()); final EventClassFamilyDto secondEventClassFamily = createEventClassFamily(tenant.getId(), "test"); @@ -200,7 +200,7 @@ public void testAddEventClassFamilySchema() throws Exception { */ @Test public void testDuplicateEventClassFamilyFqns() throws Exception { - TenantAdminDto tenant = createTenant(tenantAdminUser); + TenantAdminDto tenant = createTenantAdmin(tenantAdminUser); loginTenantAdmin(tenantAdminUser); EventClassFamilyDto eventClassFamily = createEventClassFamily(tenant.getId()); client.addEventClassFamilySchema(eventClassFamily.getId(), TEST_EVENT_CLASS_FAMILY_SCHEMA); diff --git a/server/node/src/test/java/org/kaaproject/kaa/server/control/ControlServerProfileSchemaIT.java b/server/node/src/test/java/org/kaaproject/kaa/server/control/ControlServerProfileSchemaIT.java index 84e43ede38..3f5d931b85 100644 --- a/server/node/src/test/java/org/kaaproject/kaa/server/control/ControlServerProfileSchemaIT.java +++ b/server/node/src/test/java/org/kaaproject/kaa/server/control/ControlServerProfileSchemaIT.java @@ -43,7 +43,7 @@ public void testCreateProfileSchema() throws Exception { EndpointProfileSchemaDto profileSchema = createProfileSchema(); Assert.assertFalse(strIsEmpty(profileSchema.getId())); } - + /** * Test get profile schema. * @@ -52,13 +52,13 @@ public void testCreateProfileSchema() throws Exception { @Test public void testGetProfileSchema() throws Exception { EndpointProfileSchemaDto profileSchema = createProfileSchema(); - + EndpointProfileSchemaDto storedProfileSchema = client.getProfileSchema(profileSchema.getId()); - + Assert.assertNotNull(storedProfileSchema); assertProfileSchemasEquals(profileSchema, storedProfileSchema); } - + /** * Test get profile schemas by application id. * @@ -66,12 +66,12 @@ public void testGetProfileSchema() throws Exception { */ @Test public void testGetProfileSchemasByApplicationToken() throws Exception { - + List profileSchemas = new ArrayList<>(11); ApplicationDto application = createApplication(tenantAdminDto); - + loginTenantDeveloper(tenantDeveloperDto.getUsername()); - + CTLSchemaDto ctlSchema = this.createCTLSchema(this.ctlRandomFieldType(), CTL_DEFAULT_NAMESPACE, 1, tenantDeveloperDto.getTenantId(), null, null, null); List defaultProfileSchemas = client.getProfileSchemas(application.getApplicationToken()); @@ -81,13 +81,13 @@ public void testGetProfileSchemasByApplicationToken() throws Exception { EndpointProfileSchemaDto profileSchema = createEndpointProfileSchema(application.getId(), ctlSchema.getId()); profileSchemas.add(profileSchema); } - + Collections.sort(profileSchemas, new IdComparator()); - + List storedProfileSchemas = client.getProfileSchemas(application.getApplicationToken()); Collections.sort(storedProfileSchemas, new IdComparator()); - + Assert.assertEquals(profileSchemas.size(), storedProfileSchemas.size()); for (int i=0;i tenants = new ArrayList(10); -// for (int i=0;i<10;i++) { -// TenantUserDto tenant = createTenant(); -// tenants.add(tenant); -// } -// -// Collections.sort(tenants, new IdComparator()); -// -// List storedTenants = client.getTenants(); -// Collections.sort(storedTenants, new IdComparator()); -// -// Assert.assertEquals(tenants.size(), storedTenants.size()); -// for (int i=0;i tenants = new ArrayList(10); + for (int i=0;i<10;i++) { + TenantDto tenant = createTenant(); + tenants.add(tenant); + } + + Collections.sort(tenants, new IdComparator()); + + List storedTenants = client.getTenants(); + Collections.sort(storedTenants, new IdComparator()); + + Assert.assertEquals(tenants.size(), storedTenants.size()); + for (int i=0;i