From 1785dedb0251c6f08bc6d5f97cc6811251a3d4c5 Mon Sep 17 00:00:00 2001 From: "m.kowalski3" Date: Fri, 7 Jun 2019 14:35:26 +0200 Subject: Improve unit tests for ActionDeserializer Issue-ID: SDC-2327 Signed-off-by: Marcin Kowalski Change-Id: I2f5657dce880c5a52c36839e6a53b131b6a33dcb --- .../ruleeditor/ActionDeserializerTest.java | 117 +++++++++++++++++++-- 1 file changed, 106 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/test/java/org/onap/sdc/dcae/composition/restmodels/ruleeditor/ActionDeserializerTest.java b/src/test/java/org/onap/sdc/dcae/composition/restmodels/ruleeditor/ActionDeserializerTest.java index 61c9543..c695d2b 100644 --- a/src/test/java/org/onap/sdc/dcae/composition/restmodels/ruleeditor/ActionDeserializerTest.java +++ b/src/test/java/org/onap/sdc/dcae/composition/restmodels/ruleeditor/ActionDeserializerTest.java @@ -1,26 +1,121 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Modifications Copyright (c) 2019 Samsung + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END============================================ + * =================================================================== + * + */ + package org.onap.sdc.dcae.composition.restmodels.ruleeditor; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import com.google.gson.JsonElement; +import com.google.gson.JsonParseException; import org.junit.Test; - import com.google.gson.Gson; import com.google.gson.GsonBuilder; public class ActionDeserializerTest { - private Gson gson = new GsonBuilder().registerTypeAdapter(BaseAction.class, new ActionDeserializer()).create(); + private Gson gson = new GsonBuilder().registerTypeAdapter(BaseAction.class, new ActionDeserializer()).create(); + + + @Test + public void deserializerTest() { + BaseCopyAction action = new BaseCopyAction(); + action.setActionType("map"); + String input = gson.toJson(action); + BaseAction res = gson.fromJson(input, BaseAction.class); + assertEquals(gson.toJson(res), input); + assertTrue(res instanceof MapAction); + + action.setActionType("copy"); + input = gson.toJson(action); + res = gson.fromJson(input, BaseAction.class); + assertEquals(gson.toJson(res), input); + assertTrue(res instanceof BaseCopyAction); + + action.setActionType("topologySearch"); + input = gson.toJson(action); + res = gson.fromJson(input, BaseAction.class); + assertEquals(gson.toJson(res), input); + assertTrue(res instanceof TopoSearchAction); + + action.setActionType("stringTransform"); + input = gson.toJson(action); + res = gson.fromJson(input, BaseAction.class); + assertEquals(gson.toJson(res), input); + assertTrue(res instanceof StringTransformAction); + + action.setActionType("hpMetric"); + input = gson.toJson(action); + res = gson.fromJson(input, BaseAction.class); + assertEquals(gson.toJson(res), input); + assertTrue(res instanceof HpMetricAction); + + action.setActionType("clearNSF"); + input = gson.toJson(action); + res = gson.fromJson(input, BaseAction.class); + assertEquals(gson.toJson(res), input); + assertTrue(res instanceof UnaryFieldAction); + + action.setActionType("replaceText"); + input = gson.toJson(action); + res = gson.fromJson(input, BaseAction.class); + assertEquals(gson.toJson(res), input); + assertTrue(res instanceof ReplaceTextAction); + + action.setActionType("clear"); + input = gson.toJson(action); + res = gson.fromJson(input, BaseAction.class); + assertEquals(gson.toJson(res), input); + assertTrue(res instanceof UnaryFieldAction); + + action.setActionType("logText"); + input = gson.toJson(action); + res = gson.fromJson(input, BaseAction.class); + assertEquals(gson.toJson(res), input); + assertTrue(res instanceof LogTextAction); + + action.setActionType("logEvent"); + input = gson.toJson(action); + res = gson.fromJson(input, BaseAction.class); + assertEquals(gson.toJson(res), input); + assertTrue(res instanceof LogEventAction); + + action.setActionType("dateFormatter"); + input = gson.toJson(action); + res = gson.fromJson(input, BaseAction.class); + assertTrue(res instanceof DateFormatterAction); + } - @Test - public void deserializerTest(){ - BaseCopyAction action = new BaseCopyAction(); - action.setActionType("map"); - String input = gson.toJson(action); - BaseAction res = gson.fromJson(input, BaseAction.class); - assertEquals(gson.toJson(res), input); - assertTrue(res instanceof MapAction); - } + @Test(expected = JsonParseException.class) + public void shouldReturnJsonParseException() { + //given + ActionDeserializer actionDeserializer = new ActionDeserializer(); + JsonElement jsonElement = mock(JsonElement.class); + when(jsonElement.getAsJsonObject()).thenThrow(RuntimeException.class); + //when + actionDeserializer.deserialize(jsonElement, null, null); + } } \ No newline at end of file -- cgit 1.2.3-korg