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.junit.jupiter.api.Assertions.assertTrue;
23  import static org.mockito.Mockito.when;
24  
25  import com.fasterxml.jackson.core.JsonParser;
26  import com.fasterxml.jackson.databind.DeserializationContext;
27  import com.fasterxml.jackson.databind.JsonNode;
28  import com.fasterxml.jackson.databind.node.ObjectNode;
29  import java.io.IOException;
30  import java.nio.charset.StandardCharsets;
31  import java.util.Base64;
32  import org.apache.commons.lang3.builder.EqualsBuilder;
33  import org.apache.syncope.core.provisioning.api.AbstractTest;
34  import org.junit.jupiter.api.BeforeEach;
35  import org.junit.jupiter.api.Test;
36  import org.mockito.Mock;
37  
38  public class SyncTokenDeserializerTest 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 ObjectNode tree;
51  
52      private final SyncTokenDeserializer deserializer = new SyncTokenDeserializer();
53  
54      @BeforeEach
55      public void initTest() throws IOException {
56          when(jp.readValueAsTree()).thenReturn(tree);
57          when(tree.has("value")).thenReturn(Boolean.TRUE);
58          when(tree.get("value")).thenReturn(node);
59      }
60  
61      @Test
62      public void deserializeIsBoolean() throws IOException {
63          Boolean value = Boolean.TRUE;
64          when(node.isBoolean()).thenReturn(value);
65          when(node.asBoolean()).thenReturn(value);
66          assertEquals(value, deserializer.deserialize(jp, ct).getValue());
67      }
68  
69      @Test
70      public void deserializeIsDouble() throws IOException {
71          Double value = 9000.1;
72          when(node.isDouble()).thenReturn(Boolean.TRUE);
73          when(node.asDouble()).thenReturn(value);
74          assertEquals(value, deserializer.deserialize(jp, ct).getValue());
75      }
76  
77      @Test
78      public void deserializeIsLong() throws IOException {
79          Long value = 9000L;
80          when(node.isLong()).thenReturn(Boolean.TRUE);
81          when(node.asLong()).thenReturn(value);
82          assertEquals(value, deserializer.deserialize(jp, ct).getValue());
83      }
84  
85      @Test
86      public void deserializeIsInt() throws IOException {
87          Integer value = 9000;
88          when(node.isInt()).thenReturn(Boolean.TRUE);
89          when(node.asInt()).thenReturn(value);
90          assertEquals(value, deserializer.deserialize(jp, ct).getValue());
91      }
92  
93      @Test
94      public void deserializeIsString() throws IOException {
95          String value = "testValue";
96          when(node.asText()).thenReturn(value);
97          assertEquals(value, deserializer.deserialize(jp, ct).getValue());
98  
99          value = Base64.getEncoder().encodeToString(value.getBytes(StandardCharsets.ISO_8859_1));
100         when(node.asText()).thenReturn(value);
101         assertTrue(EqualsBuilder.reflectionEquals(Base64.getDecoder().decode(value),
102                 deserializer.deserialize(jp, ct).getValue()));
103     }
104 }