blob: 0ea9933a46d875f66108715cf75392a1184a38f1 [file] [log] [blame]
Aharoni, Pavel (pa0916)8c70f072018-11-18 00:07:12 +02001/*-
2 * ============LICENSE_START=======================================================
3 * OSAM
4 * ================================================================================
5 * Copyright (C) 2018 AT&T
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 */
20package org.onap.osam.job.command;
21
22import com.fasterxml.jackson.annotation.JsonCreator;
23import com.fasterxml.jackson.annotation.JsonProperty;
24import com.google.common.collect.ImmutableMap;
25import org.mockito.Mock;
26import org.mockito.MockitoAnnotations;
27import org.onap.osam.job.dao.job.OsamJob;
28import org.onap.osam.job.IJobFactory;
29import org.onap.osam.job.IJobCommand;
30import org.onap.osam.job.JobType;
31import org.onap.osam.job.command.JobCommandFactory;
32import org.onap.osam.job.impl.JobSharedData;
33import org.testng.annotations.BeforeMethod;
34import org.testng.annotations.DataProvider;
35import org.testng.annotations.Test;
36
37import java.util.Arrays;
38import java.util.Map;
39import java.util.Objects;
40import java.util.UUID;
41import java.util.stream.Collectors;
42
43import static org.hamcrest.CoreMatchers.equalTo;
44import static org.hamcrest.MatcherAssert.assertThat;
45import static org.mockito.Mockito.verify;
46import static org.mockito.Mockito.when;
47
48public class JobCommandFactoryTest {
49
50 private JobCommandFactory jobCommandFactory;
51
52 @Mock
53 private OsamJob job;
54
55 @Mock
56 private IJobCommand 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 IJobFactory.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"));
109
110 when(job.getType()).thenReturn(jobType);
111 when(job.getUuid()).thenReturn(uuid);
112 when(job.getDataMap()).thenReturn(data);
113 when(job.getSharedData()).thenReturn(sharedData);
114
115 final IJobCommand command = jobCommandFactory.toCommand(job);
116
117 verify(mockCommand).init(sharedData, data);
118
119 assertThat(command, equalTo(mockCommand));
120 }
121
122}