f700a7dd22bae863e39522994491d521ac44a6c3
[ccsdk/cds.git] / ms / cds-sdc-listener / application / src / test / java / org / onap / ccsdk / cds / cdssdclistener / handler / BluePrintProcessorHandlerTest.java
1 /*
2  * Copyright © 2019 Bell Canada
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onap.ccsdk.cds.cdssdclistener.handler;
17
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.assertTrue;
20 import com.google.protobuf.ByteString;
21 import io.grpc.ManagedChannel;
22 import io.grpc.inprocess.InProcessChannelBuilder;
23 import io.grpc.inprocess.InProcessServerBuilder;
24 import io.grpc.stub.StreamObserver;
25 import io.grpc.testing.GrpcCleanupRule;
26 import java.io.File;
27 import java.io.IOException;
28 import java.nio.file.Paths;
29 import org.apache.commons.io.FileUtils;
30 import org.junit.Before;
31 import org.junit.Rule;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.onap.ccsdk.cds.cdssdclistener.client.CdsSdcListenerAuthClientInterceptor;
35 import org.onap.ccsdk.cds.controllerblueprints.common.api.Status;
36 import org.onap.ccsdk.cds.controllerblueprints.management.api.BluePrintManagementOutput;
37 import org.onap.ccsdk.cds.controllerblueprints.management.api.BluePrintManagementServiceGrpc.BluePrintManagementServiceImplBase;
38 import org.onap.ccsdk.cds.controllerblueprints.management.api.BluePrintUploadInput;
39 import org.onap.ccsdk.cds.controllerblueprints.management.api.FileChunk;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.boot.context.properties.EnableConfigurationProperties;
42 import org.springframework.boot.test.context.SpringBootTest;
43 import org.springframework.test.context.junit4.SpringRunner;
44
45 @RunWith(SpringRunner.class)
46 @EnableConfigurationProperties({BluePrintProcesssorHandler.class, CdsSdcListenerAuthClientInterceptor.class})
47 @SpringBootTest(classes = {BluePrintProcessorHandlerTest.class})
48 public class BluePrintProcessorHandlerTest {
49
50     @Autowired
51     private BluePrintProcesssorHandler bluePrintProcesssorHandler;
52
53     @Autowired
54     private CdsSdcListenerAuthClientInterceptor cdsSdcListenerAuthClientInterceptor;
55
56     @Rule
57     public GrpcCleanupRule grpcCleanup = new GrpcCleanupRule();
58
59     private static final String CBA_ARCHIVE = "src/test/resources/testcba.zip";
60     private static final String SUCCESS_MSG = "Successfully uploaded CBA";
61     private static final int SUCCESS_CODE = 200;
62     private ManagedChannel channel;
63
64     @Before
65     public void setUp() throws IOException {
66         final BluePrintManagementServiceImplBase serviceImplBase = new BluePrintManagementServiceImplBase() {
67             @Override
68             public void uploadBlueprint(BluePrintUploadInput request,
69                 StreamObserver<BluePrintManagementOutput> responseObserver) {
70                 responseObserver.onNext(getBluePrintManagementOutput());
71                 responseObserver.onCompleted();
72             }
73         };
74
75         // Generate server name.
76         String serverName = InProcessServerBuilder.generateName();
77
78         // Create a server, add service, start, and register.
79         grpcCleanup.register(
80             InProcessServerBuilder.forName(serverName).addService(serviceImplBase).directExecutor().build().start());
81
82         // Create a client channel.
83         channel = grpcCleanup.register(InProcessChannelBuilder.forName(serverName).directExecutor().build());
84     }
85
86     @Test
87     public void testApplicationEndPointSucess() throws IOException {
88         // Arrange
89         BluePrintUploadInput request = generateRequest();
90
91         // Act
92         Status output = bluePrintProcesssorHandler.sendRequest(request, channel);
93
94         // Verify
95         assertEquals(SUCCESS_CODE, output.getCode());
96         assertTrue(output.getMessage().contains(SUCCESS_MSG));
97     }
98
99     private BluePrintUploadInput generateRequest() throws IOException {
100         File file = Paths.get(CBA_ARCHIVE).toFile();
101         byte[] bytes = FileUtils.readFileToByteArray(file);
102         FileChunk fileChunk = FileChunk.newBuilder().setChunk(ByteString.copyFrom(bytes)).build();
103
104         return BluePrintUploadInput.newBuilder().setFileChunk(fileChunk).build();
105     }
106
107     private BluePrintManagementOutput getBluePrintManagementOutput() {
108         return BluePrintManagementOutput.newBuilder()
109                 .setStatus(Status.newBuilder().setMessage(SUCCESS_MSG).setCode(200).build())
110                 .build();
111     }
112 }