Java tutorial
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF 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.apache.syncope.fit.core.reference; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.util.List; import javax.ws.rs.core.Response; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.Predicate; import org.apache.syncope.common.lib.SyncopeClientException; import org.apache.syncope.common.lib.SyncopeConstants; import org.apache.syncope.common.lib.to.AccountPolicyTO; import org.apache.syncope.common.lib.to.RealmTO; import org.apache.syncope.common.lib.types.AccountPolicySpec; import org.apache.syncope.common.lib.types.ClientExceptionType; import org.apache.syncope.common.rest.api.service.RealmService; import org.junit.FixMethodOrder; import org.junit.Test; import org.junit.runners.MethodSorters; @FixMethodOrder(MethodSorters.JVM) public class RealmITCase extends AbstractITCase { private RealmTO getRealm(final String fullPath) { return CollectionUtils.find(realmService.list(fullPath), new Predicate<RealmTO>() { @Override public boolean evaluate(final RealmTO object) { return fullPath.equals(object.getFullPath()); } }); } @Test public void list() { List<RealmTO> realms = realmService.list(); assertNotNull(realms); assertFalse(realms.isEmpty()); for (RealmTO realm : realms) { assertNotNull(realm); } try { realmService.list("a name"); fail(); } catch (SyncopeClientException e) { assertEquals(ClientExceptionType.InvalidPath, e.getType()); } } @Test public void createUpdate() { final RealmTO realm = new RealmTO(); realm.setName("last"); // 1. create Response response = realmService.create("/even/two", realm); RealmTO[] actuals = getObject(response.getLocation(), RealmService.class, RealmTO[].class); assertNotNull(actuals); assertTrue(actuals.length > 0); RealmTO actual = actuals[0]; assertNotNull(actual.getKey()); assertEquals("last", actual.getName()); assertEquals("/even/two/last", actual.getFullPath()); assertEquals(actual.getParent(), getRealm("/even/two").getKey()); assertNull(realm.getAccountPolicy()); assertNull(realm.getPasswordPolicy()); // 2. update setting policies actual.setAccountPolicy(6L); actual.setPasswordPolicy(4L); realmService.update(actual); actual = getRealm(actual.getFullPath()); assertNotNull(actual.getAccountPolicy()); assertNotNull(actual.getPasswordPolicy()); // 3. update changing parent actual.setParent(getRealm("/odd").getKey()); realmService.update(actual); actual = getRealm("/odd/last"); assertNotNull(actual); assertEquals("/odd/last", actual.getFullPath()); assertEquals(1, CollectionUtils.countMatches(realmService.list(), new Predicate<RealmTO>() { @Override public boolean evaluate(final RealmTO object) { return realm.getName().equals(object.getName()); } })); // 4. create under invalid path try { realmService.create("a name", realm); fail(); } catch (SyncopeClientException e) { assertEquals(ClientExceptionType.InvalidPath, e.getType()); } // 5. attempt to create duplicate try { realmService.create("/odd", realm); fail(); } catch (SyncopeClientException e) { assertEquals(ClientExceptionType.DataIntegrityViolation, e.getType()); } } @Test public void deletingAccountPolicy() { // 1. create account policy AccountPolicyTO policy = new AccountPolicyTO(); policy.setDescription("deletingAccountPolicy"); final AccountPolicySpec accountPolicySpec = new AccountPolicySpec(); accountPolicySpec.setMinLength(3); accountPolicySpec.setMaxLength(8); policy.setSpecification(accountPolicySpec); policy = createPolicy(policy); assertNotNull(policy); // 2. create realm with policy assigned RealmTO realm = new RealmTO(); realm.setName("withppolicy"); realm.setAccountPolicy(policy.getKey()); Response response = realmService.create(SyncopeConstants.ROOT_REALM, realm); RealmTO[] actuals = getObject(response.getLocation(), RealmService.class, RealmTO[].class); assertNotNull(actuals); assertTrue(actuals.length > 0); RealmTO actual = actuals[0]; assertEquals(policy.getKey(), actual.getAccountPolicy(), 0); // 3. remove policy policyService.delete(policy.getKey()); // 4. verify actual = getRealm(actual.getFullPath()); assertNull(actual.getAccountPolicy()); } @Test public void delete() { RealmTO realm = new RealmTO(); realm.setName("deletable"); Response response = realmService.create("/even/two", realm); RealmTO[] actuals = getObject(response.getLocation(), RealmService.class, RealmTO[].class); assertNotNull(actuals); assertTrue(actuals.length > 0); RealmTO actual = actuals[0]; realmService.delete(actual.getFullPath()); try { realmService.list(actual.getFullPath()); fail(); } catch (SyncopeClientException e) { assertEquals(ClientExceptionType.NotFound, e.getType()); } } }