diff --git a/itests/src/test/java/org/openengsb/itests/exam/UserProjectsFileConnectorIT.java b/itests/src/test/java/org/openengsb/itests/exam/UserProjectsFileConnectorIT.java new file mode 100644 index 000000000..f5638c3ae --- /dev/null +++ b/itests/src/test/java/org/openengsb/itests/exam/UserProjectsFileConnectorIT.java @@ -0,0 +1,158 @@ +/** + * Licensed to the Austrian Association for Software Tool Integration (AASTI) + * under one or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information regarding copyright + * ownership. The AASTI licenses this file to you 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.openengsb.itests.exam; + +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import javax.inject.Inject; + +import org.hamcrest.CoreMatchers; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.openengsb.core.ekb.api.QueryInterface; +import org.openengsb.domain.userprojects.UserProjectsDomain; +import org.openengsb.domain.userprojects.model.Assignment; +import org.openengsb.domain.userprojects.model.Project; +import org.openengsb.domain.userprojects.model.Role; +import org.openengsb.domain.userprojects.model.User; +import org.openengsb.itests.util.AbstractPreConfiguredExamTestHelper; +import org.ops4j.pax.exam.junit.PaxExam; + +import com.google.common.collect.Lists; + +@RunWith(PaxExam.class) +public class UserProjectsFileConnectorIT extends AbstractPreConfiguredExamTestHelper { + + private static final String BASE_DIR = "data/userprojectsfile"; + private static final File ASSIGNMENTS_FILE = new File(BASE_DIR, "assignments"); + private static final File PROJECTS_FILE = new File(BASE_DIR, "projects"); + private static final File ROLES_FILE = new File(BASE_DIR, "roles"); + private static final File USERS_FILE = new File(BASE_DIR, "users"); + + private List assignments = new ArrayList<>(); + private List projects = new ArrayList<>(); + private List roles = new ArrayList<>(); + private List users = new ArrayList<>(); + + @Inject + private QueryInterface queryInterface; + + @Before + public void setup() throws Exception { + authenticateAsAdmin(); + setupAssignments(); + setupProjects(); + setupRoles(); + setupUsers(); + } + + private void setupAssignments() { + Assignment assignment = new Assignment("user1", "project1"); + assignment.setRoles(Lists.newArrayList("role1", "role2")); + assignments.add(assignment); + + assignments.add(new Assignment("user2", "project2")); + assignments.add(new Assignment("user1", "project2")); + } + + private void setupProjects() { + projects.add(new Project("project1")); + projects.add(new Project("project2")); + } + + private void setupRoles() { + roles.add(new Role("role1")); + roles.add(new Role("role2")); + Role role = new Role("role3"); + role.setRoles(Arrays.asList("role1", "role2")); + roles.add(role); + roles.add(new Role("role4")); + } + + private void setupUsers() { + users.add(new User("user1")); + users.add(new User("user2")); + } + + @Test + public void testSyncFromFilesToOpenEngSB_shouldThrowTheRespectiveEvents() throws InterruptedException, + IOException { + assertTrue(queryInterface.queryForActiveModels(Assignment.class).isEmpty()); + assertTrue(queryInterface.queryForActiveModels(Project.class).isEmpty()); + assertTrue(queryInterface.queryForActiveModels(Role.class).isEmpty()); + assertTrue(queryInterface.queryForActiveModels(User.class).isEmpty()); + + getOsgiService(UserProjectsDomain.class, 5000); + copyFiles(); + Thread.sleep(10000); + + assertAssignmentsExist(queryInterface.queryForActiveModels(Assignment.class)); + assertProjectsExist(queryInterface.queryForActiveModels(Project.class)); + assertRolesExist(queryInterface.queryForActiveModels(Role.class)); + assertUsersExist(queryInterface.queryForActiveModels(User.class)); + + for (User user : users) { + System.out.println(user.getUsername()); + } + + } + + private void assertAssignmentsExist(List actualAssignments) { + Assert.assertThat(actualAssignments, + CoreMatchers.hasItems(assignments.toArray(new Assignment[assignments.size()]))); + } + + private void assertProjectsExist(List actualProjects) { + Assert.assertThat(actualProjects, CoreMatchers.hasItems(projects.toArray(new Project[projects.size()]))); + } + + private void assertRolesExist(List actualRoles) { + Assert.assertThat(actualRoles, CoreMatchers.hasItems(roles.toArray(new Role[roles.size()]))); + } + + private void assertUsersExist(List actualUsers) { + Assert.assertThat(actualUsers, CoreMatchers.hasItems(users.toArray(new User[users.size()]))); + } + + private void copyFiles() throws IOException { + copyFile(ASSIGNMENTS_FILE); + copyFile(PROJECTS_FILE); + copyFile(ROLES_FILE); + copyFile(USERS_FILE); + } + + private void copyFile(File file) throws IOException { + Files.createDirectories(file.getParentFile().toPath()); + Files.copy(getFileAsStream(file), Paths.get(file.getPath())); + } + + private InputStream getFileAsStream(File file) { + return getClass().getClassLoader().getResourceAsStream(file.getPath().replaceAll(File.separator, "/")); + } + +} diff --git a/itests/src/test/resources/data/userprojectsfile/assignments b/itests/src/test/resources/data/userprojectsfile/assignments new file mode 100644 index 000000000..587cb2940 --- /dev/null +++ b/itests/src/test/resources/data/userprojectsfile/assignments @@ -0,0 +1,7 @@ +user1:-:project1:-:role1,-,role2 +user2:-:project2 + +user1 +user2:-: + +user1:-:project2:-: \ No newline at end of file diff --git a/itests/src/test/resources/data/userprojectsfile/projects b/itests/src/test/resources/data/userprojectsfile/projects new file mode 100644 index 000000000..b085603bc --- /dev/null +++ b/itests/src/test/resources/data/userprojectsfile/projects @@ -0,0 +1,3 @@ +project1 + +project2 diff --git a/itests/src/test/resources/data/userprojectsfile/roles b/itests/src/test/resources/data/userprojectsfile/roles new file mode 100644 index 000000000..6d444fbe3 --- /dev/null +++ b/itests/src/test/resources/data/userprojectsfile/roles @@ -0,0 +1,6 @@ +role1 +role2 + +role3:-:role1,-,role2 + +role4:-: diff --git a/itests/src/test/resources/data/userprojectsfile/users b/itests/src/test/resources/data/userprojectsfile/users new file mode 100644 index 000000000..d56d2770c --- /dev/null +++ b/itests/src/test/resources/data/userprojectsfile/users @@ -0,0 +1,3 @@ +user1 + +user2