2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.vid.job.command;
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;
37 import java.util.Arrays;
39 import java.util.Objects;
40 import java.util.UUID;
41 import java.util.stream.Collectors;
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;
48 public class JobCommandFactoryTest {
50 private JobCommandFactory jobCommandFactory;
56 private JobCommand mockCommand;
59 public void initMocks() {
60 MockitoAnnotations.initMocks(this);
65 jobCommandFactory = new JobCommandFactory(any -> mockCommand);
69 public Object[][] jobTypes() {
72 ).map(v -> new Object[]{v}).collect(Collectors.toList()).toArray(new Object[][]{});
76 public static class MockedRequest implements JobAdapter.AsyncJobRequest {
79 final public String y;
82 public MockedRequest(@JsonProperty("x")int x, @JsonProperty("y")String y) {
88 public boolean equals(Object o) {
89 if (this == o) return true;
90 if (!(o instanceof MockedRequest)) return false;
91 MockedRequest that = (MockedRequest) o;
93 Objects.equals(y, that.y);
97 public int hashCode() {
99 return Objects.hash(x, y);
103 @Test(dataProvider = "jobTypes")
104 public void givenJob_createCommandCallsTheInitAndReturnsTheInstance(JobType jobType) {
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");
110 when(job.getType()).thenReturn(jobType);
111 when(job.getUuid()).thenReturn(uuid);
112 when(job.getData()).thenReturn(data);
113 when(job.getSharedData()).thenReturn(sharedData);
115 final JobCommand command = jobCommandFactory.toCommand(job);
117 verify(mockCommand).init(sharedData, data);
119 assertThat(command, equalTo(mockCommand));