View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.fit.core;
20  
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertFalse;
23  import static org.junit.jupiter.api.Assertions.assertNotNull;
24  import static org.junit.jupiter.api.Assertions.assertTrue;
25  import static org.junit.jupiter.api.Assertions.fail;
26  
27  import java.util.Set;
28  import java.util.UUID;
29  import javax.ws.rs.core.Response;
30  import org.apache.syncope.client.lib.SyncopeClient;
31  import org.apache.syncope.common.lib.Attr;
32  import org.apache.syncope.common.lib.SyncopeClientException;
33  import org.apache.syncope.common.lib.SyncopeConstants;
34  import org.apache.syncope.common.lib.request.AnyObjectCR;
35  import org.apache.syncope.common.lib.request.AnyObjectUR;
36  import org.apache.syncope.common.lib.request.RelationshipUR;
37  import org.apache.syncope.common.lib.request.StringPatchItem;
38  import org.apache.syncope.common.lib.to.AnyObjectTO;
39  import org.apache.syncope.common.lib.to.ConnObject;
40  import org.apache.syncope.common.lib.to.PagedResult;
41  import org.apache.syncope.common.lib.to.RelationshipTO;
42  import org.apache.syncope.common.lib.types.AnyTypeKind;
43  import org.apache.syncope.common.lib.types.ClientExceptionType;
44  import org.apache.syncope.common.lib.types.PatchOperation;
45  import org.apache.syncope.common.lib.types.SchemaType;
46  import org.apache.syncope.common.rest.api.beans.AnyQuery;
47  import org.apache.syncope.fit.AbstractITCase;
48  import org.junit.jupiter.api.Assertions;
49  import org.junit.jupiter.api.Test;
50  
51  public class AnyObjectITCase extends AbstractITCase {
52  
53      public static AnyObjectCR getSample(final String location) {
54          return new AnyObjectCR.Builder(SyncopeConstants.ROOT_REALM, PRINTER, location + getUUIDString()).
55                  plainAttr(attr("location", location + getUUIDString())).
56                  resource(RESOURCE_NAME_DBSCRIPTED).
57                  build();
58      }
59  
60      @Test
61      public void create() {
62          AnyObjectCR anyObjectCR = getSample("create");
63  
64          AnyObjectTO anyObjectTO = createAnyObject(anyObjectCR).getEntity();
65          assertNotNull(anyObjectTO);
66          assertEquals("REST", anyObjectTO.getCreationContext());
67          assertEquals("REST", anyObjectTO.getLastChangeContext());
68  
69          ConnObject connObjectTO =
70                  RESOURCE_SERVICE.readConnObject(RESOURCE_NAME_DBSCRIPTED, anyObjectTO.getType(), anyObjectTO.getKey());
71          assertNotNull(connObjectTO);
72          assertNotNull(connObjectTO.getAttr("LOCATION"));
73          assertEquals(
74                  anyObjectTO.getPlainAttr("location").get().getValues(),
75                  connObjectTO.getAttr("LOCATION").get().getValues());
76      }
77  
78      @Test
79      public void delete() {
80          try {
81              ANY_OBJECT_SERVICE.delete(UUID.randomUUID().toString());
82          } catch (SyncopeClientException e) {
83              assertEquals(Response.Status.NOT_FOUND, e.getType().getResponseStatus());
84          }
85  
86          AnyObjectCR anyObjectCR = getSample("deletable");
87          anyObjectCR.setRealm("/even");
88  
89          AnyObjectTO anyObjectTO = createAnyObject(anyObjectCR).getEntity();
90          assertNotNull(anyObjectTO);
91  
92          AnyObjectTO deletedAnyObject = deleteAnyObject(anyObjectTO.getKey()).getEntity();
93          assertNotNull(deletedAnyObject);
94  
95          try {
96              ANY_OBJECT_SERVICE.read(deletedAnyObject.getKey());
97          } catch (SyncopeClientException e) {
98              assertEquals(Response.Status.NOT_FOUND, e.getType().getResponseStatus());
99          }
100     }
101 
102     @Test
103     public void list() {
104         PagedResult<AnyObjectTO> anyObjectTOs = ANY_OBJECT_SERVICE.search(
105                 new AnyQuery.Builder().realm(SyncopeConstants.ROOT_REALM).
106                         fiql(SyncopeClient.getAnyObjectSearchConditionBuilder(PRINTER).query()).
107                         build());
108         assertNotNull(anyObjectTOs);
109         assertTrue(anyObjectTOs.getResult().size() >= 2);
110         anyObjectTOs.getResult().forEach(Assertions::assertNotNull);
111     }
112 
113     @Test
114     public void read() {
115         AnyObjectTO anyObjectTO = ANY_OBJECT_SERVICE.read("fc6dbc3a-6c07-4965-8781-921e7401a4a5");
116         assertNotNull(anyObjectTO);
117         assertNotNull(anyObjectTO.getPlainAttrs());
118         assertFalse(anyObjectTO.getPlainAttrs().isEmpty());
119     }
120 
121     @Test
122     public void readByName() {
123         AnyObjectTO anyObjectTO = ANY_OBJECT_SERVICE.read(PRINTER, "HP LJ 1300n");
124         assertEquals("fc6dbc3a-6c07-4965-8781-921e7401a4a5", anyObjectTO.getKey());
125     }
126 
127     @Test
128     public void update() {
129         AnyObjectCR anyObjectCR = getSample("update");
130         AnyObjectTO anyObjectTO = createAnyObject(anyObjectCR).getEntity();
131 
132         assertEquals(1, anyObjectTO.getPlainAttrs().size());
133 
134         AnyObjectUR anyObjectUR = new AnyObjectUR();
135         anyObjectUR.setKey(anyObjectTO.getKey());
136         String newLocation = "new" + getUUIDString();
137         anyObjectUR.getPlainAttrs().add(attrAddReplacePatch("location", newLocation));
138 
139         anyObjectTO = updateAnyObject(anyObjectUR).getEntity();
140 
141         assertEquals(newLocation, anyObjectTO.getPlainAttr("location").get().getValues().get(0));
142     }
143 
144     @Test
145     public void readAttrs() {
146         AnyObjectCR anyObjectCR = getSample("readAttrs");
147         AnyObjectTO anyObjectTO = createAnyObject(anyObjectCR).getEntity();
148         assertNotNull(anyObjectTO);
149 
150         Set<Attr> attrs = ANY_OBJECT_SERVICE.read(anyObjectTO.getKey(), SchemaType.PLAIN);
151         assertEquals(anyObjectTO.getPlainAttrs(), attrs);
152 
153         Attr location = ANY_OBJECT_SERVICE.read(anyObjectTO.getKey(), SchemaType.PLAIN, "location");
154         assertEquals(anyObjectTO.getPlainAttr("location").get(), location);
155     }
156 
157     @Test
158     public void updateAttr() {
159         AnyObjectCR anyObjectCR = getSample("updateAttr");
160         AnyObjectTO anyObjectTO = createAnyObject(anyObjectCR).getEntity();
161         assertNotNull(anyObjectTO);
162 
163         Attr updated = attr("location", "newlocation");
164         ANY_OBJECT_SERVICE.update(anyObjectTO.getKey(), SchemaType.PLAIN, updated);
165 
166         Attr location = ANY_OBJECT_SERVICE.read(anyObjectTO.getKey(), SchemaType.PLAIN, "location");
167         assertEquals(updated, location);
168     }
169 
170     @Test
171     public void deleteAttr() {
172         AnyObjectCR anyObjectCR = getSample("deleteAttr");
173         AnyObjectTO anyObjectTO = createAnyObject(anyObjectCR).getEntity();
174         assertNotNull(anyObjectTO);
175         assertNotNull(anyObjectTO.getPlainAttr("location"));
176 
177         ANY_OBJECT_SERVICE.delete(anyObjectTO.getKey(), SchemaType.PLAIN, "location");
178 
179         try {
180             ANY_OBJECT_SERVICE.read(anyObjectTO.getKey(), SchemaType.PLAIN, "location");
181             fail("This should not happen");
182         } catch (SyncopeClientException e) {
183             assertEquals(ClientExceptionType.NotFound, e.getType());
184         }
185     }
186 
187     @Test
188     public void unlimitedRelationships() {
189         AnyObjectCR anyObjectCR = getSample("unlimited1");
190         anyObjectCR.setRealm("/even/two");
191         anyObjectCR.getResources().clear();
192         AnyObjectTO left = createAnyObject(anyObjectCR).getEntity();
193 
194         anyObjectCR = getSample("unlimited2");
195         anyObjectCR.setRealm(SyncopeConstants.ROOT_REALM);
196         anyObjectCR.getResources().clear();
197         anyObjectCR.getRelationships().add(new RelationshipTO.Builder("neighborhood").
198                 otherEnd(left.getType(), left.getKey()).build());
199         AnyObjectTO right = createAnyObject(anyObjectCR).getEntity();
200 
201         assertEquals(1, right.getRelationships().size());
202         assertEquals(left.getKey(), right.getRelationships().get(0).getOtherEndKey());
203 
204         AnyObjectUR anyObjectUR = new AnyObjectUR.Builder(left.getKey()).
205                 relationship(new RelationshipUR.Builder(new RelationshipTO.Builder("neighborhood").
206                         otherEnd(right.getType(), right.getKey()).build()).build()).build();
207         left = updateAnyObject(anyObjectUR).getEntity();
208         assertEquals(2, left.getRelationships().size());
209         assertTrue(left.getRelationships().stream().anyMatch(r -> right.getKey().equals(r.getOtherEndKey())));
210     }
211 
212     @Test
213     public void issueSYNCOPE756() {
214         AnyObjectCR anyObjectCR = getSample("issueSYNCOPE756");
215         anyObjectCR.getRelationships().add(new RelationshipTO.Builder("neighborhood").otherEnd(
216                 AnyTypeKind.USER.name(), "1417acbe-cbf6-4277-9372-e75e04f97000").build());
217 
218         try {
219             createAnyObject(anyObjectCR).getEntity();
220             fail("This should not happen");
221         } catch (SyncopeClientException e) {
222             assertEquals(ClientExceptionType.InvalidAnyType, e.getType());
223         }
224     }
225 
226     @Test
227     public void issueSYNCOPE1472() {
228         // 1. assign resource-db-scripted again to Canon MF 8030cn and update twice
229         AnyObjectUR updateReq = new AnyObjectUR();
230         updateReq.setKey("8559d14d-58c2-46eb-a2d4-a7d35161e8f8");
231         updateReq.getResources().add(new StringPatchItem.Builder().value(RESOURCE_NAME_DBSCRIPTED).build());
232         updateReq.getAuxClasses().add(new StringPatchItem.Builder().value("csv").build());
233 
234         for (int i = 0; i < 2; i++) {
235             updateAnyObject(updateReq);
236         }
237 
238         // 2. remove resources and auxiliary classes
239         updateReq.getResources().clear();
240         updateReq.getResources().add(new StringPatchItem.Builder()
241                 .value(RESOURCE_NAME_DBSCRIPTED)
242                 .operation(PatchOperation.DELETE)
243                 .build());
244         updateReq.getAuxClasses().clear();
245         updateReq.getAuxClasses().add(new StringPatchItem.Builder()
246                 .value("csv")
247                 .operation(PatchOperation.DELETE)
248                 .build());
249 
250         updateAnyObject(updateReq);
251 
252         AnyObjectTO printer = ANY_OBJECT_SERVICE.read("8559d14d-58c2-46eb-a2d4-a7d35161e8f8");
253         assertFalse(printer.getResources().contains(RESOURCE_NAME_DBSCRIPTED), "Should not contain removed resources");
254         assertFalse(printer.getAuxClasses().contains("csv"), "Should not contain removed auxiliary classes");
255     }
256 }