Aharoni, Pavel (pa0916) | 8c70f07 | 2018-11-18 00:07:12 +0200 | [diff] [blame^] | 1 | /*-
|
| 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 | */
|
| 20 | package org.onap.osam.job.command;
|
| 21 |
|
| 22 | import com.fasterxml.jackson.annotation.JsonCreator;
|
| 23 | import com.fasterxml.jackson.annotation.JsonProperty;
|
| 24 | import com.google.common.collect.ImmutableMap;
|
| 25 | import org.mockito.Mock;
|
| 26 | import org.mockito.MockitoAnnotations;
|
| 27 | import org.onap.osam.job.dao.job.OsamJob;
|
| 28 | import org.onap.osam.job.IJobFactory;
|
| 29 | import org.onap.osam.job.IJobCommand;
|
| 30 | import org.onap.osam.job.JobType;
|
| 31 | import org.onap.osam.job.command.JobCommandFactory;
|
| 32 | import org.onap.osam.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 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 | }
|