60214eb8b11332e8b0392851f4d08ea4dd2629ce
[dcaegen2/services/sdk.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * DCAEGEN2-SERVICES-SDK
4  * ================================================================================
5  * Copyright (C) 2020 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.onap.dcaegen2.services.sdk.services.external.schema.manager.service;
22
23 import org.junit.jupiter.api.Assertions;
24 import org.junit.jupiter.api.BeforeAll;
25 import org.junit.jupiter.api.Test;
26 import org.onap.dcaegen2.services.sdk.services.external.schema.manager.exception.NoLocalReferenceException;
27
28 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
29 import static org.junit.jupiter.api.Assertions.assertEquals;
30 import static org.junit.jupiter.api.Assertions.assertThrows;
31
32 class UrlMapperTest {
33     private static final String MAPPING_FILE_PATH = "src/main/test/resources/schema-map-to-tests.json";
34     private static final String SCHEMAS_PATH = "src/main/test/resources";
35     private static UrlMapper urlMapper;
36
37     @BeforeAll
38     public static void setUp() {
39         urlMapper = new UrlMapperFactory().getUrlMapper(MAPPING_FILE_PATH, SCHEMAS_PATH);
40     }
41
42     @Test
43     public void shouldThrowExceptionWhenNoMappingExists() {
44         //given
45         String notMappedPublicUrl = "http://localhost:8080/notExisting";
46
47         //when
48         //then
49         assertThrows(NoLocalReferenceException.class, () -> urlMapper.mapToLocalUrl(notMappedPublicUrl));
50     }
51
52     @Test
53     public void shouldThrowExceptionWhenLocalSchemaFileIsEmpty() {
54         //given
55         String publicUrlToEmptyLocal = "http://someExternalUrl/emptySchema";
56
57         //when
58         //then
59         assertThrows(NoLocalReferenceException.class, () -> urlMapper.mapToLocalUrl(publicUrlToEmptyLocal));
60     }
61
62     @Test
63     public void shouldThrowExceptionWhenFileHasInvalidYamlStructure() {
64         //given
65         String publicUrlToInvalidYamlLocal = "http://someExternalUrl/invalidYamlFile";
66
67         //when
68         //then
69         assertThrows(NoLocalReferenceException.class, () -> urlMapper.mapToLocalUrl(publicUrlToInvalidYamlLocal));
70     }
71
72     @Test
73     public void shouldThrowExceptionWhenLocalFileDoesNotExist() {
74         //given
75         String publicUrlToNotExistingLocalFile = "http://someExternalUrl/missingFile";
76
77         //when
78         //then
79         assertThrows(NoLocalReferenceException.class, () -> urlMapper.mapToLocalUrl(publicUrlToNotExistingLocalFile));
80     }
81
82     @Test
83     public void shouldReturnLocalUrlWhenFileValidAndFound() {
84         //given
85         String publicUrl = "http://someExternalUrl/external";
86
87         //when
88         //then
89         assertEquals("file_with_one_line.json", urlMapper.mapToLocalUrl(publicUrl));
90     }
91
92     @Test
93     public void shouldNotThrowExceptionWhenMappingFileDoesNotExist() {
94         String invalidMappingFilePath = "src/main/test/resources/missing-schema.json";
95
96         Assertions.assertDoesNotThrow(() -> new UrlMapperFactory().getUrlMapper(invalidMappingFilePath, SCHEMAS_PATH));
97     }
98 }