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.core.provisioning.api.serialization;
20  
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  import static org.mockito.Mockito.when;
23  
24  import com.fasterxml.jackson.core.JsonParser;
25  import com.fasterxml.jackson.databind.DeserializationContext;
26  import com.fasterxml.jackson.databind.JsonNode;
27  import com.fasterxml.jackson.databind.node.ObjectNode;
28  import java.io.IOException;
29  import java.util.Collections;
30  import java.util.List;
31  import org.apache.syncope.core.provisioning.api.AbstractTest;
32  import org.identityconnectors.framework.common.objects.Attribute;
33  import org.identityconnectors.framework.common.objects.Name;
34  import org.junit.jupiter.api.BeforeEach;
35  import org.junit.jupiter.api.Test;
36  import org.mockito.Mock;
37  
38  public class AttributeDeserializerTest extends AbstractTest {
39  
40      @Mock
41      private JsonParser jp;
42  
43      @Mock
44      private DeserializationContext ct;
45  
46      @Mock
47      private JsonNode node;
48  
49      @Mock
50      private JsonNode node2;
51  
52      @Mock
53      private ObjectNode tree;
54  
55      private final AttributeDeserializer deserializer = new AttributeDeserializer();
56  
57      private String name;
58  
59      @BeforeEach
60      public void initTest() throws IOException {
61          name = Name.NAME;
62          when(jp.readValueAsTree()).thenReturn(tree);
63          when(tree.get("name")).thenReturn(node2);
64          when(tree.get("value")).thenReturn(node);
65          when(node.iterator()).thenReturn(List.of(node).iterator());
66      }
67  
68      @Test
69      public void deserializeIsNull() throws IOException {
70          when(node2.asText()).thenReturn(name);
71          when(node.isNull()).thenReturn(Boolean.TRUE);
72          Attribute attr = deserializer.deserialize(jp, ct);
73          assertEquals(name, attr.getName());
74          assertEquals(Collections.singletonList(null), attr.getValue());
75      }
76  
77      @Test
78      public void deserializeIsBoolean() throws IOException {
79          when(node2.asText()).thenReturn(name);
80          when(node.isBoolean()).thenReturn(Boolean.TRUE);
81          when(node.asBoolean()).thenReturn(Boolean.TRUE);
82          Attribute attr = deserializer.deserialize(jp, ct);
83          assertEquals(name, attr.getName());
84          assertEquals(List.of(Boolean.TRUE.toString()).get(0), attr.getValue().get(0));
85      }
86  
87      @Test
88      public void deserializeIsDouble() throws IOException {
89          Double number = 9000.1;
90          name = "__TEST__";
91          when(node2.asText()).thenReturn(name);
92          when(node.isDouble()).thenReturn(Boolean.TRUE);
93          when(node.asDouble()).thenReturn(number);
94          Attribute attr = deserializer.deserialize(jp, ct);
95          assertEquals(name, attr.getName());
96          assertEquals(List.of(number).get(0), attr.getValue().get(0));
97      }
98  
99      @Test
100     public void deserializeIsLong() throws IOException {
101         Long number = 9000L;
102         name = "__UID__";
103         when(node2.asText()).thenReturn(name);
104         when(node.isLong()).thenReturn(Boolean.TRUE);
105         when(node.asLong()).thenReturn(number);
106         Attribute attr = deserializer.deserialize(jp, ct);
107         assertEquals(name, attr.getName());
108         assertEquals(List.of(number.toString()).get(0), attr.getValue().get(0));
109     }
110 
111     @Test
112     public void deserializeIsInt() throws IOException {
113         Integer number = 9000;
114         when(node2.asText()).thenReturn(name);
115         when(node.isInt()).thenReturn(Boolean.TRUE);
116         when(node.asInt()).thenReturn(number);
117         Attribute attr = deserializer.deserialize(jp, ct);
118         assertEquals(attr.getName(), name);
119         assertEquals(List.of(number.toString()).get(0), attr.getValue().get(0));
120     }
121 
122     @Test
123     public void deserializeIsText() throws IOException {
124         String text = "<binary>test";
125         when(node2.asText()).thenReturn(name);
126         when(node.asText()).thenReturn(text);
127         Attribute attr = deserializer.deserialize(jp, ct);
128         assertEquals(attr.getName(), name);
129         assertEquals(List.of(text).get(0), attr.getValue().get(0));
130     }
131 }