bc1735b06284bce1f0644917b149576a17996dce
[vid.git] / vid-app-common / src / test / java / org / onap / vid / mso / rest / TaskTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.vid.mso.rest;
22
23 import static com.google.code.beanmatchers.BeanMatchers.hasValidGettersAndSetters;
24 import static net.javacrumbs.jsonunit.JsonMatchers.jsonEquals;
25 import static org.hamcrest.CoreMatchers.is;
26 import static org.hamcrest.MatcherAssert.assertThat;
27
28 import com.fasterxml.jackson.databind.ObjectMapper;
29 import com.google.common.collect.ImmutableList;
30 import java.io.IOException;
31 import org.onap.vid.testUtils.TestUtils;
32 import org.testng.annotations.Test;
33
34 public class TaskTest {
35
36     private final ObjectMapper mapper = new ObjectMapper();
37
38     private String templateTaskJson(String insertion) {
39         return ""
40             + "{ "
41             + "  \"taskId\": \"taskId\", "
42             + "  \"type\": \"type\", "
43             + "  \"nfRole\": \"nfRole\", "
44             + "  \"subscriptionServiceType\": \"subscriptionServiceType\", "
45             + "  \"originalRequestId\": \"originalRequestId\", "
46             + "  \"originalRequestorId\": \"originalRequestorId\", "
47             + "  \"buildingBlockName\": \"buildingBlockName\", "
48             + "  \"buildingBlockStep\": \"buildingBlockStep\", "
49             + "  \"errorSource\": \"errorSource\", "
50             + "  \"errorCode\": \"errorCode\", "
51             + "  \"errorMessage\": \"errorMessage\", "
52             + insertion
53             + "  \"validResponses\": [ "
54             + "    \"a\", "
55             + "    \"b\", "
56             + "    \"c\" "
57             + "  ] "
58             + "} ";
59     }
60
61     private final String TASK_JSON = templateTaskJson(""
62         + "  \"description\": \"description\", "
63         + "  \"timeout\": \"timeout\", "
64     );
65
66     private final String TASK_JSON_WITHOUT_TIMEOUT = templateTaskJson("");
67
68     private Task newTaskWithPopulatedFields() {
69         Task task = TestUtils.setStringsInStringFields(new Task());
70         task.setValidResponses(ImmutableList.of("a", "b", "c"));
71         return task;
72     }
73
74     @Test
75     public void shouldHaveProperSettersAndGetters() {
76         assertThat(Task.class, hasValidGettersAndSetters());
77     }
78
79     @Test
80     public void serializeTask() throws IOException {
81         assertThat(
82             mapper.writeValueAsString(newTaskWithPopulatedFields()),
83             jsonEquals(TASK_JSON)
84         );
85     }
86
87     @Test
88     public void deserializeTask() throws IOException {
89         assertThat(
90             mapper.readValue(TASK_JSON, Task.class),
91             is(newTaskWithPopulatedFields())
92         );
93     }
94
95     @Test
96     public void deserializeTaskWithoutTimeout() throws IOException {
97         /*
98         SO may return no timeout, and therefore no description as well
99          */
100         final Task taskWithoutTimeout = newTaskWithPopulatedFields();
101         taskWithoutTimeout.setDescription(null);
102         taskWithoutTimeout.setTimeout(null);
103
104         assertThat(
105             mapper.readValue(TASK_JSON_WITHOUT_TIMEOUT, Task.class),
106             is(taskWithoutTimeout)
107         );
108     }
109 }