ad4d6b1fe4251c264784e10f011c001fc1fda3bf
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 Nokia. 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.openecomp.core.model.types;
22
23 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertThrows;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
27 import static org.mockito.BDDMockito.given;
28 import static org.mockito.Mockito.mock;
29
30 import java.io.IOException;
31 import org.apache.commons.io.IOUtils;
32 import org.apache.commons.lang3.StringUtils;
33 import org.junit.jupiter.api.Test;
34 import org.openecomp.sdc.common.errors.SdcRuntimeException;
35 import org.openecomp.sdc.versioning.dao.types.Version;
36
37
38 public class ServiceTemplateEntityTest {
39
40     private static final byte[] BYTE_ARRAY = new byte[]{0xA, 0xB, 0xC, 0xD};
41
42     private static ServiceTemplate createServiceTemplate() {
43         ServiceTemplate serviceTemplate = new ServiceTemplate();
44         serviceTemplate.setVspId("someIdd");
45         serviceTemplate.setVersion(new Version("best"));
46         serviceTemplate.setName("name");
47         serviceTemplate.setContentData(BYTE_ARRAY);
48         return serviceTemplate;
49     }
50
51     @Test
52     public void shouldReturnNonEmptyEntityType() {
53         ServiceTemplateEntity entity =
54             new ServiceTemplateEntity();
55         assertTrue(StringUtils.isNoneEmpty(entity.getEntityType()));
56     }
57
58     @Test
59     public void shouldHaveFirstClassCitizenIdEqualToVspId() {
60         ServiceTemplateEntity entity =
61             new ServiceTemplateEntity(createServiceTemplate());
62         assertEquals(entity.getId(), entity.getFirstClassCitizenId());
63     }
64
65     @Test
66     public void serviceTemplateGetterShouldReturnCorrectData() throws IOException {
67         ServiceTemplate serviceTemplate = createServiceTemplate();
68         ServiceTemplateEntity entity =
69             new ServiceTemplateEntity(serviceTemplate);
70
71         ServiceTemplate actual = entity.getServiceTemplate();
72
73         assertEquals(serviceTemplate.getVspId(), actual.getVspId());
74         assertEquals(serviceTemplate.getVersion(), actual.getVersion());
75         assertEquals(serviceTemplate.getName(), actual.getName());
76         assertArrayEquals(IOUtils.toByteArray(serviceTemplate.getContent()), IOUtils.toByteArray(actual.getContent()));
77     }
78
79     @Test
80     public void shouldFailOnNullContentBytesSupplied() {
81         ServiceTemplate serviceTemplateMock = mock(ServiceTemplate.class);
82         given(serviceTemplateMock.getContent()).willAnswer(invocation -> {
83             throw new IOException("Test exception");
84         });
85         assertThrows(SdcRuntimeException.class, () -> {
86             new ServiceTemplateEntity(serviceTemplateMock);
87         });
88     }
89 }