Implant vid-app-common org.onap.vid.job (main and test)
[vid.git] / vid-app-common / src / test / java / org / onap / vid / job / command / JobCommandFactoryTest.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.job.command;
22
23 import com.fasterxml.jackson.annotation.JsonCreator;
24 import com.fasterxml.jackson.annotation.JsonProperty;
25 import com.google.common.collect.ImmutableMap;
26 import org.mockito.Mock;
27 import org.mockito.MockitoAnnotations;
28 import org.onap.vid.job.Job;
29 import org.onap.vid.job.JobAdapter;
30 import org.onap.vid.job.JobCommand;
31 import org.onap.vid.job.JobType;
32 import org.onap.vid.job.impl.JobSharedData;
33 import org.testng.annotations.BeforeMethod;
34 import org.testng.annotations.DataProvider;
35 import org.testng.annotations.Test;
36
37 import java.util.Arrays;
38 import java.util.Map;
39 import java.util.Objects;
40 import java.util.UUID;
41 import java.util.stream.Collectors;
42
43 import static org.hamcrest.CoreMatchers.equalTo;
44 import static org.hamcrest.MatcherAssert.assertThat;
45 import static org.mockito.Mockito.verify;
46 import static org.mockito.Mockito.when;
47
48 public class JobCommandFactoryTest {
49
50     private JobCommandFactory jobCommandFactory;
51
52     @Mock
53     private Job job;
54
55     @Mock
56     private JobCommand mockCommand;
57
58     @BeforeMethod
59     public void initMocks() {
60         MockitoAnnotations.initMocks(this);
61     }
62
63     @BeforeMethod
64     public void setUp() {
65         jobCommandFactory = new JobCommandFactory(any -> mockCommand);
66     }
67
68     @DataProvider
69     public Object[][] jobTypes() {
70         return Arrays.stream(
71                 JobType.values()
72         ).map(v -> new Object[]{v}).collect(Collectors.toList()).toArray(new Object[][]{});
73
74     }
75
76     public static class MockedRequest implements JobAdapter.AsyncJobRequest {
77
78         final public int x;
79         final public String y;
80
81         @JsonCreator
82         public MockedRequest(@JsonProperty("x")int x, @JsonProperty("y")String y) {
83             this.x = x;
84             this.y = y;
85         }
86
87         @Override
88         public boolean equals(Object o) {
89             if (this == o) return true;
90             if (!(o instanceof MockedRequest)) return false;
91             MockedRequest that = (MockedRequest) o;
92             return x == that.x &&
93                     Objects.equals(y, that.y);
94         }
95
96         @Override
97         public int hashCode() {
98
99             return Objects.hash(x, y);
100         }
101     }
102
103     @Test(dataProvider = "jobTypes")
104     public void givenJob_createCommandCallsTheInitAndReturnsTheInstance(JobType jobType) {
105
106         final UUID uuid = UUID.randomUUID();
107         final Map<String, Object> data = ImmutableMap.of("foo", "bar");
108         final JobSharedData sharedData = new JobSharedData(uuid, "userid", new MockedRequest(1,"a"), "testApi");
109
110         when(job.getType()).thenReturn(jobType);
111         when(job.getUuid()).thenReturn(uuid);
112         when(job.getData()).thenReturn(data);
113         when(job.getSharedData()).thenReturn(sharedData);
114
115         final JobCommand command = jobCommandFactory.toCommand(job);
116
117         verify(mockCommand).init(sharedData, data);
118
119         assertThat(command, equalTo(mockCommand));
120     }
121
122 }