5bb7a67d8e6c61d53e4b953d1b374e10abca4710
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2021,2023-2024 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.models.acm.messages.kafka.participant;
22
23 import static org.junit.jupiter.api.Assertions.assertEquals;
24
25 import org.onap.policy.common.utils.coder.CoderException;
26 import org.onap.policy.common.utils.coder.StandardCoder;
27
28 /**
29  * Utility class for tests of ParticipantMessage subclasses.
30  */
31 public class ParticipantMessageUtils {
32
33     private ParticipantMessageUtils() {
34
35     }
36
37     public static String removeVariableFields(String text) {
38         return text.replaceAll("messageId=[^,]*", "messageId=xxx").replaceAll("timestamp=[^,]*", "timestamp=nnn");
39     }
40
41     /**
42      * Check if object is Serializable.
43      *
44      * @param object the Object
45      * @param clazz the class of the Object
46      * @throws CoderException if object is not Serializable
47      */
48     public static <T> void assertSerializable(Object object, Class<T> clazz) throws CoderException {
49         var standardCoder = new StandardCoder();
50         var json = standardCoder.encode(object);
51         var other = standardCoder.decode(json, clazz);
52
53         assertEquals(removeVariableFields(object.toString()), removeVariableFields(other.toString()));
54     }
55 }