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.mockito.ArgumentMatchers.any;
22  import static org.mockito.ArgumentMatchers.anyBoolean;
23  import static org.mockito.ArgumentMatchers.anyDouble;
24  import static org.mockito.ArgumentMatchers.anyInt;
25  import static org.mockito.ArgumentMatchers.anyLong;
26  import static org.mockito.ArgumentMatchers.anyString;
27  import static org.mockito.ArgumentMatchers.eq;
28  import static org.mockito.Mockito.verify;
29  import static org.mockito.Mockito.when;
30  
31  import com.fasterxml.jackson.core.JsonGenerator;
32  import com.fasterxml.jackson.databind.SerializerProvider;
33  import java.io.IOException;
34  import java.util.List;
35  import org.apache.syncope.core.provisioning.api.AbstractTest;
36  import org.identityconnectors.common.security.GuardedString;
37  import org.identityconnectors.framework.common.objects.Attribute;
38  import org.junit.jupiter.api.Test;
39  import org.mockito.Mock;
40  
41  public class AttributeSerializerTest extends AbstractTest {
42  
43      @Test
44      public void serialize(
45              final @Mock Attribute source,
46              final @Mock JsonGenerator jgen,
47              final @Mock SerializerProvider sp)
48              throws IOException {
49  
50          AttributeSerializer serializer = new AttributeSerializer();
51          when(source.getValue()).thenReturn(null);
52          serializer.serialize(source, jgen, sp);
53          verify(jgen).writeStartObject();
54          verify(jgen).writeFieldName("value");
55          verify(jgen).writeNull();
56  
57          when(source.getValue()).thenAnswer(ic -> List.of(new GuardedString()));
58          serializer.serialize(source, jgen, sp);
59          verify(jgen).writeObject(any(GuardedString.class));
60  
61          when(source.getValue()).thenAnswer(ic -> List.of(9000));
62          serializer.serialize(source, jgen, sp);
63          verify(jgen).writeNumber(anyInt());
64  
65          when(source.getValue()).thenAnswer(ic -> List.of(9000L));
66          serializer.serialize(source, jgen, sp);
67          verify(jgen).writeNumber(anyLong());
68  
69          when(source.getValue()).thenAnswer(ic -> List.of(9000.1));
70          serializer.serialize(source, jgen, sp);
71          verify(jgen).writeNumber(anyDouble());
72  
73          when(source.getValue()).thenAnswer(ic -> List.of(Boolean.TRUE));
74          serializer.serialize(source, jgen, sp);
75          verify(jgen).writeBoolean(anyBoolean());
76  
77          when(source.getValue()).thenAnswer(ic -> List.of(new byte[] { 9, 0, 0, 0 }));
78          serializer.serialize(source, jgen, sp);
79          verify(jgen).writeString(anyString());
80  
81          when(source.getValue()).thenAnswer(ic -> List.of("test"));
82          serializer.serialize(source, jgen, sp);
83          verify(jgen).writeString(eq("test"));
84      }
85  }